Skip to content

Latest commit

 

History

History
241 lines (193 loc) · 6.77 KB

File metadata and controls

241 lines (193 loc) · 6.77 KB

📝 Todo App - Go REST API

Go Gin MongoDB Docker HTML5 CSS3 JavaScript

A basic Todo application built with Go, Gin framework, and MongoDB featuring both REST API and web interface

✨ Features

🔧 Backend Features

  • RESTful API - Clean and organized REST endpoints
  • Official MongoDB Driver Integration - Persistent data storage with efficient queries
  • Gin Framework - Fast HTTP web framework with middleware support
  • JSON API - Standard JSON responses for all endpoints
  • Error Handling - Error handling and validation
  • Context Management - Proper timeout handling for database operations

🎨 Frontend Features

  • Interactive UI - Dynamic web interface with real-time updates
  • Responsive Design - Mobile-friendly responsive layout
  • AJAX Operations - Smooth user experience without page reloads
  • Task Management - Add, delete, and view tasks seamlessly
  • Visual Feedback - Intuitive user interface with Font Awesome icons

🏗️ Project Structure

todo-golang/
├── 📁 controllers/         # Business logic and request handlers
│   ├── task.go             # Task controller with CRUD operations
│   └── task_test.go        # Controller unit tests
├── 📁 models/              # Data models and structures
│   ├── task.go             # Task and ViewTask model definitions
│   └── task_test.go        # Model unit tests
├── 📁 public/              # Static assets
│   ├── 📁 css/
│   │   └── style.css       # Application styles
│   ├── 📁 img/
│   │   └── *.ico           # Favicon and images
│   └── 📁 js/
│       └── index.js        # Frontend JavaScript logic
├── 📁 templates/           # HTML templates
│   └── index.gohtml        # Main application template
├── 📄 main.go              # Application entry point and server setup
├── 📄 go.mod               # Go module dependencies
├── 📄 go.sum               # Go module checksums
├── 📄 integration_test.go  # Integration tests
├── 📄 Dockerfile           # Docker container configuration
├── 📄 docker-compose.yml   # Docker Compose setup
├── 📄 Makefile             # Build automation
├── 📄 .air.toml            # Live reload configuration
├── 📄 .env                 # Environment variables
└── 📄 init-mongo.js        # MongoDB initialization script

🚀 Quick Start

Prerequisites

Make sure you have the following installed on your system:

Installation

  1. Clone the repository

    git clone <your-repository-url>
    cd todo-golang
  2. Start the application with Docker Compose

    docker-compose up --build
  3. Access the application

Docker Commands

# Build and start all services
docker-compose up --build

# Run in background (detached mode)
docker-compose up -d

# Stop all services
docker-compose down

# View logs
docker-compose logs -f

# Rebuild and restart
docker-compose down && docker-compose up --build

📖 API Documentation

Base URL

http://localhost:8080/api

Endpoints

Method Endpoint Description Request Body Response
GET /tasks Retrieve all tasks - Array of tasks
POST /task Create a new task {"description": "string"} Created task object
DELETE /task/:id Delete specific task - Success message
DELETE /tasks Delete all tasks - Success message with count

Web Interface

Method Endpoint Description
GET /view/tasks Display tasks in HTML template

Example API Usage

Create a Task

curl -X POST http://localhost:8080/api/task \
  -H "Content-Type: application/json" \
  -d '{"description": "Learn Go programming"}'

Response:

{
  "id": "507f1f77bcf86cd799439011",
  "description": "Learn Go programming"
}

Get All Tasks

curl http://localhost:8080/api/tasks

Response:

[
  {
    "id": "507f1f77bcf86cd799439011",
    "description": "Learn Go programming"
  },
  {
    "id": "507f1f77bcf86cd799439012", 
    "description": "Build a REST API"
  }
]

Delete a Task

curl -X DELETE http://localhost:8080/api/task/507f1f77bcf86cd799439011

Response:

{
  "message": "Task deleted successfully"
}

Delete All Tasks

curl -X DELETE http://localhost:8080/api/tasks

Response:

{
  "message": "All tasks deleted successfully",
  "deletedCount": 5
}

🧪 Testing

Run Unit Tests

go test ./...

Run Integration Tests

go test -tags=integration

Test Coverage

go test -cover ./...

Using the Web Interface

  1. Navigate to http://localhost:8080/view/tasks
  2. Add new tasks using the input field
  3. Delete individual tasks using the trash icon
  4. Clear all tasks using the "Clear all" button

🛠️ Technology Stack

Backend

  • Go (v1.25) - Modern programming language
  • Gin - HTTP web framework
  • MongoDB - NoSQL database
  • MongoDB Go Driver (v2.3.0) - Official MongoDB driver

Frontend

  • HTML5 - Semantic markup with Go templates
  • CSS3 - Modern styling with Flexbox
  • Vanilla JavaScript - Interactive functionality with Fetch API

Development Tools

  • Air - Live reload for Go apps
  • Docker - Containerization
  • Make - Build automation

🔧 Configuration

Environment Variables

  • MONGODB_URI - MongoDB connection string (default: detected from environment)

Database Schema

{
  "_id": "ObjectId",
  "description": "string"
}