This is a little webserver implementation based on chi. The webserver is supposed to serve a folder containing e.g. a static website and is suited to serve a SPA.
The server package contains a collection of http.Handler implementations which may be reused in other projects. The filesystem package contains a readonly in-memory-filesystem implementation.
Logs are (without -pretty option) are provided in a GCP compatible JSON format.
The following middleware handler features are provided in the server package:
- Fallback: Handler that falls back on a configured default path when retrieving a specified set of status codes from the next handler. Very useful for serving a SPA.
- Headers: Static Headers can be easily configured.
- Caching: Support via ETag and If-None-Match HTTP-Headers
- Access-Log: Basic access-logging formatted in a GCP-compatible way.
- CspReplace and SessionCookie: See my blog about fixing Angular CSP regarding style-src.
You can use the ngergs/websrv docker container, the entrypoint is set to the websrv executable.
All releases starting with v1.4.3 have binary builds attached to them for linux, windows and osx.
Compile from source:
git clone https://github.com/ngergs/websrv
go build ./cmd/websrv
The path to the folder to be served has to be provided as command line argument.
Usage: ./websrv {options} [target-path]
Options:
-conf string
config file to load
There are a number of various optional settings configured via config files. The config options and documentation can be found in the config.yaml. There is also an example configuration.
All config settings can be also set via environment variables. Environment variables take precedence over config file settings. All env config vars start with WEBSRV
and follow with
the upper-cased config-setting name. You can use underscores to set nested values. To e.g. set the value of
log:
level: info
the following configuration variable can be used: WEBSRV_LOG_LEVEL=info
.