spaserve

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2024 License: MIT Imports: 14 Imported by: 0

README

Go SPA Serve

codecov

Go SPA Serve is a simple package that serves static files specifically focused on serving single page applications (SPA). Generally, this can be used to instead of the stdlib http.FileServer, but the main use case is to serve a SPA with a single entry point (e.g. index.html) and let the client-side router handle the rest.

In addition to serving static files, this package also provides a way to load environment variables from a Go struct at runtime by injecting them into the head tag of the served HTML file.

Problem

Vite is a fantastic build tool, but it doesn't support loading environment variables at runtime. This becomes quite a problem when you build a single image for multiple environments or need to build images for on-premises deployments where you can't bake in the environment variables.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCouldNotAppendScript = errors.New("could not append script")
View Source
var ErrCouldNotAppendToIndex = errors.New("could not append to index")
View Source
var ErrCouldNotFindHead = errors.New("could not find <head> tag")
View Source
var ErrCouldNotMakeDir = errors.New("could not make dir")
View Source
var ErrCouldNotMarshalConfig = errors.New("could not marshal config")

injectWebEnv.InjectWindowVars

View Source
var ErrCouldNotOpenFile = errors.New("could not open file")
View Source
var ErrCouldNotParseIndex = errors.New("could not parse index")

injectWebEnv.appendToIndex

View Source
var ErrCouldNotParseNamespace = errors.New("namespace must match regex: ^[a-zA-Z_][a-zA-Z0-9_]*$")
View Source
var ErrCouldNotReadFile = errors.New("could not read file")
View Source
var ErrCouldNotWriteFile = errors.New("could not write file")
View Source
var ErrCouldNotWriteIndex = errors.New("could not write index")
View Source
var ErrNoIndexFound = errors.New("no index.html found")
View Source
var ErrNoNamespace = errors.New("no namespace provided")
View Source
var ErrUnexpectedWalkError = errors.New("unexpected walk error")

Functions

func CopyFileSys

func CopyFileSys(filesys fs.FS, onHook OnHookFunc) (*memfs.FS, error)

func InjectWebEnv

func InjectWebEnv(filesys fs.FS, conf any, ns string) (*memfs.FS, error)

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

func NewStaticFilesHandler(filesys fs.FS, fn ...staticFilesHandlerFunc) (http.Handler, error)

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

func WithInjectWebEnv(env any, namespace string) staticFilesHandlerFunc

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

func WithLogger(logger *slog.Logger) staticFilesHandlerFunc

WithLogger sets the logger for the static file server. Defaults to slog.Logger.

func WithMuxErrorHandler

func WithMuxErrorHandler(handler func(int) http.Handler) staticFilesHandlerFunc

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

type OnHookFunc func(path string, data []byte) ([]byte, error)

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)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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