
This commit adds a complete Go implementation of the Todo application. The application uses Gin framework for the web server, GORM for database access, and SQLite for storage. Key features: - Todo CRUD operations with the same API endpoints - Health check endpoint - Database migrations - Tests for models, services, and API handlers - Documentation for the API - Configurable settings
162 lines
4.7 KiB
Markdown
162 lines
4.7 KiB
Markdown
# Go Todo App
|
|
|
|
A simple Todo application API built with Go, Gin, and SQLite. This is a Go rewrite of the original FastAPI Python application.
|
|
|
|
## Features
|
|
|
|
- RESTful API for managing todo items
|
|
- SQLite database for persistent storage
|
|
- CORS support for cross-origin requests
|
|
- Health check endpoint
|
|
- Pagination and filtering support
|
|
|
|
## Technology Stack
|
|
|
|
- [Go](https://golang.org/) - Programming language
|
|
- [Gin](https://github.com/gin-gonic/gin) - Web framework
|
|
- [GORM](https://gorm.io/) - ORM library
|
|
- [SQLite](https://www.sqlite.org/) - Database
|
|
- [Viper](https://github.com/spf13/viper) - Configuration management
|
|
|
|
## API Endpoints
|
|
|
|
| Method | URL | Description |
|
|
|--------|--------------------|-----------------------------|
|
|
| GET | /health | Health check |
|
|
| GET | /api/todos | List all todos |
|
|
| POST | /api/todos | Create a new todo |
|
|
| GET | /api/todos/:id | Get a specific todo |
|
|
| PUT | /api/todos/:id | Update a todo |
|
|
| DELETE | /api/todos/:id | Delete a todo |
|
|
|
|
## Query Parameters
|
|
|
|
The `GET /api/todos` endpoint supports the following query parameters:
|
|
|
|
- `skip` - Number of items to skip (default: 0)
|
|
- `limit` - Maximum number of items to return (default: 100)
|
|
- `completed` - Filter by completion status (boolean, optional)
|
|
|
|
## Prerequisites
|
|
|
|
- Go 1.18 or higher
|
|
- Git
|
|
|
|
## Getting Started
|
|
|
|
### Clone the repository
|
|
|
|
```bash
|
|
git clone https://github.com/yourusername/go-todo-app.git
|
|
cd go-todo-app
|
|
```
|
|
|
|
### Build and run the application
|
|
|
|
```bash
|
|
# Build the application
|
|
go build -o go-todo-app ./cmd/api
|
|
|
|
# Run the application
|
|
./go-todo-app
|
|
```
|
|
|
|
The API will be available at http://localhost:8000
|
|
|
|
### Development mode
|
|
|
|
```bash
|
|
go run cmd/api/main.go
|
|
```
|
|
|
|
## Configuration
|
|
|
|
The application can be configured using environment variables or a config file. The following settings are available:
|
|
|
|
| Environment Variable | Description | Default Value |
|
|
|----------------------|-------------------------------|---------------------------------------------|
|
|
| APP_NAME | Application name | Go Todo App |
|
|
| APP_DESCRIPTION | Application description | A simple Todo application API... |
|
|
| APP_VERSION | Application version | 0.1.0 |
|
|
| SERVER_PORT | HTTP server port | 8000 |
|
|
| DB_PATH | Database directory path | /app/storage/db |
|
|
| DB_NAME | Database file name | db.sqlite |
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
.
|
|
├── cmd/
|
|
│ └── api/
|
|
│ └── main.go # Application entry point
|
|
├── internal/
|
|
│ ├── config/ # Application configuration
|
|
│ │ └── config.go
|
|
│ ├── api/ # API routes and handlers
|
|
│ │ ├── api.go # API initialization
|
|
│ │ ├── middleware.go # Middleware functions
|
|
│ │ ├── todo.go # Todo route handlers
|
|
│ │ └── health.go # Health check route handler
|
|
│ ├── database/ # Database connection and initialization
|
|
│ │ └── db.go
|
|
│ ├── model/ # Database models (GORM)
|
|
│ │ └── todo.go
|
|
│ ├── dto/ # Data transfer objects
|
|
│ │ └── todo.go
|
|
│ └── service/ # Business logic
|
|
│ └── todo.go
|
|
├── migrations/ # Database migration files
|
|
├── storage/ # Storage directory
|
|
│ └── db/ # Database files location
|
|
├── go.mod # Go module file
|
|
├── go.sum # Go dependencies checksums
|
|
└── README.md # Project documentation
|
|
```
|
|
|
|
## Example Usage
|
|
|
|
### Create a todo
|
|
|
|
```bash
|
|
curl -X POST http://localhost:8000/api/todos \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"title": "Learn Go", "description": "Learn Go programming language", "completed": false}'
|
|
```
|
|
|
|
### Get all todos
|
|
|
|
```bash
|
|
curl http://localhost:8000/api/todos
|
|
```
|
|
|
|
### Get a specific todo
|
|
|
|
```bash
|
|
curl http://localhost:8000/api/todos/1
|
|
```
|
|
|
|
### Update a todo
|
|
|
|
```bash
|
|
curl -X PUT http://localhost:8000/api/todos/1 \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"completed": true}'
|
|
```
|
|
|
|
### Delete a todo
|
|
|
|
```bash
|
|
curl -X DELETE http://localhost:8000/api/todos/1
|
|
```
|
|
|
|
### Get completed todos
|
|
|
|
```bash
|
|
curl http://localhost:8000/api/todos?completed=true
|
|
```
|
|
|
|
### Health check
|
|
|
|
```bash
|
|
curl http://localhost:8000/health
|
|
``` |