costmodel.go 1.1 KB

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