Skip to content

victor-code19/todo-golang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

17 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“ 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"
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published