Files
ocis/ocis-pkg/service/debug/service.go
T

80 lines
1.9 KiB
Go
Raw Normal View History

2019-12-05 11:50:00 +01:00
package debug
import (
"context"
"net"
2019-12-05 11:50:00 +01:00
"net/http"
"net/http/pprof"
2021-08-24 17:48:37 +02:00
chimiddleware "github.com/go-chi/chi/v5/middleware"
2019-12-05 11:50:00 +01:00
"github.com/justinas/alice"
2022-05-04 14:49:59 +02:00
"github.com/owncloud/ocis/v2/ocis-pkg/cors"
"github.com/owncloud/ocis/v2/ocis-pkg/middleware"
graphMiddleware "github.com/owncloud/ocis/v2/services/graph/pkg/middleware"
2019-12-05 11:50:00 +01:00
"github.com/prometheus/client_golang/prometheus/promhttp"
2023-05-27 10:18:24 +02:00
"go.opentelemetry.io/contrib/zpages"
2019-12-05 11:50:00 +01:00
)
// NewService initializes a new debug service.
func NewService(opts ...Option) *http.Server {
dopts := newOptions(opts...)
mux := http.NewServeMux()
mux.Handle("/metrics", alice.New(
graphMiddleware.Token(
2019-12-05 11:50:00 +01:00
dopts.Token,
),
).Then(
promhttp.Handler(),
))
mux.HandleFunc("/healthz", dopts.Health)
mux.HandleFunc("/readyz", dopts.Ready)
2021-11-09 14:14:31 +01:00
if dopts.ConfigDump != nil {
mux.HandleFunc("/config", dopts.ConfigDump)
}
2019-12-05 11:50:00 +01:00
if dopts.Pprof {
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
}
if dopts.Zpages {
2023-05-27 10:18:24 +02:00
h := zpages.NewTracezHandler(zpages.NewSpanProcessor())
mux.Handle("/debug", h)
2019-12-05 11:50:00 +01:00
}
baseCtx := dopts.Context
if baseCtx == nil {
baseCtx = context.Background()
}
2019-12-05 11:50:00 +01:00
return &http.Server{
Addr: dopts.Address,
BaseContext: func(_ net.Listener) context.Context {
return baseCtx
},
2019-12-05 11:50:00 +01:00
Handler: alice.New(
2021-08-12 17:14:41 +02:00
chimiddleware.RealIP,
chimiddleware.RequestID,
2020-11-16 22:13:26 +01:00
middleware.NoCache,
middleware.Cors(
cors.AllowedOrigins(dopts.CorsAllowedOrigins),
cors.AllowedMethods(dopts.CorsAllowedMethods),
cors.AllowedHeaders(dopts.CorsAllowedHeaders),
cors.AllowCredentials(dopts.CorsAllowCredentials),
),
2019-12-05 11:50:00 +01:00
middleware.Secure,
middleware.Version(
dopts.Name,
dopts.Version,
),
).Then(
mux,
),
}
}