costmodel.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package costmodel
  2. import (
  3. "net/http"
  4. "github.com/julienschmidt/httprouter"
  5. "github.com/opencost/opencost/pkg/costmodel"
  6. "github.com/opencost/opencost/pkg/errors"
  7. "github.com/opencost/opencost/pkg/log"
  8. "github.com/opencost/opencost/pkg/metrics"
  9. "github.com/opencost/opencost/pkg/version"
  10. "github.com/prometheus/client_golang/prometheus/promhttp"
  11. "github.com/rs/cors"
  12. )
  13. // CostModelOpts contain configuration options that can be passed to the Execute() method
  14. type CostModelOpts struct {
  15. // Stubbed for future configuration
  16. }
  17. func Healthz(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
  18. w.WriteHeader(200)
  19. w.Header().Set("Content-Length", "0")
  20. w.Header().Set("Content-Type", "text/plain")
  21. }
  22. func Execute(opts *CostModelOpts) error {
  23. log.Infof("Starting cost-model version %s", version.FriendlyVersion())
  24. a := costmodel.Initialize()
  25. rootMux := http.NewServeMux()
  26. a.Router.GET("/healthz", Healthz)
  27. a.Router.GET("/allocation/summary", a.ComputeAllocationHandlerSummary)
  28. rootMux.Handle("/", a.Router)
  29. rootMux.Handle("/metrics", promhttp.Handler())
  30. telemetryHandler := metrics.ResponseMetricMiddleware(rootMux)
  31. handler := cors.AllowAll().Handler(telemetryHandler)
  32. return http.ListenAndServe(":9003", errors.PanicHandlerMiddleware(handler))
  33. }