This template includes all the scaffolding you need to get started on a Go HTTP Server running in NGINX Unit.
DO NOT FORK -- this template is meant to be used from the Use this template feature.
- Click on
Use this template - Give a name to your project
- Wait until the first run of CI finishes (GitHub Actions will process the template and commit to your new repo)
- Clone your new project and tweak any of the placeholders if necessary. Pay special attention to the README!
- Happy coding!
NOTE: WAIT until the first CI run on GitHub Actions finishes before cloning your new project.
This project is intended to serve three purposes:
- Provide a template for creating HTTP Servers in Go
- Provide a reference for how to create a Go project that can be compiled to WebAssembly;
- Provide a reference for how to create a Go project that can be compiled to WebAssembly and run in NGINX Unit;
This project requires the following tools be installed on your system:
- Go, version 1.21 or higher
Included is a asdf configuration file that will install the required version of Go if you have asdf installed, using the following command:
asdf install- WebAssembly compilation
- NGINX Unit configuration
- Prometheus metrics
Compilation to WebAssembly uses the WASI support in Go 1.21.
NOTE: The WASM support in Go is still experimental and may change in future versions of Go. Additionally, running WASM in NGINX Unit is currently only supported for C and Rust. As support for Go WASM in NGINX Unit improves, this project will be updated to reflect those changes.
The project includes a Prometheus metrics endpoint that can be used to monitor the health of the service.
This can be used as an example to add additional metrics to your project.
The Prometheus Go client library is used to implement the metrics endpoint.
When running, the metrics can be accessed at the following URL:
http://<hostname>:<port>/metrics- Run the tests
- Build the project
- Compile to WebAssembly
- Run the project in NGINX Unit
To run the tests, execute the following command:
go test ./...To build the project, execute the following command:
go build -o _build/http-server cmd/main/main.goTo compile the project to WebAssembly, execute the following command:
GOOS=js GOARCH=wasm go build -o _build/http-server.wasm cmd/main/main.goGOOS=wasip1 GOARCH=wasm go build -o _build/http-server.wasm cmd/main/main.goThe easiest way to run the project in NGINX Unit is to use Docker. The Dockerfile can be used to build an
image that can be used to run the project in NGINX Unit.
To build the image, execute the following command:
docker build -t bhs:dev .To run the image, execute the following command:
docker run -dit -p 8888:8888 --rm --name bhs_unit_dev bhs:devThrow some requests at the server:
curl http://localhost:8888
curl http://localhost:8888/metricsTo stop the container, execute the following command:
docker stop bhs_unit_devTo remove the image, execute the following command:
docker rmi bhs:devPlease see the [contributing guide](https://github.com/ciroque/buddaboom/blob/main/CONTRIBUTING.md) for guidelines on how to best contribute to this project.
© ciroque 2023