Skip to content

Agarwalsahil/TODOAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“ TODOAPI – RESTful ToDo API in Go

Go CI

A secure and efficient TODO list REST API built with Go, using Gin, GORM, SQLite, and JWT authentication. Built as a learning project following the Todo List API challenge.


πŸš€ Deployed API

Live URL:
➑️ https://todoapi-nd4n.onrender.com


πŸš€ Features

  • βœ… User registration and login with hashed passwords.
  • πŸ” JWT-based authentication middleware.
  • πŸ“ CRUD operations for Todo items.
  • πŸ” Pagination and filtering on todo list.
  • πŸ›‘οΈ Per-user scoped data access.
  • πŸ’Ύ SQLite database (simple and lightweight).
  • πŸ§ͺ Unit-tested endpoints with GitHub Actions CI
  • 🧱 Clean project structure with modular code.

πŸ“ Folder Structure

TODOAPI/
β”œβ”€β”€ controllers/        # Request handlers
β”œβ”€β”€ db/                 # DB initialization
β”œβ”€β”€ middleware/         # JWT auth middleware
β”œβ”€β”€ models/             # DB models
β”œβ”€β”€ utils/              # Shared helper functions
β”œβ”€β”€ .gitignore
β”œβ”€β”€ go.mod
β”œβ”€β”€ go.sum
β”œβ”€β”€ LICENSE
β”œβ”€β”€ main.go
└── README.md

πŸ“¦ API Endpoints

Method Endpoint Description Auth Required
POST /register Register a new user ❌
POST /login Authenticate user & get JWT ❌
GET /todos Get all todos (paginated) βœ…
POST /todos Create a new todo βœ…
PUT /todos/:id Update a todo βœ…
DELETE /todos/:id Delete a todo βœ…

πŸ”§ Setup Instructions

1. πŸ“¦ Clone the Repository

git clone https://github.com/Agarwalsahil/TODOAPI.git
cd TODOAPI

2. βœ… Set Env Variable

Create a .env file or set manually:

JWT_SECRET=your-secret-key

3. βœ… Install Dependencies

go mod tidy

4. ▢️ Run the API

go run main.go

Server will run at http://localhost:8080


πŸ› οΈ API Endpoints

πŸ” Authentication

🧾 Register

POST /register

Body:

{
  "name": "your name",
  "email": "user@example.com",
  "password": "securepassword"
}

πŸ”‘ Login

POST /login

Body:

{
  "name": "your name",
  "email": "user@example.com",
  "password": "securepassword"
}

Returns:

{
  "token": "<jwt_token>"
}

πŸ“‹ Todo Operations (πŸ” Require JWT in Authorization header)

Include this header for all routes:

Authorization: Bearer <jwt_token>

βž• Create Todo

POST /todos

Body:

{
  "title": "Learn Go",
  "description": "Finish GORM chapter"
}

πŸ“„ List Todos with Pagination & Filtering

GET /todos?page=1&limit=5

Defaults: page=1, limit=10

✏️ Update Todo

PUT /todos/:id

Body:

{
  "title": "Updated title",
  "description": "Updated description"
}

πŸ—‘οΈ Delete Todo

DELETE /todos/:id

πŸ“¦ Example Curl Usage

Register:

curl -X POST http://localhost:8080/register -H "Content-Type: application/json" -d '{"name": "your name", "email": "user@example.com", "password": "pass"}'

Login:

curl -X POST http://localhost:8080/login -H "Content-Type: application/json" -d '{"name": "your name", "email": "user@example.com", "password": "pass"}'

Create Todo:

curl -X POST http://localhost:8080/todos -H "Authorization: Bearer <token>" -H "Content-Type: application/json" -d '{"title": "New Task", "description": "Do it now"}'

πŸ’‘ If you want to test the deployed version, replace http://localhost:8080 with:
πŸ”— https://todoapi-nd4n.onrender.com


🧾 Technologies Used

  • Go – Programming language
  • Gin – HTTP web framework
  • GORM – ORM for database
  • SQLite – Lightweight database
  • JWT – Token-based authentication
  • BCrypt – Password hashing

πŸ›‘οΈ Security Notes

  • Passwords are hashed using bcrypt before storing.
  • JWT secret should be kept in a .env file (not committed).
  • Authorization middleware protects all todo routes.

πŸ“ƒ License

This project is licensed under the MIT License.


πŸ™Œ Contributions

Open to suggestions, bug reports, and pull requests!


Made with ❀️ by Sahil Agarwal

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages