A RESTful API for managing todos with user authentication built using Go and modern web technologies.
- Go - Main programming language
- Gin - Web framework
- GORM - ORM for database operations
- JWT - Authentication mechanism
- PostgreSQL - Database
- Swagger - API documentation
- bcrypt - Password hashing
├── cmd/ # Application entry points
├── internal/ # Private application code
│ ├── auth/ # Authentication logic
│ ├── config/ # Configuration management
│ ├── database/ # Database connections and migrations
│ ├── handlers/ # HTTP request handlers
│ ├── middleware/ # HTTP middleware components
│ ├── models/ # Database models
│ └── routes/ # Route definitions
├── docs/ # Swagger documentation
└── main.go # Main application entry point
-
Authentication
- JWT-based authentication
- Secure password hashing with bcrypt
- Protected routes with middleware
-
Database
- GORM for database operations
- PostgreSQL for data persistence
- Models for Users and Todos
-
API Endpoints
/api/auth/signup
- User registration/api/auth/login
- User authentication/api/todos
- Todo CRUD operations- Protected routes with JWT middleware
-
Documentation
- Swagger UI for API documentation
- Auto-generated API specs
- Go 1.19 or later
- PostgreSQL
- Clone the repository
git clone <repository-url>
cd todo-api
- Install dependencies
go mod download
- Set up environment variables (create a .env file)
JWT_SECRET=your_jwt_secret_key
DATABASE_URL=postgresql://username:password@localhost:5432/dbname?sslmode=disable
- Run the application
go run main.go
The server will start at http://localhost:8080
Access the Swagger documentation at:
http://localhost:8080/swagger/index.html
swag init
go test ./...
go build -o todo-api
# Run all tests
go test ./...
# Run tests with coverage
go test -cover ./...
# Run tests for a specific package
go test ./internal/handlers
go test ./internal/auth
go test ./internal/middleware
internal/test/test_helpers.go
: Common testing utilities and mock functionsinternal/auth/jwt_test.go
: JWT token generation and validation testsinternal/handlers/auth_handler_test.go
: Authentication endpoint testsinternal/handlers/todo_handler_test.go
: Todo CRUD operation testsinternal/middleware/auth_middleware_test.go
: Authentication middleware tests
The test suite covers:
- JWT token generation and validation
- User authentication (signup/login)
- Todo CRUD operations
- Authentication middleware
- Error handling and edge cases
POST /api/auth/signup
- Register a new userPOST /api/auth/login
- Login and receive JWT token
GET /api/todos
- List all todosPOST /api/todos
- Create a new todoGET /api/todos/:id
- Get a specific todoPUT /api/todos/:id
- Update a todoDELETE /api/todos/:id
- Delete a todo
- All passwords are hashed using bcrypt
- JWT tokens are required for protected endpoints
- Environment variables for sensitive data
- Input validation on all endpoints