provide a way to run app
This commit is contained in:
42
internal/api/http/handlerbuilder.go
Normal file
42
internal/api/http/handlerbuilder.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user