serve

package
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package serve provides a way to create and manage an HTTP server, including routing, middleware, and graceful shutdown.

Index

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

type DataFunc func(r *http.Request, data *map[string]any)

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

func (s *Server) AddChainedRoute(pattern string, handler http.Handler, chain wrap.Chain)

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

func (s *Server) AddChainedRoutes(routes map[string]http.Handler, chain wrap.Chain)

AddChainedRoutes adds multiple routes to the server with a chain of middleware

func (*Server) AddRoute

func (s *Server) AddRoute(pattern string, handler http.Handler, middleware ...wrap.Middleware)

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

func (s *Server) AddRoutes(routes map[string]http.Handler, middleware ...wrap.Middleware)

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

func (s *Server) BackgroundTask(r *http.Request, fn func() error)

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

func (s *Server) CacheBuster() string

CacheBuster returns a string that can be used to bust the cache on static assets.

func (*Server) Logger

func (s *Server) Logger() *slog.Logger

Logger returns the logger for the server.

func (*Server) NewResponse added in v0.0.11

func (s *Server) NewResponse(r *http.Request) (*render.Response, error)

NewResponse creates a new Response instance with the TemplateManager.

func (*Server) NewTemplateData

func (s *Server) NewTemplateData(r *http.Request) map[string]any

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

func (s *Server) RegisterTemplateData(fn DataFunc)

RegisterTemplateData registers a function that populates template data each time a template is rendered.

func (*Server) ReportServerError

func (s *Server) ReportServerError(r *http.Request, err error)

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

func (s *Server) Start() error

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.

Jump to

Keyboard shortcuts

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