Skip to content

Latest commit

Β 

History

History
337 lines (268 loc) Β· 8.46 KB

File metadata and controls

337 lines (268 loc) Β· 8.46 KB

Chat API

A modern, scalable chat application API built with Go, featuring user management, group messaging, and real-time message tracking.

πŸš€ Features

  • User Management: Create, retrieve, and list users with email validation
  • Group Messaging: Create groups, manage members with role-based access (owner/admin/member)
  • Direct Messaging: Send private messages between users
  • Message Types: Support for text, image, and file messages
  • Read Receipts: Track message read status with seen receipts
  • File Support: Handle file uploads with metadata tracking
  • RESTful API: Clean, intuitive endpoints following REST conventions
  • Database Persistence: SQLite database with automatic migrations using GORM

πŸ—οΈ Architecture

This project follows a layered architecture pattern:

Routes β†’ Handlers β†’ Services β†’ Repositories β†’ Database

Layers

  • Routes: HTTP endpoint definitions and routing configuration
  • Handlers: Request/response handling and input validation
  • Services: Business logic and core application functionality
  • Repositories: Data access layer with CRUD operations
  • Models: Data structures and domain entities

πŸ› οΈ Tech Stack

  • Language: Go 1.22
  • Web Framework: Gin - High-performance HTTP web framework
  • ORM: GORM - Object-relational mapping for Go
  • Database: SQLite - Embedded SQL database engine

Dependencies

github.com/gin-gonic/gin v1.10.0     - Web framework
gorm.io/gorm v1.25.7                  - ORM framework
gorm.io/driver/sqlite v1.5.7          - SQLite driver for GORM

πŸ“‹ Project Structure

chat/
β”œβ”€β”€ main.go                           # Application entry point
β”œβ”€β”€ go.mod                            # Go module definition
β”œβ”€β”€ go.sum                            # Dependency checksums
β”œβ”€β”€ chat.db                           # SQLite database (auto-generated)
β”‚
β”œβ”€β”€ models/                           # Data models
β”‚   β”œβ”€β”€ user.go                       # User model
β”‚   β”œβ”€β”€ group.go                      # Group and GroupMember models
β”‚   └── message.go                    # Message, Seen, and File models
β”‚
β”œβ”€β”€ db/                               # Database initialization
β”‚   └── db.go                         # GORM setup and migrations
β”‚
β”œβ”€β”€ repository/                       # Data access layer
β”‚   β”œβ”€β”€ user_repository.go            # User CRUD operations
β”‚   β”œβ”€β”€ group_repository.go           # Group CRUD operations
β”‚   └── message_repository.go         # Message CRUD operations
β”‚
β”œβ”€β”€ service/                          # Business logic layer
β”‚   β”œβ”€β”€ user_service.go               # User business logic
β”‚   β”œβ”€β”€ group_service.go              # Group business logic
β”‚   └── message_service.go            # Message business logic
β”‚
β”œβ”€β”€ handler/                          # HTTP request handlers
β”‚   β”œβ”€β”€ user_handler.go               # User endpoints
β”‚   β”œβ”€β”€ group_handler.go              # Group endpoints
β”‚   └── message_handler.go            # Message endpoints
β”‚
β”œβ”€β”€ routes/                           # Route configuration
β”‚   └── routes.go                     # Route setup
β”‚
β”œβ”€β”€ postman_collection.json           # Postman API collection
└── README.md                         # This file

πŸ“¦ Installation

Prerequisites

  • Go 1.22 or higher
  • Git

Setup

  1. Clone the repository (if applicable):

    git clone <repository-url>
    cd chat
  2. Install dependencies:

    go mod download
  3. Build the application:

    go build

πŸš€ Running the Application

Start the Server

go run main.go

The API server will start on http://localhost:8080

You should see output like:

Database initialized and migrations completed successfully
[GIN-debug] Loaded HTML Templates (0): 
[GIN-debug] Registered 14 routes

Health Check

Verify the server is running:

curl http://localhost:8080/health

Response:

{"status": "ok"}

πŸ“‘ API Endpoints

Base URL: http://localhost:8080

For detailed request/response examples and to test endpoints interactively, use the included Postman collection (postman_collection.json).

User Endpoints

  • POST /users - Create a new user
  • GET /users - Get all users
  • GET /users/:id - Get user by ID

