This is a template/boilterplate for a REST API using Gin, Postgres, SQLC and Docker built with Go (meant to be used as a starting point for new projects).
- Basic authentication using Google OAuth2 (with classic cookie session)
- Pre built middlewares for CORS and authentication
- SQLC configured for type-safe SQL queries
- Dockerized development environment
- Fully dockerized production environment with multi-stage build for minimal image size, pgadmin for database management and managed migrations
- Clean file structure for the project
- Clone the repository
git clone https://github.com/Boolean-Autocrat/gin-api-template.git <project_name>
cd <project_name>
- Copy the
.env.example
file to.env
and update the values.
cp .env.example .env
- Spawn the postgres development database
make devdb
-
Update the database url in the
Makefile
as per your configuration. -
Run the migrations
make migrateup
- Run the server
air
- Visit
http://localhost:3000/health
orhttp://localhost:3000/example
in your browser to see the application.
make devdb
- Start the development databasemake devdbdown
- Stop the development databasemake migratecreate name=<migration_name>
- Create a new migrationmake migrateup
- Run the migrationsmake migratedown
- Rollback the migrations
health
- Health check endpointexample
- Example endpointauth/google/login
- Google OAuth2 login endpointauth/google/callback
- Google OAuth2 callback endpointauth/google/logout
- Google OAuth2 logout endpoint
SESSION_SECRET
- Secret key for session managementPOSTGRES_HOST
- Hostname for the postgres databasePOSTGRES_PORT
- Port for the postgres databasePOSTGRES_USER
- Username for the postgres databasePOSTGRES_PASSWORD
- Password for the postgres databasePOSTGRES_DB
- Database name for the postgres databaseDB_SOURCE
- Connection string for the postgres databasePORT
- Port for the gin serverGIN_MODE
- Mode for the gin server (debug/release)PGADMIN_DEFAULT_EMAIL
- Default email for pgadminPGADMIN_DEFAULT_PASSWORD
- Default password for pgadminGOOGLE_CLIENT_ID
- Google OAuth2 client idGOOGLE_CLIENT_SECRET
- Google OAuth2 client secretGOOGLE_REDIRECT_URL
- Google OAuth2 redirect urlLOGIN_REDIRECT_URL
- Redirect url after loginLOGOUT_REDIRECT_URL
- Redirect url after logoutCOOKIE_SET_URL
- Domain to set the cookie
│ .air.toml
│ .dockerignore
│ .env.example
│ .gitignore
│ docker-compose.dev.yml
│ docker-compose.yml
│ Dockerfile
│ go.mod
│ go.sum
│ main.go
│ Makefile
│ README.md
│ sqlc.yaml
│ start.sh
│ tree.txt
│ wait-for-it.sh
│
├───api
│ ├───auth
│ │ auth.go
│ │
│ ├───example
│ │ example.go
│ │
│ └───utils
│ utils.go
│
├───db
│ ├───migrations
│ │ 000001_init.down.sql
│ │ 000001_init.up.sql
│ │
│ ├───query
│ │ auth.sql
│ │
│ └───sqlc
│ auth.sql.go
│ db.go
│ models.go
│ postgres.go
│
├───middlewares
│ authMiddleware.go
│ corsMiddleware.go
│ verifySession.go