Examples of using httprouter with
This repo is not intended for use as a "framework", however, other projects may import the packages in pkg
. The code in internal
is specific to this app, and must not be imported by other projects
Clone the repo (outside your GOPATH since this is a module)
git clone https://github.com/mozey/httprouter-util.git
cd httprouter-util # This is the APP_DIR
Following the 12 factor app recommendation to store config in the environment. Configuration is done using environment variables
Generate script to set dev config
APP_DIR=$(pwd) ./make.sh env_sh dev
Set dev config
source ./dev.sh
Run dev server (no live reload)
./make.sh app_run
Or run dev server with live reload
./make.sh app
Tests require running server
gotest -v ./...
Make requests from the cli with curlie
Token is required by default http://localhost:8118/token/is/required/by/default
Some routes may skip the token check
For static files http://localhost:8118/hello/foo?token=123
And API endpoints http://localhost:8118/api?token=123
http://localhost:8118/does/not/exist?token=123
Use http.MaxBytesReader to limit POST body. Make the request with specified body size, Assuming MaxBytes
is set to 1 KiB the request below will fail
dd if=/dev/urandom bs=1 count=1025 | curlie --data-binary @- POST "http://localhost:8118/api?token=123"
Settings to protect against malicious clients. NOTE The response body for errors below is not JSON, it's not possible to override string response hard-coded in Golang SDK
# ReadTimeout
gotest -v ./... -run TestReadTimeout
# WriteTimeout
gotest -v ./... -run TestWriteTimeout
# MaxHeaderBytes
gotest -v ./... -run TestMaxHeaderBytes
Caddy is used as a HTTPS endpoint, API gateway, and reverse proxy. See #6 for Caddyfile configuration
TODO Define services on the handler, e.g. DB connection http://localhost:8118/db?sql=select * from color
Example client with self-update feature.
Build the client, download it, and print version
source dev.sh
VERSION=0.1.0 ./scripts/build-client.sh
./dist/client -version
rm -f client && curlie "http://localhost:8118/client/download?token=123" -o client
# Executing the client might require permissions, on macOS
# System Preferences > Security & Privacy > General > Allow Anyway
chmod u+x ./client && ./client -version
Create a new build
VERSION=0.2.0 ./scripts/build-client.sh
./dist/client -version
curlie "http://localhost:8118/client/version?token=123"
Update from the server and print new version
./client -update -token 123
./client -version
Running update again prints "already on the latest version"
./client -update -token 123
Removes all user config
APP_DIR=$(pwd) ./scripts/reset.sh
This example aims for a good cross platform experience by depending on
On macOS and Linux
TODO On Windows
GNU Make
is not needed because Golang is fast to build,
and fswatch
can be used for live reload.
For this example main.go
is kept in the project root.
Larger projects might have separate bins in the "/cmd" dir
Bash on Windows is easy to setup using msys2, MinGW, or native shell on Windows 10. For other UNIX programs see gow
TODO Instructions for installing deps on Windows