Group Endpoints

  • POST /groups - Create a new group
  • DELETE /groups/:id - Delete a group
  • POST /groups/:id/members - Add member to group
  • DELETE /groups/:id/members/:user_id - Remove member from group
  • GET /groups/:id/members - Get group members

Message Endpoints

  • POST /messages - Send a message (direct or group)
  • GET /messages - Get conversation history
  • POST /messages/:id/seen - Mark message as seen
  • DELETE /messages/:id - Delete a message
  • GET /messages/unseen/:user_id - Get unseen message count

πŸ—„οΈ Database Schema

The application uses SQLite with the following tables:

Users

users (
  id INTEGER PRIMARY KEY,
  username TEXT UNIQUE NOT NULL,
  email TEXT UNIQUE NOT NULL,
  created_at TIMESTAMP,
  updated_at TIMESTAMP,
  deleted_at TIMESTAMP
)

Groups

groups (
  id INTEGER PRIMARY KEY,
  name TEXT UNIQUE NOT NULL,
  created_at TIMESTAMP,
  updated_at TIMESTAMP,
  deleted_at TIMESTAMP
)

GroupMembers

group_members (
  group_id INTEGER PRIMARY KEY,
  user_id INTEGER PRIMARY KEY,
  role TEXT DEFAULT 'member',
  created_at TIMESTAMP,
  updated_at TIMESTAMP
)

Messages

messages (
  id INTEGER PRIMARY KEY,
  sender_id INTEGER NOT NULL,
  receiver_id INTEGER,
  group_id INTEGER,
  type TEXT NOT NULL,
  content TEXT,
  file_id INTEGER,
  created_at TIMESTAMP,
  updated_at TIMESTAMP,
  deleted_at TIMESTAMP
)

Seen (Read Receipts)

seens (
  message_id INTEGER PRIMARY KEY,
  user_id INTEGER PRIMARY KEY,
  seen_at TIMESTAMP
)

Files

files (
  id INTEGER PRIMARY KEY,
  url TEXT NOT NULL,
  size INTEGER,
  type TEXT,
  created_at TIMESTAMP,
  updated_at TIMESTAMP,
  deleted_at TIMESTAMP
)

πŸ§ͺ Testing

Run all tests:

go test ./...

Run tests with verbose output:

go test ./... -v

Run tests with coverage:

go test ./... -cover

Generate coverage report:

go test ./... -coverprofile=coverage.out
go tool cover -html=coverage.out

πŸ” Error Handling

The API returns standard HTTP status codes:

  • 200 OK - Request succeeded
  • 201 Created - Resource created successfully
  • 204 No Content - Resource deleted successfully
  • 400 Bad Request - Invalid request parameters
  • 404 Not Found - Resource not found
  • 500 Internal Server Error - Server-side error

πŸš€ Using Postman

A Postman collection is included (postman_collection.json). To use it:

  1. Open Postman
  2. Click Import β†’ Upload Files
  3. Select postman_collection.json
  4. Select the collection and start testing endpoints

πŸ“ Development Guidelines

Adding New Features

  1. Define Models in models/
  2. Create Repository in repository/ for database operations
  3. Implement Service in service/ for business logic
  4. Build Handler in handler/ for HTTP endpoints
  5. Register Routes in routes/routes.go
  6. Write Tests alongside implementation

Code Style

  • Follow Go conventions (Effective Go)
  • Use meaningful variable and function names
  • Add comments for exported functions
  • Keep functions small and focused

πŸ› Troubleshooting

Database Lock Error

If you encounter "database is locked":

  • Ensure only one instance of the app is running
  • Delete chat.db and restart the application

Port Already in Use

If port 8080 is already in use:

  • Edit main.go line 34 to use a different port: r.Run(":8081")
  • Restart the application

Dependency Issues

Resolve dependency issues with:

go mod tidy
go mod download

πŸ“„ License

This project is provided as-is for educational and development purposes.

πŸ‘₯ Contributing

Contributions are welcome! Please ensure:

  • Code follows Go conventions
  • Tests are included for new features
  • Commit messages are clear and descriptive

πŸ“ž Support

For issues or questions, please check:

  1. The API endpoints documentation above
  2. The Postman collection for example requests
  3. Application logs for error details