server

package
v1.23.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 16 Imported by: 80

Documentation

Index

Constants

View Source
const DefaultAdminListen = ":6060"

DefaultAdminListen is the default admin listen address (pprof + metrics + admin API).

View Source
const DefaultAdminLogLevelPath = "/cl"

DefaultAdminLogLevelPath is the admin listener path for runtime log level changes.

View Source
const DefaultMetricsPath = "/metrics"
View Source
const DefaultPprofPath = "/pprof"

DefaultPprofPath is the URL prefix for pprof routes on the admin listener.

Variables

This section is empty.

Functions

func GinHandler added in v1.23.2

func GinHandler(h Handler) gin.HandlerFunc

GinHandler adapts a server.Handler to gin.HandlerFunc (e.g. admin listener routes).

func MetricsHandler added in v1.23.2

func MetricsHandler() http.Handler

MetricsHandler serves Prometheus metrics from the shared registry (admin listener).

func MetricsMiddleware added in v1.23.2

func MetricsMiddleware() gin.HandlerFunc

MetricsMiddleware records HTTP RED metrics into the shared registry (main web server).

func MetricsRegistry added in v1.23.2

func MetricsRegistry() *prometheus.Registry

MetricsRegistry returns the shared Prometheus registry for custom business metrics.

func StartAdmin added in v1.23.7

func StartAdmin(ctx context.Context, cfg AdminConfig) error

StartAdmin listens on cfg.Listen and serves pprof, /metrics, and admin routes. Returns immediately with nil when Listen is empty.

Types

type AdminConfig added in v1.23.7

type AdminConfig struct {
	Listen             string // e.g. DefaultAdminListen; empty disables the listener
	Path               string // pprof prefix, default DefaultPprofPath
	MetricsPath        string // default DefaultMetricsPath
	LogLvl             string // "debug" enables Gin route listing (same as web LogLvl)
	StripTrailingSlash bool   // rewrite /demo/ping/ → /demo/ping before routing (no 301)
	Setup              AdminSetup
}

AdminConfig configures the admin HTTP listener (pprof, metrics, and admin API).

type AdminSetup added in v1.23.5

type AdminSetup func(Router)

AdminSetup registers custom routes on the admin HTTP listener (:6060). Same signature as Component so business code uses server.Router, not gin.

type Component

type Component func(root Router)

type Config

type Config struct {
	Listen             string
	LogLvl             string
	StripTrailingSlash bool // rewrite /api/ → /api before routing (no 301)
}

type Context added in v1.12.6

type Context struct {
	// contains filtered or unexported fields
}

Context is the framework-agnostic HTTP request context passed to handlers and middleware.

func (*Context) BindJSON added in v1.23.2

func (c *Context) BindJSON(v any) error

BindJSON decodes the request body JSON into v.

func (*Context) ClientIP added in v1.23.2

func (c *Context) ClientIP() string

ClientIP returns the client IP address of the request.

func (*Context) FormFile added in v1.23.2

func (c *Context) FormFile(name string) (*multipart.FileHeader, error)

FormFile returns the first file for the given multipart form key.

func (*Context) FormValue added in v1.23.2

func (c *Context) FormValue(name string) string

FormValue returns the first value for the given form key.

func (*Context) Get added in v1.23.2

func (c *Context) Get(key string) (any, bool)

Get retrieves a value previously stored with Set.

func (*Context) GetCookie added in v1.23.2

func (c *Context) GetCookie(name string) (string, error)

GetCookie returns the named cookie from the request.

func (*Context) GetHeader added in v1.23.2

func (c *Context) GetHeader(name string) string

GetHeader returns the request header named name.

func (*Context) Html added in v1.12.6

func (c *Context) Html(layout, view string, data any) error

Html renders a server-side template named view with data. layout is ignored; templates must be loaded via Server.LoadHTMLGlob.

func (*Context) JSON added in v1.23.2

func (c *Context) JSON(resp any)

JSON writes resp as JSON with status 200.

func (*Context) Json added in v1.12.6

func (c *Context) Json(resp any)

