Initial QSfera import

This commit is contained in:
Курнат Андрей
2026-06-07 10:20:04 +03:00
commit 2315f25754
16485 changed files with 4826827 additions and 0 deletions
+146
View File
@@ -0,0 +1,146 @@
package debug
import (
"context"
"net/http"
"github.com/qsfera/server/pkg/log"
)
// Option defines a single option function.
type Option func(o *Options)
// Options defines the available options for this package.
type Options struct {
Logger log.Logger
Context context.Context
Name string
Version string
Address string
Token string
Pprof bool
Zpages bool
Health http.Handler
Ready http.Handler
ConfigDump http.Handler
CorsAllowedOrigins []string
CorsAllowedMethods []string
CorsAllowedHeaders []string
CorsAllowCredentials bool
}
// newOptions initializes the available default options.
func newOptions(opts ...Option) Options {
opt := Options{}
for _, o := range opts {
o(&opt)
}
return opt
}
// Logger provides a function to set the logger option.
func Logger(l log.Logger) Option {
return func(o *Options) {
o.Logger = l
}
}
// Context provides a function to set the context option.
func Context(ctx context.Context) Option {
return func(o *Options) {
o.Context = ctx
}
}
// Name provides a function to set the name option.
func Name(n string) Option {
return func(o *Options) {
o.Name = n
}
}
// Version provides a function to set the version option.
func Version(v string) Option {
return func(o *Options) {
o.Version = v
}
}
// Address provides a function to set the address option.
func Address(a string) Option {
return func(o *Options) {
o.Address = a
}
}
// Token provides a function to set the token option.
func Token(t string) Option {
return func(o *Options) {
o.Token = t
}
}
// Pprof provides a function to set the pprof option.
func Pprof(p bool) Option {
return func(o *Options) {
o.Pprof = p
}
}
// Zpages provides a function to set the zpages option.
func Zpages(z bool) Option {
return func(o *Options) {
o.Zpages = z
}
}
// Health provides a function to set the health option.
func Health(h http.Handler) Option {
return func(o *Options) {
o.Health = h
}
}
// Ready provides a function to set the ready option.
func Ready(r http.Handler) Option {
return func(o *Options) {
o.Ready = r
}
}
// ConfigDump to be documented.
func ConfigDump(r http.Handler) Option {
return func(o *Options) {
o.ConfigDump = r
}
}
// CorsAllowedOrigins provides a function to set the CorsAllowedOrigin option.
func CorsAllowedOrigins(origins []string) Option {
return func(o *Options) {
o.CorsAllowedOrigins = origins
}
}
// CorsAllowedMethods provides a function to set the CorsAllowedMethods option.
func CorsAllowedMethods(methods []string) Option {
return func(o *Options) {
o.CorsAllowedMethods = methods
}
}
// CorsAllowedHeaders provides a function to set the CorsAllowedHeaders option.
func CorsAllowedHeaders(headers []string) Option {
return func(o *Options) {
o.CorsAllowedHeaders = headers
}
}
// CorsAllowCredentials provides a function to set the CorsAllowAllowCredential option.
func CorsAllowCredentials(allow bool) Option {
return func(o *Options) {
o.CorsAllowCredentials = allow
}
}
+93
View File
@@ -0,0 +1,93 @@
package debug
import (
"context"
"net"
"net/http"
"net/http/pprof"
chimiddleware "github.com/go-chi/chi/v5/middleware"
"github.com/justinas/alice"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/contrib/zpages"
"github.com/qsfera/server/pkg/cors"
"github.com/qsfera/server/pkg/handlers"
"github.com/qsfera/server/pkg/log"
"github.com/qsfera/server/pkg/middleware"
graphMiddleware "github.com/qsfera/server/services/graph/pkg/middleware"
)
var handleProbe = func(mux *http.ServeMux, pattern string, h http.Handler, logger log.Logger) {
if h == nil {
h = handlers.NewCheckHandler(handlers.NewCheckHandlerConfiguration())
logger.Info().
Str("endpoint", pattern).
Msg("no probe provided, reverting to default (OK)")
}
mux.Handle(pattern, h)
}
// 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(
dopts.Token,
),
).Then(
promhttp.Handler(),
))
handleProbe(mux, "/healthz", dopts.Health, dopts.Logger) // healthiness check
handleProbe(mux, "/readyz", dopts.Ready, dopts.Logger) // readiness check
if dopts.ConfigDump != nil {
mux.Handle("/config", dopts.ConfigDump)
}
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 {
h := zpages.NewTracezHandler(zpages.NewSpanProcessor())
mux.Handle("/debug", h)
}
baseCtx := dopts.Context
if baseCtx == nil {
baseCtx = context.Background()
}
return &http.Server{
Addr: dopts.Address,
BaseContext: func(_ net.Listener) context.Context {
return baseCtx
},
Handler: alice.New(
chimiddleware.RealIP,
chimiddleware.RequestID,
middleware.NoCache,
middleware.Cors(
cors.AllowedOrigins(dopts.CorsAllowedOrigins),
cors.AllowedMethods(dopts.CorsAllowedMethods),
cors.AllowedHeaders(dopts.CorsAllowedHeaders),
cors.AllowCredentials(dopts.CorsAllowCredentials),
),
middleware.Version(
dopts.Name,
dopts.Version,
),
).Then(
mux,
),
}
}