costmodel.go 1013 B

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