Extremely simple Go tool that serves files out of a given folder, using a file resolution algorithm similar to GitHub Pages, Netlify, or the default Nginx config. Provides a Go "library" (less than 100 LoC) and an optional CLI tool.
Useful when making a site that will be served by GitHub Pages or similar.
- Usable from Go code.
- Usable from a shell.
- Not overweight.
- Can serve files from
zip
archives without nesseserity of unpacking.
An almost drop-in replacement for http.FileServer
:
import "github.com/mitranim/srv"
http.ListenAndServe(":<some-port>", srv.FileServer("."))
For CLI usage, first install Go: https://golang.org. Then run this:
go install github.com/mitranim/srv/srv@latest
# Get help
srv -h
# Actually run
srv
This will compile the executable into $GOPATH/bin/srv
. Make sure $GOPATH/bin
is in your $PATH
so the shell can discover the srv
command. For example, my ~/.profile
contains this:
export GOPATH=~/go
export PATH=$PATH:$GOPATH/bin
Alternatively, you can run the executable using the full path. At the time of writing, ~/go
is the default $GOPATH
for Go installations. Some systems may have a different one.
~/go/bin/srv
- For directories including
/
, useindex.html
when available. - For "clean" links without an extension, try appending
.html
. - For "not found", try using
404.html
if one exists, falling back on a simple hardcoded message. - For
zip
archive download if no/
. Ifarch.zip/index.html
it serveshtml
and works as it regular folder.
I'm receptive to suggestions. If this library almost satisfies you but needs changes, open an issue or chat me up. Contacts: https://mitranim.com/#contacts