A real-time chat application built with Go, WebSockets, and a PostgreSQL backend.
- Real-time messaging via WebSockets
- User Authentication with JWT
- REST API for registration, login, and message history
- PostgreSQL Integration for data persistence
- Database Migrations managed by Atlas and GORM
- Hub Pattern for concurrent client management
- Configuration Management using environment variables
- Containerized development environment with Docker
Before you begin, ensure you have the following installed:
- Go (version 1.23 or newer)
- Docker and Docker Compose
- Task for running project tasks
- Atlas for database migrations
-
Clone the repository:
git clone https://github.com/codaxa/z-chat cd z-chat -
Configure your environment: Create a
.envfile in the root directory. You can use the following template, replacing the placeholder values with your local configuration.# PostgreSQL Settings DB_USER=admin DB_PASSWORD=your_secure_password DB_NAME=z-chat DB_HOST=localhost DB_PORT=5432 # For Docker Compose POSTGRES_USER=${DB_USER} POSTGRES_PASSWORD=${DB_PASSWORD} POSTGRES_DB=${DB_NAME} # JWT Settings JWT_SECRET=your_strong_jwt_secret
-
Start the database:
docker-compose up -d
-
Set up the database schema: This command will create the database, run migrations, and get everything ready.
task setup
-
Run the application:
task run
The server will start on
localhost:8080.
This project uses Taskfile as a command runner.
-
Run tests:
task test -
Run with hot-reloading (requires Air):
go install github.com/cosmtrek/air@latest task dev-air
-
Run code quality checks:
task lint
-
Create a new database migration: After making changes to GORM models in
internal/domain/models/, run:task migrate-new
-
See all available tasks:
task --list
cmd/chatserver/ # Application entry point
internal/
├── config/ # Configuration management
├── context/ # Context keys for request-scoped values
├── domain/ # Core domain models and repository interfaces
├── handlers/ # HTTP and WebSocket handlers
├── hub/ # WebSocket hub & client management
├── middleware/ # HTTP middleware (e.g., authentication)
├── services/ # Business logic (e.g., authentication service)
├── storage/ # Database implementation (PostgreSQL)
└── transport/http/ # HTTP routing and server setup
migrations/ # Database migration files
- go-chi/chi - HTTP router & middleware
- gorilla/websocket - WebSocket implementation
- pgx - PostgreSQL driver and toolkit
- golang-jwt/jwt - JWT implementation
- go-playground/validator - Struct validation
- GORM - ORM for schema generation
- Atlas - Database schema migration tool