Idlemon
Idlemon is a networked tower defense game. This is the server side of the game.
Architecture
Idlemon includes an API, a WebSocket server, and a client made with Unity.
- The API exposes HTTP routes for user authentication.
- The client acts as the "frontend" of the game and is kept in a separate repository.
- The WebSocket server runs the game and handles the connected players.
Dependencies
Environment Variables
Idlemon uses several environment variables with default values. You can make a copy of .env.default and name it .env, this file will be loaded instead of .env.default if it is present. Note that environment variables will not be overwritten by the .env file if they are already set. Also note that .env files will not be loaded if ENV is set to production, you will need to set the env variables manually when deploying to a production environment.
ENV=development
set this to production when deploying.
API_PORT=3000
this is the port that the API will bind to.
SERVER_PORT=8080
this is the port that the WebSocket server will bind to.
DB_HOST=127.0.0.1
this is the IP address of the PostgreSQL database.
DB_NAME=postgres
this is the name of the database to connect to.
DB_USER=postgres
this is the username used to connect to PostgreSQL.
DB_PASS=password
this is the password used to connect to PostgreSQL.
REDIS_HOST=127.0.0.1
this is the IP address of the Redis server.
Docker
Idlemon includes a docker-compose.yml file and Dockerfiles, you can build and deploy the binaries manually or use Docker.
Dockerfile
Docker files are included to build Alpine images of the API and WebSocket server. You should provide your PostgreSQL password through the DB_PASS
environment variable. Both the API and Server need to access the same PostgreSQL and Redis server. Note that the ENV variable is set to production by default.
Build and run the API
docker build -t idlemon-api -f api.Dockerfile .
docker run -itd -e DB_PASS=password --restart=always --network=host --name idlemon-api idlemon-api
Build and run the WebSocket server
docker build -t server .
docker run -itd --env-file .env --restart=always --network=host --name server server
Authentication process
- User logs in through the API.
- API generates an authentication token and stores it in Redis using the user ID as the key. The token is set to expire after a certain time.
- The user ID and auth token are returned to the client.
- The client sends the user ID and auth token to the server through HTTP headers requesting to upgrade to a WebSocket connection.
- The server verifies that the user ID and auth token are in redis.
- Once verified the user is considered authenticated and the connection is upgraded to a WebSocket connection
VS Code
If you are using VS Code and have Go installed, you can press "ctrl + shift + b" to show the build tasks. They can help with running the binaries during development.
Documentation