A Go backend for a Telegram-style chat system.
Features:
- User signup & login (JWT, bcrypt)
- Direct one-to-one messages
- Group chat (create, send, fetch)
- Global broadcast messaging
gochat/
main.go # Entry point
internal/
api.go # HTTP handlers
auth.go # JWT middleware
user.go # User logic
chat.go # Messaging logic
model.go # Data models
redis.go # Redis connection
go.mod
README.md
-
Clone the repository
git clone https://github.com/asm2212/gochat.git cd gochat -
Start a Redis server
You must have Redis running on your local machine (localhost:6379).- You can download and run Redis for your operating system.
-
Install Go dependencies
go mod tidy
-
Run the server
go run .The server will start on port
:8080.
All endpoints except /signup and /login require JWT authentication.
Provide the token in the Authorization: Bearer <token> HTTP header.
- Endpoint:
POST /signup - Request Body:
{ "username": "your_username", "password": "your_password" } - Response:
201 Createdon success:{ "message": "user registered" }409 Conflictif user exists:{ "error": "username already exists" }
- Endpoint:
POST /login - Request Body:
{ "username": "your_username", "password": "your_password" } - Response:
200 OKon success:{ "token": "<JWT_TOKEN>" }401 Unauthorizedon failure:{ "error": "invalid credentials" }
- Endpoint:
POST /dm/send - Headers:
Authorization: Bearer <token> - Request Body:
{ "to": "recipient_username", "content": "Hello!" } - Response:
200 OK{ "message": "sent" }500 Internal Server Error{ "error": "..." }
- Endpoint:
GET /dm/history?user=recipient_username - Headers:
Authorization: Bearer <token> - Response:
200 OK[ { "id": "b7c9...", "from": "alice", "to": "bob", "content": "Hello Bob!", "type": "direct", "timestamp": "2025-07-06T05:00:00Z" }, ... ]500 Internal Server Error{ "error": "..." }
- Endpoint:
POST /group/create - Headers:
Authorization: Bearer <token> - Request Body:
{ "group": "group_name" } - Response:
201 Created{ "message": "group created" }500 Internal Server Error{ "error": "..." }
- Endpoint:
POST /group/send - Headers:
Authorization: Bearer <token> - Request Body:
{ "group": "group_name", "content": "Group message!" } - Response:
200 OK{ "message": "sent" }500 Internal Server Error{ "error": "..." }
- Endpoint:
GET /group/history?group=group_name - Headers:
Authorization: Bearer <token> - Response:
200 OK[ { "id": "a1b2...", "from": "alice", "group": "mygroup", "content": "Hi everyone", "type": "group", "timestamp": "2025-07-06T05:00:00Z" }, ... ]500 Internal Server Error{ "error": "..." }
- Endpoint:
POST /broadcast/send - Headers:
Authorization: Bearer <token> - Request Body:
{ "content": "Message to everyone!" } - Response:
200 OK{ "message": "broadcasted" }500 Internal Server Error{ "error": "..." }
- Endpoint:
GET /broadcast/history - Headers:
Authorization: Bearer <token> - Response:
200 OK[ { "id": "xxxx", "from": "alice", "content": "Hello all!", "type": "broadcast", "timestamp": "2025-07-06T05:00:00Z" }, ... ]500 Internal Server Error{ "error": "..." }