Json is an alias for JSON.

func (*Context) Next added in v1.23.2

func (c *Context) Next()

Next invokes the remaining handlers in the middleware chain.

func (*Context) Param added in v1.23.2

func (c *Context) Param(name string) string

Param returns the path parameter named name (e.g. ":id" in the route).

func (*Context) ParamInt added in v1.23.2

func (c *Context) ParamInt(name string) (int, error)

ParamInt parses the path parameter named name as an int.

func (*Context) Path added in v1.23.2

func (c *Context) Path() string

Path returns the request URL path (without query string).

func (*Context) Query added in v1.23.2

func (c *Context) Query(name string) string

Query returns the URL query parameter named name.

func (*Context) QueryInt added in v1.23.2

func (c *Context) QueryInt(name string) (int, error)

QueryInt parses the URL query parameter named name as an int.

func (*Context) Redirect added in v1.23.2

func (c *Context) Redirect(url string, code int)

Redirect sends an HTTP redirect to url with the given status code.

func (*Context) Request added in v1.23.2

func (c *Context) Request() *http.Request

Request returns the underlying HTTP request.

func (*Context) ResponseWriter added in v1.23.2

func (c *Context) ResponseWriter() http.ResponseWriter

ResponseWriter returns the underlying HTTP response writer.

func (*Context) Set added in v1.23.2

func (c *Context) Set(key string, value any)

Set stores a value in the per-request context (visible to later middleware/handlers).

func (*Context) SetCookie added in v1.23.2

func (c *Context) SetCookie(cookie *http.Cookie)

SetCookie adds a Set-Cookie header to the response.

func (*Context) SetHeader added in v1.23.2

func (c *Context) SetHeader(key, value string)

SetHeader sets a response header.

func (*Context) Status added in v1.23.2

func (c *Context) Status(code int)

Status sets the HTTP response status code without writing a body.

func (*Context) Write added in v1.23.2

func (c *Context) Write(data []byte) (int, error)

Write writes raw bytes to the response body.

type Handler added in v1.12.6

type Handler func(ctx Context)

func FileServerNoRoute added in v1.23.4

func FileServerNoRoute(fs http.FileSystem) Handler

FileServerNoRoute serves files from fs for unmatched GET/HEAD requests. Use as Gin NoRoute handler so root StaticFS does not conflict with /api, /m, etc.

type Router added in v1.12.6

type Router interface {
	Get(path string, handlers ...Handler)
	Post(path string, handlers ...Handler)
	Put(path string, handlers ...Handler)
	Delete(path string, handlers ...Handler)
	Patch(path string, handlers ...Handler)
	Options(path string, handlers ...Handler)
	Any(path string, handlers ...Handler)
	Path(prefix string) Router
	MountPprof(relativePath string)
}

func NewRouter added in v1.12.6

func NewRouter(engine *gin.Engine) Router

type Server

type Server struct {
	// contains filtered or unexported fields
}

func New

func New(config Config) *Server

func (*Server) AdvancedConfig

func (s *Server) AdvancedConfig(handler func(*gin.Engine)) *Server

func (*Server) EnableMetrics added in v1.23.2

func (s *Server) EnableMetrics() *Server

EnableMetrics installs HTTP RED Prometheus middleware on the main web server.

func (*Server) GetEngine added in v1.23.2

func (s *Server) GetEngine() *gin.Engine

func (*Server) LoadHTMLGlob added in v1.23.2

func (s *Server) LoadHTMLGlob(pattern string) *Server

func (*Server) RegisterComponent

func (s *Server) RegisterComponent(component Component) *Server

func (*Server) RegisterComponents

func (s *Server) RegisterComponents(components ...Component) *Server

func (*Server) SetHomePage

func (s *Server) SetHomePage(indexHTML string) *Server

func (*Server) SetNoRoute added in v1.23.2

func (s *Server) SetNoRoute(handler Handler) *Server

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

func (*Server) StaticFS added in v1.23.2

func (s *Server) StaticFS(relativePath string, fs http.FileSystem) *Server

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL