Golang websrv

(Source code)

A small webserver with the purpose of serving static websites and single page applications (SPA).This is just a toy experiment which resulted from playing around with golang.

Usually nginx is used to serve static websites. Nginx is a mixture of a reverse proxy and a static file server.While generally easy to set up I wanted a setup that just fulfils the rather simple task of serving static files and nothing on top.Go is especially interesting for such a project as it combines relative simplicity with decent performance and a small memory footprint.The executable ends up around 11MB and the distroless based compressed Docker image ends up around 5MB.

Implementation details

The implementation itself uses the Go standard library and chi to web serving purposes.Further important dependencies are:

The main functionalities are:

  • The filesystem package provides a very basic readonly in-memory-fileystem (implements fs.ReadFileFS).
  • The server package contains a collection of http.Handler implementations which may also be useful for other setups.
    • Headers: Static Headers can be easily configured.
    • Caching: Support via ETag and If-None-Match HTTP-Headers
    • Access-Log: Basic access-logging in GCP-compatible format.
    • CspReplace: See this article about fixing Angular CSP regarding style-src.