Documentation ¶
Index ¶
- Variables
- func CopyFileSys(filesys fs.FS, onHook OnHookFunc) (*memfs.FS, error)
- func InjectWebEnv(filesys fs.FS, conf any, ns string) (*memfs.FS, error)
- func NewStaticFilesHandler(filesys fs.FS, fn ...staticFilesHandlerFunc) (http.Handler, error)
- func WithBasePath(basePath string) staticFilesHandlerFunc
- func WithInjectWebEnv(env any, namespace string) staticFilesHandlerFunc
- func WithLogger(logger *slog.Logger) staticFilesHandlerFunc
- func WithMuxErrorHandler(handler func(int) http.Handler) staticFilesHandlerFunc
- type OnHookFunc
- type StaticFilesHandler
Constants ¶
This section is empty.
Variables ¶
var ErrCouldNotAppendScript = errors.New("could not append script")
var ErrCouldNotAppendToIndex = errors.New("could not append to index")
var ErrCouldNotFindHead = errors.New("could not find <head> tag")
var ErrCouldNotMakeDir = errors.New("could not make dir")
var ErrCouldNotMarshalConfig = errors.New("could not marshal config")
injectWebEnv.InjectWindowVars
var ErrCouldNotOpenFile = errors.New("could not open file")
var ErrCouldNotParseIndex = errors.New("could not parse index")
injectWebEnv.appendToIndex
var ErrCouldNotParseNamespace = errors.New("namespace must match regex: ^[a-zA-Z_][a-zA-Z0-9_]*$")
var ErrCouldNotReadFile = errors.New("could not read file")
var ErrCouldNotWriteFile = errors.New("could not write file")
var ErrCouldNotWriteIndex = errors.New("could not write index")
var ErrNoIndexFound = errors.New("no index.html found")
var ErrNoNamespace = errors.New("no namespace provided")
var ErrUnexpectedWalkError = errors.New("unexpected walk error")
Functions ¶
func CopyFileSys ¶
func InjectWebEnv ¶
InjectWebEnv injects the web environment into the index.html file of the given file system.
- filesys: the file system to inject the web environment into
- conf: the web environment to inject, use json struct tags to drive the marshalling
- ns: the namespace to use for the web environment, must match regex: ^[a-zA-Z_][a-zA-Z0-9_]*$
func NewStaticFilesHandler ¶
StaticFilesHandler creates a static file server handler that serves files from the given fs.FS. It serves index.html for the root path and 404 for actual static file requests that don't exist.
- ctx: the context
- filesys: the file system to serve files from - this will be copied to a memfs
- fn: optional functions to configure the handler (e.g. WithLogger, WithBasePath, WithMuxErrorHandler, WithInjectWebEnv)
func WithBasePath ¶
func WithBasePath(basePath string) staticFilesHandlerFunc
WithBasePath sets the base path for the web server which will be trimmed from the request path before looking up files.
func WithInjectWebEnv ¶
WithInjectWebEnv injects the web environment into the static file server.
env: the web environment to inject, use json struct tags to drive the marshalling namespace: the namespace to use for the web environment, defaults to "APP_ENV"
func WithLogger ¶
WithLogger sets the logger for the static file server. Defaults to slog.Logger.
func WithMuxErrorHandler ¶
WithMuxErrorHandler sets custom error handlers for the static file server.
handler: a function that returns an http.Handler for the given status code
Types ¶
type OnHookFunc ¶
OnHookFunc is a function that can be used to modify the data of a file before it is written to the memfs. The function should return the modified data and an error if one occurred.
type StaticFilesHandler ¶
type StaticFilesHandler struct {
// contains filtered or unexported fields
}
func (*StaticFilesHandler) ServeHTTP ¶
func (h *StaticFilesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)