Documentation ¶
Overview ¶
Package serve provides a way to create and manage an HTTP server, including routing, middleware, and graceful shutdown.
Index ¶
- type CleanupFunc
- type DataFunc
- type Server
- func (s *Server) AddChainedRoute(pattern string, handler http.Handler, chain wrap.Chain)
- func (s *Server) AddChainedRoutes(routes map[string]http.Handler, chain wrap.Chain)
- func (s *Server) AddRoute(pattern string, handler http.Handler, middleware ...wrap.Middleware)
- func (s *Server) AddRoutes(routes map[string]http.Handler, middleware ...wrap.Middleware)
- func (s *Server) BackgroundTask(r *http.Request, fn func() error)
- func (s *Server) BaseConfig() *conf.BaseConfig
- func (s *Server) CacheBuster() string
- func (s *Server) Logger() *slog.Logger
- func (s *Server) NewResponse(r *http.Request) (*render.Response, error)
- func (s *Server) NewTemplateData(r *http.Request) map[string]any
- func (s *Server) RegisterCleanup(fn func())
- func (s *Server) RegisterTemplateData(fn DataFunc)
- func (s *Server) ReportServerError(r *http.Request, err error)
- func (s *Server) Session() *scs.SessionManager
- func (s *Server) Start() error
- func (s *Server) TM() *render.TemplateManager
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CleanupFunc ¶
type CleanupFunc func()
CleanupFunc is a function type that can be used for cleanup tasks when the server shuts down.
type DataFunc ¶
DataFunc is a function type that takes an HTTP request and a pointer to a map of data. It represents a callback function that can be used to populate data for templates.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
func NewServer ¶
func NewServer(config *conf.BaseConfig, logger *slog.Logger, tm *render.TemplateManager, session *scs.SessionManager) *Server
NewServer creates a new server with the given configuration and logger.
func (*Server) AddChainedRoute ¶
AddChainedRoute adds a new route to the server with a chain of middleware It takes a pattern, an http.Handler, and a wrap.Chain struct
func (*Server) AddChainedRoutes ¶
AddChainedRoutes adds multiple routes to the server with a chain of middleware
func (*Server) AddRoute ¶
AddRoute adds a new route to the server, using the newer v1.22 http.Handler interface. It takes a pattern, an http.Handler, and an optional list of middleware.
func (*Server) AddRoutes ¶
AddRoutes adds multiple routes to the server. It takes a map of patterns to http.Handlers and an optional list of middleware.
func (*Server) BackgroundTask ¶
BackgroundTask runs a function in a goroutine, and reports any errors to the server's error logger.
func (*Server) BaseConfig ¶
func (s *Server) BaseConfig() *conf.BaseConfig
BaseConfig returns the server configuration.
func (*Server) CacheBuster ¶
CacheBuster returns a string that can be used to bust the cache on static assets.
func (*Server) NewResponse ¶ added in v0.0.11
NewResponse creates a new Response instance with the TemplateManager.
func (*Server) NewTemplateData ¶
NewTemplateData returns a map of data that can be used in a Go template. It includes the current user, environment, version, and other useful information.
func (*Server) RegisterCleanup ¶
func (s *Server) RegisterCleanup(fn func())
RegisterCleanup registers a cleanup function to be called when the server shuts down.
func (*Server) RegisterTemplateData ¶
RegisterTemplateData registers a function that populates template data each time a template is rendered.
func (*Server) ReportServerError ¶
ReportServerError logs the error and sends an email to the admin
func (*Server) Session ¶
func (s *Server) Session() *scs.SessionManager
Session returns the session manager for the server.
func (*Server) Start ¶
Start starts the server and listens for incoming requests. It will block until the server is shut down.
func (*Server) TM ¶
func (s *Server) TM() *render.TemplateManager
TM returns the template manager for the server.