Skip to content

putra4648/todo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Todo API

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.

πŸš€ Features

  • 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 sqlc for generating type-safe Go code from SQL queries.
  • Centralized Error Handling: Custom middleware for consistent JSON error responses.
  • Logging: Integrated request logging middleware.

πŸ› οΈ Tech Stack

πŸ“ Project Structure

β”œβ”€β”€ 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)

βš™οΈ Setup & Installation

1. Prerequisites

  • Go 1.21+
  • PostgreSQL
  • sqlc (optional, for regenerating queries)

2. Database Setup

Create a PostgreSQL database and apply the migrations. You can find the schema in the query/ or migrations/ directory.

3. Environment Variables

Create a .env file in the root directory:

JWT_SECRET=your_secret_key_here

4. Running the Application

go mod tidy
go run main.go

The server will start at http://localhost:8080.

πŸ›£οΈ API Endpoints

Authentication

Method Endpoint Description
POST /auth/register Register a new user
POST /auth/login Login and receive JWT (Cookie & JSON)

Todos (Requires Auth)

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

πŸ› οΈ Development

Generating Database Code

If you modify query/query.sql, regenerate the Go code using:

sqlc generate

πŸ“œ License

This project is open-source and available under the MIT License.

About

My todo list

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors