Lightweight real-time chat application built in Go (Chi + WebSockets) with a SvelteKit frontend embedded into the Go binary. SQLite + migrations via golang-migrate, SQL code generated with sqlc, structured logging, Docker multi-stage build.
embed.FS
) – single self-contained binarysqlc
slog
cmd/real-time-chat/main.go # Application entry point
internal/
database/ # DB init + migrations runner
migrations/ # SQL migration files
queries/ # SQL source for sqlc
dto/ # DTO definitions (e.g. UserDTO)
frontend/ # Embedded frontend build & source
embed.go # go:embed directive
olha-mensagem-app/ # SvelteKit app (src + build)
handlers/ # HTTP user endpoints
logger/ # Logger interface + slog impl
repository/ # Generated sqlc code (models, queries)
server/ # HTTP server + routes + static serving
websocket/ # Hub, client, message & WS handler
.air.toml # Air hot-reload config
docker-compose.yml
Dockerfile
sqlc.yml
Base path: /api
Method | Path | Description | Request Body |
---|---|---|---|
POST | /api/users | Register new user | { "username": "...", "password": "..." } |
POST | /api/users/login | Login existing user | { "username": "...", "password": "..." } |
Successful responses:
{ "id": 1, "username": "alice" }
Path: /api/ws/{userId}
(must be a valid registered user ID)
Outgoing broadcast message shape:
{
"type": "Chat",
"userId": 1,
"username": "alice",
"content": "hello world",
"timestamp": "2025-08-28T12:34:56Z",
"color": "#FF6B6B"
}
Client sends plain text frames; server wraps them into structured JSON.
id
NOTE: Not production-ready. Add JWT / sessions + CSRF + rate limiting for real deployments.
Name | Default (Docker image) | Purpose |
---|---|---|
PORT |
8080 |
HTTP listen port |
DB_NAME |
/app/data/olha_mensagem.db |
SQLite database file |
DB_MIGRATIONS_PATH |
/app/internal/database/migrations |
Migrations directory |
Local dev example (optional .env
):
PORT=8080
DB_NAME=./olha_mensagem.db
DB_MIGRATIONS_PATH=./internal/database/migrations
go mod download
go run ./cmd/real-time-chat
Hot reload with Air:
go install github.com/cosmtrek/air@latest
air
cd internal/frontend/olha-mensagem-app
npm install # or bun install
npm run dev # local dev server
Build (assets consumed by Go embed):
npm run build
Rebuild Go binary afterwards to embed updated assets.
go test ./...
Current coverage targets:
Edit queries under internal/database/queries/
. Then regenerate:
sqlc generate
Build image:
docker build -t olha-mensagem-app .
Run with Compose (includes persistent volume):
docker compose up -d
Visit: http://localhost:8080
embed.FS
# Run backend (dev)
air
# Build backend binary
go build -o chat ./cmd/real-time-chat && ./chat
# Run tests
go test -v ./...
# Regenerate SQL code
sqlc generate
# Docker build & run
docker build -t olha-mensagem-app .
docker run -p 8080:8080 olha-mensagem-app
MIT – see LICENSE.
Educational / demo project.