server

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorHandler

type ErrorHandler func(c ctx.Ctx, err error)

type Group

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

Group represents a route group with a shared path prefix and middleware.

func (*Group) Group

func (g *Group) Group(prefix string, middleware ...ctx.Handler) *Group

Group creates a sub-group with an additional prefix and middleware.

admin := api.Group("/admin", requireAdmin)
admin.Route(web.MethodGet, "/stats", getStats)   // handles GET /api/v1/admin/stats

func (*Group) Route

func (g *Group) Route(method web.Method, path string, h ctx.Handler)

Route registers a route within this group. The final path is prefix + path. The handler chain is: server middleware -> group middleware -> handler.

type Server

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

func New

func New(host, port string, timeoutSeconds int) *Server

func (*Server) ErrorHandler

func (s *Server) ErrorHandler(h ErrorHandler)

func (*Server) Group

func (s *Server) Group(prefix string, middleware ...ctx.Handler) *Group

Group creates a new route group with the given path prefix and optional middleware. The group middleware runs after server-level middleware and before the route handler.

api := srv.Group("/api/v1", authMiddleware)
api.Route(web.MethodGet, "/users", listUsers)    // handles GET /api/v1/users
api.Route(web.MethodPost, "/users", createUser)   // handles POST /api/v1/users

func (*Server) IsReady

func (s *Server) IsReady() bool

func (*Server) ListenAndShutdown

func (s *Server) ListenAndShutdown(onShutdown ...func()) error

ListenAndShutdown starts the server and blocks until a SIGINT or SIGTERM signal is received, then performs a graceful shutdown. The optional onShutdown callbacks are invoked after the HTTP server stops (use them to close databases, flush logs, etc.). Returns nil when the server shuts down cleanly.

func (*Server) ListenAndShutdownTLS

func (s *Server) ListenAndShutdownTLS(certFile, keyFile string, onShutdown ...func()) error

ListenAndShutdownTLS is like ListenAndShutdown but starts the server with TLS.

func (*Server) Route

func (s *Server) Route(method web.Method, path string, h ctx.Handler)

Route registers a route handler for the given method and path. Path params can be defined as :param or {param}.

func (*Server) SetTLSConfig

func (s *Server) SetTLSConfig(cfg *tls.Config)

SetTLSConfig sets a custom TLS configuration on the server. Call this before StartTLS or ListenAndShutdownTLS for advanced scenarios like mutual TLS or custom cipher suites.

func (*Server) Start

func (s *Server) Start() error

func (*Server) StartTLS

func (s *Server) StartTLS(certFile, keyFile string) error

StartTLS starts the server with TLS using the provided certificate and key files.

func (*Server) Static

func (s *Server) Static(prefix, dir string)

Static serves files from the given directory under the specified URL prefix. The prefix must start and end with a slash (e.g., "/static/").

srv.Static("/static/", "./public")

func (*Server) StaticFS

func (s *Server) StaticFS(prefix string, fsys fs.FS)

StaticFS serves files from the given fs.FS under the specified URL prefix. This is useful for embedding static files with go:embed.

//go:embed static
var staticFiles embed.FS
srv.StaticFS("/static/", staticFiles)

func (*Server) Stop

func (s *Server) Stop() error

func (*Server) Use

func (s *Server) Use(mw ...ctx.Handler)

func (*Server) WaitUntilReady

func (s *Server) WaitUntilReady()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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