A starter kit for building a web API in Go using Gin, with Docker, configuration, dependency injection, JWT authentication, PostgreSQL support, and user authentication out of the box.
- Gin web framework
- PostgreSQL integration (with migration example)
- Environment variable/config management
- JWT authentication (registration, login, protected routes)
- Docker support (build and run with ease)
- Example handler/service structure
- Clean architecture: separation of handlers, services, repositories
- Ready for production: .env config, error handling, extensible modules
- Go 1.21+ (How to install Go)
- Docker
- PostgreSQL
git clone https://github.com/asm2212/go-gin-starter.git
cd go-gin-starter
Copy and edit the .env
file:
cp .env.example .env
# Edit .env to match your local or Docker PostgreSQL settings and set a JWT_SECRET
Example:
APP_PORT=8080
APP_ENV=development
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=postgres
DB_NAME=starterdb
JWT_SECRET=your_super_secret_key
Make sure PostgreSQL is running and a database named starterdb
exists. To create with Docker:
docker run --name go-starter-postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=starterdb -p 5432:5432 -d postgres
Run the migration SQL:
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
username VARCHAR(64) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP NOT NULL
);
go mod tidy
go run ./cmd/main.go
docker build -t go-gin-starter .
docker run --env-file .env -p 8080:8080 go-gin-starter
Or use Docker Compose (recommended for public use, see below).
Best for reproducible, containerized deployments.
- Create a
docker-compose.yml
(or use the provided one if exists):
version: "3.8"
services:
db:
image: postgres:16
restart: always
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: starterdb
ports:
- "5432:5432"
volumes:
- db_data:/var/lib/postgresql/data
api:
build: .
ports:
- "8080:8080"
env_file:
- .env
depends_on:
- db
volumes:
db_data:
- Launch both services:
docker-compose up --build
POST /register
Content-Type: application/json
{
"username": "youruser",
"password": "yourpass"
}
POST /login
Content-Type: application/json
{
"username": "youruser",
"password": "yourpass"
}
Response: { "token": "<jwt>" }
GET /api/profile
Authorization: Bearer <jwt>
- Add new models in
internal/model/
- Add repositories in
internal/db/
- Add business logic in
internal/service/
- Add new endpoints in
internal/api/
- Add middleware in
internal/middleware/
- Update your database schema and migrations as needed
- Fork the repo
- Create your feature branch (
git checkout -b feature/my-feature
) - Commit your changes
- Push to the branch
- Open a Pull Request
MIT