web

package module
v0.0.0-...-5c4fa2e Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2025 License: BSD-3-Clause Imports: 22 Imported by: 0

README

web

Extracted code for building web applications in Go. Heavily WIP.

Documentation

Index

Constants

View Source
const (
	MiddlewareCSPName         = "csp"
	MiddlewareCSRFName        = "csrf"
	MiddlewareRequestIDName   = "requestid"
	MiddlewareRequestLogName  = "requestlog"
	MiddlewareSessionName     = "session"
	MiddlewareErrorName       = "error"
	MiddlewareStaticName      = "static"
	MiddlewareBaseHeadersName = "baseheaders"
)

Variables

View Source
var DefaultCSPOpts = []csp.HandlerOpt{
	csp.DefaultSrc(`'none'`),
	csp.WithScriptNonce(),
	csp.WithStyleNonce(),
	csp.ImgSrc(`'self'`),
	csp.ConnectSrc(`'self'`),
	csp.FontSrc(`'self'`),
	csp.BaseURI(`'self'`),
	csp.FrameAncestors(`'none'`),
}

Functions

func BaseHeaders

func BaseHeaders(h http.Handler) http.Handler

BaseHeaders sets basic security headers for all requests: - X-Frame-Options: SAMEORIGIN - X-XSS-Protection: 1; mode=block - X-Content-Type-Options: nosniff

func NoopHandler

func NoopHandler(h http.Handler) http.Handler

NoopHandler can be used to explicitly opt-out of a handler.

func TemplateFuncs

func TemplateFuncs(ctx context.Context, addlFuncs template.FuncMap) template.FuncMap

Types

type BrowserHandlerFunc

type BrowserHandlerFunc func(context.Context, ResponseWriter, *Request) error

func (BrowserHandlerFunc) ServeHTTP

func (b BrowserHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request)

type BrowserResponse

type BrowserResponse interface {
	// contains filtered or unexported methods
}

type CommonResponse

type CommonResponse struct {
	Cookies []*http.Cookie
}

type Config

type Config struct {
	BaseURL        *url.URL
	SessionManager *session.Manager
	ErrorHandler   func(w http.ResponseWriter, r *http.Request, err error)
	Static         fs.FS
	CSPOpts        []csp.HandlerOpt
	// ScriptNonce indicates that a nonce should be used for inline scripts.
	// This will update the CSP, and the template func will return a value.
	ScriptNonce bool
	// ScriptNonce indicates that a nonce should be used for inline styles. This
	// will update the CSP, and the template func will return a value.
	StyleNonce bool
	// AdditionalBrowserMiddleware is a set of middleware that will be added to
	// all browser handlers, after the base middleware.
	AdditionalBrowserMiddleware []func(http.Handler) http.Handler

	/* start new section */
	CSRFHandler func(http.Handler) http.Handler
}

type HandlerOpt

type HandlerOpt func(r *http.Request) *http.Request

HandlerOpt are functions that can be used to provide options to middleware serving a request. When registered on a handler, they will be called before the request hits the middleware stack.

type JSONResponse

type JSONResponse struct {
	CommonResponse
	// Data to be marshaled to JSON
	Data any
}

type NilResponse

type NilResponse struct {
	CommonResponse
}

NilResponse indicates that no action should be taken. This should be used if the response was handled directly.

type RedirectResponse

type RedirectResponse struct {
	CommonResponse
	// Code for redirect. If not set, http.StatusSeeOther(303) will be used
	Code int
	// URL to redirect to
	URL string
}

type Request

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

func NewRequestFrom

func NewRequestFrom(r *http.Request) *Request

func (*Request) Cookie

func (b *Request) Cookie(name string) (*http.Cookie, error)

func (*Request) DecodeForm

func (b *Request) DecodeForm(target any) error

DecodeForm unpacks the POST form into the target. The target type should be tagged appropriately.

func (*Request) PathValue

func (b *Request) PathValue(name string) string

func (*Request) PostForm

func (b *Request) PostForm() url.Values

func (*Request) RawRequest

func (b *Request) RawRequest() *http.Request

RawRequest returns the raw http.Request underlying this request.

func (*Request) Session

func (b *Request) Session() *session.Session

Session returns the session associated with this request.

func (*Request) URL

func (b *Request) URL() *url.URL

func (*Request) UnmarshalJSONBody

func (b *Request) UnmarshalJSONBody(target any) error

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	WriteResponse(r *Request, resp BrowserResponse) error
}

func NewResponseWriter

func NewResponseWriter(w http.ResponseWriter) ResponseWriter

NewResponseWriter creates a new ResponseWriter

type Server

type Server struct {
	BrowserMux        *http.ServeMux
	BrowserMiddleware *middleware.Chain

	RawMux *http.ServeMux

	BaseMiddleware *middleware.Chain

	HTTPServer *http.Server
	// contains filtered or unexported fields
}

func NewServer

func NewServer(c *Config) (*Server, error)

func (*Server) Handle

func (s *Server) Handle(pattern string, h http.Handler, opts ...HandlerOpt)

func (*Server) HandleFunc

func (s *Server) HandleFunc(pattern string, h func(w http.ResponseWriter, r *http.Request), opts ...HandlerOpt)

func (*Server) HandleRaw

func (s *Server) HandleRaw(pattern string, handler http.Handler)

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

type TemplateResponse

type TemplateResponse struct {
	CommonResponse
	Name string
	// Funcs are additional functions merged in to the rendered template.
	Funcs template.FuncMap
	// Templates to render response from. If not set, the configured templates
	// on the server are used.
	Templates *template.Template
	Data      any
}

Directories

Path Synopsis
Package cors provides a middleware to handle CORS pre-flight requests.
Package cors provides a middleware to handle CORS pre-flight requests.
Package requestid allows the generation and propagation of request ID's via context, and HTTP calls.
Package requestid allows the generation and propagation of request ID's via context, and HTTP calls.

Jump to

Keyboard shortcuts

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