Documentation
¶
Overview ¶
Package web provides a general-purpose web server for serving HTML pages (html/template)
Index ¶
- type Config
- type Controller
- type Option
- type Route
- type Server
- func (s *Server) Addr() string
- func (s *Server) Handle(pattern string, h http.Handler)
- func (s *Server) HandleFunc(pattern string, h http.HandlerFunc)
- func (s *Server) Handler() http.Handler
- func (s *Server) NotFoundHandler(h http.HandlerFunc)
- func (s *Server) Register(controllers ...Controller)
- func (s *Server) Run(ctx context.Context) error
- func (s *Server) Static(urlPath, dir string)
- func (s *Server) Templates() *Templates
- func (s *Server) Use(mw ...middleware.Middleware)
- type Templates
- type TemplatesOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Host is the address the server listens on:
// "0.0.0.0" for all interfaces,
// "127.0.0.1" for local connections only.
Host string `yaml:"host" default:"0.0.0.0"`
// Port is the server's TCP port.
Port int `yaml:"port" default:"8080" env:"SERVER_PORT"`
// ReadTimeout is the maximum duration for reading the entire request,
// including the body, from the time the connection is established.
ReadTimeout time.Duration `yaml:"read_timeout" default:"15s"`
// ReadHeaderTimeout is the maximum duration for reading request
// headers. Provides separate protection against slow clients (Slowloris).
ReadHeaderTimeout time.Duration `yaml:"read_header_timeout" default:"5s"`
// WriteTimeout is the maximum duration for writing the response.
WriteTimeout time.Duration `yaml:"write_timeout" default:"15s"`
// IdleTimeout is how long to keep a keep-alive connection open
// between requests from the same client.
IdleTimeout time.Duration `yaml:"idle_timeout" default:"60s"`
// ShutdownTimeout is how long to wait for active requests to complete
// during server shutdown (graceful shutdown) before forcibly
// terminating them.
ShutdownTimeout time.Duration `yaml:"shutdown_timeout" default:"10s"`
// MaxHeaderBytes is the maximum total size of request headers
// in bytes. Protects against excessively large headers.
MaxHeaderBytes int `yaml:"max_header_bytes" default:"1048576"` // 1 MB
// TemplatesDir is the directory with HTML templates (html/template),
// scanned recursively. If empty, templates are not loaded
// automatically; pass them via WithTemplates if needed.
TemplatesDir string `yaml:"templates_dir"`
// StaticDir is the directory with static files (CSS, JS, images).
// If empty, static file serving is not enabled.
StaticDir string `yaml:"static_dir"`
// StaticURLPath is the URL prefix under which static files are served.
StaticURLPath string `yaml:"static_url_path" default:"/static/"`
// CORS configures the CORS headers for the CORS middleware
CORS middleware.CORSConfig `yaml:"cors"`
}
Config describes the web server configuration
type Controller ¶
type Controller interface {
Routes() []Route
}
Controller represents a unit of web pages: a group of related routes (e.g., all pages in the "user profile" section). Registered via Server.Register.
A typical implementation stores dependencies (templates, data access) as struct fields and returns its methods as handlers:
type HomeController struct {
tmpl *web.Templates
}
func (c *HomeController) Routes() []web.Route {
return []web.Route{
{Pattern: "GET /{$}", Handler: c.Index},
{Pattern: "GET /about", Handler: c.About},
}
}
type Option ¶
type Option func(*Server)
Option configures a Server during creation via New
func WithTemplates ¶
WithTemplates passes pre-loaded templates instead of automatically loading them from Config.TemplatesDir
type Route ¶
type Route struct {
Pattern string
Handler http.HandlerFunc
}
Route describes a single controller route. Pattern is a standard net/http.ServeMux pattern (Go 1.22+), e.g., "GET /{$}", "POST /login", "GET /users/{id}". If no method is specified at the beginning, the route responds to any HTTP method.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a web server: a router, a set of global middleware, (optionally) loaded templates, and a wrapper around http.Server with graceful shutdown support
func Load ¶
Load reads a YAML file (and optional .env files) via the cfg package and immediately builds a Server. Use this function if the application does not have a single unified config.yml and the server settings are the only thing that needs to be loaded
func New ¶
New creates a Server from the provided Config. Use this function if Config is part of the application's overall configuration, already loaded separately (via cfg.Load[AppConfig] with a Server web.Config field). If logger is nil, slog.Default() is used
func (*Server) Addr ¶
Addr returns the address the server is actually listening on, after Run has started accepting connections (useful when Config.Port is 0 — the OS selects a free port). Until that point, it returns an empty string
func (*Server) HandleFunc ¶
func (s *Server) HandleFunc(pattern string, h http.HandlerFunc)
HandleFunc registers a single handler directly, bypassing controllers — for cases where a full Controller is overkill
func (*Server) Handler ¶
Handler returns the final http.Handler — the router wrapped in the global middleware chain. It does not require starting a network listener, making it convenient for testing (httptest.NewServer(srv.Handler())) or if you want to manage the net.Listener and TLS yourself instead of calling Run
func (*Server) NotFoundHandler ¶ added in v0.3.0
func (s *Server) NotFoundHandler(h http.HandlerFunc)
NotFoundHandler sets a custom handler for when no route matches. If h is nil, the default http.NotFound handler is restored.
func (*Server) Register ¶
func (s *Server) Register(controllers ...Controller)
Register registers the routes of one or more controllers
func (*Server) Run ¶
Run starts the server and blocks until ctx is canceled, after which it performs a graceful shutdown with the Config.ShutdownTimeout. Typical call from main:
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if err := srv.Run(ctx); err != nil {
log.Fatal(err)
}
Run can only be called once per Server instance
func (*Server) Static ¶
Static enables serving static files from the dir directory under the urlPath URL prefix. It is called automatically from New if Config.StaticDir is set — use it directly only if you need to serve more than one static directory
func (*Server) Templates ¶
Templates returns the loaded templates (or nil if they were not loaded — TemplatesDir is empty and WithTemplates was not used)
func (*Server) Use ¶ added in v0.2.0
func (s *Server) Use(mw ...middleware.Middleware)
Use adds global middleware applied to all requests. Middleware are executed in the order they are added (the first added is the outermost, see middleware.Chain). Call before Run
type Templates ¶
type Templates struct {
// contains filtered or unexported fields
}
Templates is a wrapper around a set of HTML templates loaded from a directory. It uses the html/template package (instead of text/template) — values are automatically escaped when inserted into HTML, protecting against XSS vulnerabilities when rendering user data.
func LoadTemplates ¶
func LoadTemplates(dir string, opts ...TemplatesOption) (*Templates, error)
LoadTemplates recursively scans dir and parses all found template files using html/template. The name of each template within the set is its filename (e.g., "index.html"), so layouts and partials can be organized into subdirectories arbitrarily — the only requirement is that filenames must be unique within dir.
func (*Templates) Render ¶
Render renders the template name with the given data and writes the result to w with the specified HTTP status. The template is first rendered into a temporary buffer — if rendering fails (e.g., due to a missing field in data), the client will not receive a half-sent page mixed with a 200 status code: headers and body are written to the response only when rendering is guaranteed to succeed.
type TemplatesOption ¶
type TemplatesOption func(*templatesOptions)
TemplatesOption configures LoadTemplates.
func WithExtensions ¶
func WithExtensions(exts ...string) TemplatesOption
WithExtensions specifies the file extensions that are considered templates (by default, only ".html").
func WithFuncs ¶
func WithFuncs(fm template.FuncMap) TemplatesOption
WithFuncs adds custom functions available within templates (similar to template.FuncMap).