This repository is a basic template for creating APIs in Go. It includes support for:
- Docker: To create containers and manage dependencies.
- Nginx: Configured as a reverse proxy for the API.
- Gin: A web framework for building APIs in Go.
- GORM: An ORM library for interacting with the database.
- Insomnia: Pre-configured file with requests to facilitate testing.
- Initial setup for APIs in Go.
- Support for JWT authentication.
- Pre-configured CORS policies.
- Rate limiting for request control.
- PostgreSQL support (or other databases, depending on the setup).
-
Clone the repository:
git clone https://github.com/martimmpr/go-api-template.git cd go-api-template -
Set up the environment variables:
- On Linux:
cp .env.example .env
- On Windows (PowerShell):
Copy-Item .env.example .env
- On Linux:
-
Start the containers using Docker Compose:
docker compose up --build
-
Test the API endpoints using the
insomnia.jsonfile:- Import the file into Insomnia and execute the requests directly.
-
Access the API: The server will be available at
http://localhost:8080.
| Command | Action | Mode |
|---|---|---|
docker compose up |
Starts the containers and shows logs. | Foreground |
docker compose up -d |
Starts the containers without showing logs. | Background |
docker compose up --build |
Rebuilds images and starts the containers (showing logs). | Foreground |
docker compose up --build -d |
Rebuilds images and starts the containers (without showing logs). | Background |
docker compose down |
Stops and removes containers, networks, and anonymous volumes. | N/A |
docker compose down -v |
Stops and removes containers, networks, and all volumes. | N/A |
| Command | Description |
|---|---|
go mod init [FOLDER] |
Initializes the project and creates go.mod and go.sum. |
go mod tidy |
Updates go.mod with the used packages. |
The provided insomnia.json file contains a collection of pre-configured requests to test the API endpoints. To use it:
- Open Insomnia.
- Go to File > Import > From File.
- Select the
insomnia.jsonfile located in this repository. - Use the requests to quickly test the API.
- Go: For building the API.
- Gin: Web framework for building APIs.
- GORM: ORM for interacting with databases.
- Docker: For containerization and isolated development environments.
- Nginx: Reverse proxy for the API.
- Insomnia: For easy API testing.
- JWT: For secure authentication.
- PostgreSQL: Relational database (can be replaced).
Contributions, issues, and feature requests are welcome! Feel free to check the issues page.
This project is licensed under the MIT License - see the LICENSE file for details.