This repository provides a production-ready starter template for building REST APIs in Go following a schema-first approach. It leverages code generation to create a robust and maintainable application structure.
- Go for the core application logic.
- OpenAPI-first development with
oapi-codegen
to generate server stubs and models from anopenapi.yaml
spec. - Type-safe database interaction with
sqlc
to generate Go code from raw SQL queries. - Clear project structure separating API definitions, business logic, database code, and configuration.
- Environment-based configuration using
envconfig
to read configuration from environment variables.
The application can be configured using the following environment variables:
Variable | Description | Default |
---|---|---|
PORT | The port to run the HTTP server on | 8080 |
HOST | The host to bind the HTTP server to | localhost |
To run the application with default settings:
go run cmd/main.go
To run the application with custom settings:
PORT=9000 HOST=0.0.0.0 go run cmd/main.go
The API provides the following endpoints:
GET /users
- List all usersPOST /users
- Create a new userGET /users/{id}
- Get a user by IDPUT /users/{id}
- Update a userDELETE /users/{id}
- Delete a user
For more details, see the OpenAPI specification in api/openapi.yml
.