Skip to content

djkapoor/learning-chi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Todo API with Go and Chi

This is a Todo API built with Go and the Chi router, demonstrating best practices for building web applications in Go. It includes authentication, middleware, context usage, and proper project structure.

Features

  • Clean and modular project structure
  • Chi router with middleware
  • Custom middleware (logging, CORS, error handling)
  • JWT-based authentication
  • Goroutines for background tasks
  • In-memory data store with repository pattern
  • Dependency injection pattern
  • Unit tests

Project Structure

├── cmd/
│   └── api/                  # Application entry point
├── internal/                 # Private application code
│   ├── handlers/             # HTTP request handlers
│   ├── middleware/           # Custom middleware
│   ├── models/               # Data models
│   ├── repository/           # Data access layer
│   └── services/             # Business logic
├── pkg/                      # Public libraries
│   └── auth/                 # Authentication utilities
└── configs/                  # Configuration files

Getting Started

Prerequisites

  • Go 1.16 or higher

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/todo-chi.git
    cd todo-chi
  2. Install dependencies:

    go mod tidy
  3. Run the application:

    go run cmd/api/main.go
  4. The API will be available at http://localhost:8080

API Endpoints

Authentication

  • POST /api/auth/register - Register a new user

    {
      "username": "user",
      "password": "password"
    }
  • POST /api/auth/login - Login and get a JWT token

    {
      "username": "user",
      "password": "password"
    }

Todos (Protected Routes - Require JWT Authentication)

  • GET /api/todos - Get all todos
  • GET /api/todos/{id} - Get a specific todo
  • POST /api/todos - Create a new todo
    {
      "title": "Todo Title",
      "description": "Todo Description"
    }
  • PUT /api/todos/{id} - Update a todo
    {
      "title": "Updated Title",
      "description": "Updated Description",
      "completed": true
    }
  • DELETE /api/todos/{id} - Delete a todo
  • PATCH /api/todos/{id}/toggle - Toggle todo completion status

Authentication

Authentication is handled with JWT tokens. To access protected routes:

  1. Register or login to get a token
  2. Include the token in the Authorization header for subsequent requests:
    Authorization: Bearer YOUR_TOKEN_HERE
    

Testing

Run tests with:

go test ./...

Key Concepts Demonstrated

Chi Router and Middleware

The application uses Chi for routing and middleware chains. Middleware is applied in both global and route-specific contexts.

Context Usage

See how request context is used to pass data between middleware and handlers, particularly for authentication.

Goroutines

Background tasks are handled with goroutines, demonstrating safe concurrent programming.

Clean Architecture

The project follows principles of clean architecture with clear separation between:

  • Handlers (presentation layer)
  • Services (business logic)
  • Repository (data access)

Dependency Injection

Components are designed with dependency injection principles for better testability.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published