provide a way to run app

This commit is contained in:
2024-08-10 14:14:38 +03:00
parent ba6ac26bac
commit 0046755c7d
10 changed files with 468 additions and 168 deletions

View File

@ -0,0 +1,42 @@
package http
import (
"log/slog"
"net/http"
"net/http/pprof"
"git.loyso.art/frx/devsim/internal/store"
)
type handlersBuilder struct {
mux *http.ServeMux
}
func NewHandlersBuilder() *handlersBuilder {
return &handlersBuilder{
mux: http.NewServeMux(),
}
}
// MountStatsHandlers mounts stats related handlers.
func (h *handlersBuilder) MountStatsHandlers(sr store.Stats, log *slog.Logger) {
log = log.With(slog.String("api", "http"))
mws := multipleMiddlewares(
middlewarePanicRecovery(log),
middlewareLogger(log),
)
h.mux.Handle("/api/v1/stats/", mws(listStatsHandler(sr)))
h.mux.Handle("/api/v1/stats/{id}", mws(postStatsHandler(sr)))
}
func (s *handlersBuilder) MountProfileHandlers() {
s.mux.HandleFunc("/debug/pprof", pprof.Index)
s.mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
s.mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
}
func (s *handlersBuilder) Build() http.Handler {
return s.mux
}