server
v1.1.1
Documentation
pkg.go.dev ↗Package server provides an HTTP server implementation that wraps the http.Server from the stdlib, and a Router interface for implementing routing.
Types
API
type API struct {
*Server
// Prefix defines the router prefix to register the API routes against.
Prefix string
// contains filtered or unexported fields
}
API wraps the Server struct, and uses a separate mux.Router for serving routes for the API.
API.Init
func (s *API) Init()
Init will initialize the API server. This will create a new subrouter to register the routes on if the Prefix for the API server was set.
API.Register
func (s *API) Register(name string, gates ...web.Gate)
Register will register the API routers of the given name with the given gates.
MiddlewareFunc
type MiddlewareFunc func(http.Handler) http.Handler
Router
type Router interface {
// RegisterUI will register the router's UI routes with the given
// mux.Router. It will also pass through the CSRF middleware function, and
// a variadic list of gates to apply to the routes being registered.
RegisterUI(*mux.Router, func(http.Handler) http.Handler, ...web.Gate)
// RegisterAPI will register the router's API routes with the given
// mux.Router. Unlike RegisterUI, this does not take a CSRF middleware
// function, and only the variadic list of gates.
RegisterAPI(string, *mux.Router, ...web.Gate)
}
Router defines how a router should be implemented to be used for the Server. It is perfectly valid for the RegisterUI or RegisterAPI methods to be simple stubs to statisfy the interface is a Router doesn't need to expose either via the Server.
Server
type Server struct {
*http.Server
Log *log.Logger // Log is the logger to use for application logging.
Router *mux.Router // Router is the mux.Router to use for registering routes.
Routers map[string]Router // Routers defines the routers for the server, along with their name.
// Cert and Key define the paths to the certificate and key to use for
// serving over TLS.
Cert string
Key string
}
Server is a wrapper around the stdlib http.Server. It provides a simple mechanism of adding Routers for routing requests.
Server.AddRouter
func (s *Server) AddRouter(name string, r Router)
AddRouter adds the given router to the server with the given name. If the router already exists, then it will be replaced.
Server.Init
func (s *Server) Init(middleware ...MiddlewareFunc)
Init will initialize the server, and apply the given list of middleware functions to all of the routes added to the server.
Server.Serve
func (s *Server) Serve() error
Serve will bind the server to the given address. If a certificate and key were given, then the server will be served over TLS.
UI
type UI struct {
*Server
// CSRF defines the middleware function to use for protecting form submissions
// from CSRF attacks.
CSRF func(http.Handler) http.Handler
}
UI wraps the Server struct, and provides CSRF middleware for each route.
UI.Init
func (s *UI) Init()
Init will initialize the UI server, and register the necessary types with gob for encoding session data, such as form errors, form fields, and alerts. This will also wrap the underlying Router with a handler for spoofing HTTP methods, such as PATCH, and DELETE.
UI.Register
func (s *UI) Register(name string, gates ...web.Gate)
Register will register the UI routers of the given name with the given gates.