A modern, scalable chat application API built with Go, featuring user management, group messaging, and real-time message tracking.
- 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
This project follows a layered architecture pattern:
Routes β Handlers β Services β Repositories β Database
- 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
- 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
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
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
- Go 1.22 or higher
- Git
-
Clone the repository (if applicable):
git clone <repository-url> cd chat
-
Install dependencies:
go mod download
-
Build the application:
go build
go run main.goThe 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
Verify the server is running:
curl http://localhost:8080/healthResponse:
{"status": "ok"}Base URL: http://localhost:8080
For detailed request/response examples and to test endpoints interactively, use the included Postman collection (postman_collection.json).
POST /users- Create a new userGET /users- Get all usersGET /users/:id- Get user by ID
POST /groups- Create a new groupDELETE /groups/:id- Delete a groupPOST /groups/:id/members- Add member to groupDELETE /groups/:id/members/:user_id- Remove member from groupGET /groups/:id/members- Get group members
POST /messages- Send a message (direct or group)GET /messages- Get conversation historyPOST /messages/:id/seen- Mark message as seenDELETE /messages/:id- Delete a messageGET /messages/unseen/:user_id- Get unseen message count
The application uses SQLite with the following tables:
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 (
id INTEGER PRIMARY KEY,
name TEXT UNIQUE NOT NULL,
created_at TIMESTAMP,
updated_at TIMESTAMP,
deleted_at TIMESTAMP
)group_members (
group_id INTEGER PRIMARY KEY,
user_id INTEGER PRIMARY KEY,
role TEXT DEFAULT 'member',
created_at TIMESTAMP,
updated_at TIMESTAMP
)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
)seens (
message_id INTEGER PRIMARY KEY,
user_id INTEGER PRIMARY KEY,
seen_at TIMESTAMP
)files (
id INTEGER PRIMARY KEY,
url TEXT NOT NULL,
size INTEGER,
type TEXT,
created_at TIMESTAMP,
updated_at TIMESTAMP,
deleted_at TIMESTAMP
)Run all tests:
go test ./...Run tests with verbose output:
go test ./... -vRun tests with coverage:
go test ./... -coverGenerate coverage report:
go test ./... -coverprofile=coverage.out
go tool cover -html=coverage.outThe API returns standard HTTP status codes:
200 OK- Request succeeded201 Created- Resource created successfully204 No Content- Resource deleted successfully400 Bad Request- Invalid request parameters404 Not Found- Resource not found500 Internal Server Error- Server-side error
A Postman collection is included (postman_collection.json). To use it:
- Open Postman
- Click Import β Upload Files
- Select
postman_collection.json - Select the collection and start testing endpoints
- Define Models in
models/ - Create Repository in
repository/for database operations - Implement Service in
service/for business logic - Build Handler in
handler/for HTTP endpoints - Register Routes in
routes/routes.go - Write Tests alongside implementation
- Follow Go conventions (Effective Go)
- Use meaningful variable and function names
- Add comments for exported functions
- Keep functions small and focused
If you encounter "database is locked":
- Ensure only one instance of the app is running
- Delete
chat.dband restart the application
If port 8080 is already in use:
- Edit
main.goline 34 to use a different port:r.Run(":8081") - Restart the application
Resolve dependency issues with:
go mod tidy
go mod downloadThis project is provided as-is for educational and development purposes.
Contributions are welcome! Please ensure:
- Code follows Go conventions
- Tests are included for new features
- Commit messages are clear and descriptive
For issues or questions, please check:
- The API endpoints documentation above
- The Postman collection for example requests
- Application logs for error details