This repository contains the source code for a Cards Against Humanity-like game server, implemented in Go. It features WebSocket-based multiplayer support and RESTful API endpoints to handle game logic and player interactions.
- WebSocket support for real-time communication.
- REST API for game management.
- Dynamic player management.
- Full game flow: starting a game, playing cards, and evaluating rounds.
- Hand management for each player.
- SQLite database for card storage.
- Simple CORS handling for development.
- Swagger documentation for the API.
- Go 1.23.2
- SQLite database with Cards Against Humanity cards.
- Docker (if using the containerised version).
-
Clone the repository:
git clone https://github.com/kapiw04/go-app.git cd go-app
-
Install dependencies:
go mod download
-
Make sure you have an SQLite database with the necessary card data. This project expects a
cah/cah_cards.db
file in the root directory with thecards
table populated. -
Run the server:
go run .
-
Swagger documentation will be available at http://localhost:8080/swagger/.
-
WebSocket connection can be established at
ws://localhost:8080/ws
, and the API is available at the listed routes.
You can build and run the project using Docker.
docker compose up
The game server will be available at http://localhost:8080
.
- POST
/start
: Start the game. - GET
/hand
: Retrieve a player's hand of cards. - GET
/black-card
: Get the current black card. - POST
/play-card
: Play a card by sending its index. - GET
/played-cards
: Retrieve all played cards.
For detailed API documentation, please visit the Swagger UI.
Players connect via WebSocket to the ws://localhost/ws
endpoint.
-
Host the Server: Start the server and find the host machine's local IP (e.g.,
192.168.x.x
). -
API and WebSocket Access: Players on the same network can:
- Make API requests to
http://<host-ip>:8080
for game interactions. - Connect to WebSocket at
ws://<host-ip>:8080/ws
for real-time gameplay.
- Make API requests to
A frontend will be developed later, so for now, players must interact directly via API calls and WebSocket clients.
For development, CORS is temporarily configured to allow all origins. Modify the corsMiddleware
function in main.go
to restrict origins in production.
Some basic error handling is included for incorrect card indices and invalid requests. Improve as needed for production.
Feel free to fork this repository, submit issues, or make pull requests. Contributions are always welcome.
This project is licensed under the MIT License. See the LICENSE
file for details.