A robust and secure Todo List API built with Go, Gin, and PostgreSQL. This project follows clean architectural patterns, uses sqlc for type-safe database interactions, and implements JWT-based authentication with dual support for Cookies and Authorization headers.
- User Authentication: Secure Registration and Login using JWT.
- Dual Auth Support: Works with both
Authorization: Bearer <token>headers and HTTP-only Cookies. - User Isolation: Users can only create, view, update, and delete their own todos.
- Type-Safe Database: Uses
sqlcfor generating type-safe Go code from SQL queries. - Centralized Error Handling: Custom middleware for consistent JSON error responses.
- Logging: Integrated request logging middleware.
- Languange: Go (Golang)
- Web Framework: Gin Gonic
- Database: PostgreSQL
- ORM/Query Builder: sqlc
- Driver: pgx/v5
- Security: JWT (golang-jwt/v5) & bcrypt for password hashing.
βββ db/ # Generated sqlc code (DB interfaces and models)
βββ handlers/ # API route handlers (Auth and Todo logic)
βββ middleware/ # Gin middlewares (Auth, Log, Error)
βββ migrations/ # SQL migration files
βββ models/ # Data Transfer Objects (DTOs) for requests/responses
βββ query/ # Raw SQL queries used by sqlc
βββ utils/ # Helper functions (Hashing, etc.)
βββ main.go # Application entry point
βββ sqlc.yaml # sqlc configuration
βββ .env # Environment variables (not tracked)
- Go 1.21+
- PostgreSQL
- sqlc (optional, for regenerating queries)
Create a PostgreSQL database and apply the migrations. You can find the schema in the query/ or migrations/ directory.
Create a .env file in the root directory:
JWT_SECRET=your_secret_key_herego mod tidy
go run main.goThe server will start at http://localhost:8080.
| Method | Endpoint | Description |
|---|---|---|
| POST | /auth/register |
Register a new user |
| POST | /auth/login |
Login and receive JWT (Cookie & JSON) |
| Method | Endpoint | Description |
|---|---|---|
| GET | /todos |
List all todos for the logged-in user |
| POST | /todos |
Create a new todo |
| PUT | /todos/:id |
Update an existing todo |
| DELETE | /todos/:id |
Delete a todo |
If you modify query/query.sql, regenerate the Go code using:
sqlc generateThis project is open-source and available under the MIT License.