Spring based score api for generic games. I use this for Spring and Kotlin learning. Requirement for this project is to be as simple as possible with as less code as possible. Final implementation most likely uses basic authentication for clients to post and get results which is security wise bad, but PR's could bring more security schemas like OAuth or something else.
- Spring
- Booted using https://start.spring.io/ with this configuration .
- Kotlin.
- PostgreSQL.
- User account is owned by a developer who manages games.
- Less we know about game clients the better. Find a way to get device specific unique identifier to populate clientId.
Sketch of planned database structure.
You need to have Postgresql server installed. This app needs it as data store.
Run standalone with ide having env var string
DATABASE_URL=localhost;DATABASE_USERNAME=someuser;DATABASE_PASSWORD=somepassword
or use docker via docker-compose up
.
- Create psql database called
game-scores
. - Rename
docker-compose.yml_tpl
todocker-compose.yml
- Set
environment
variable values:
- DATABASE_URL=
- DATABASE_USERNAME=
- DATABASE_PASSWORD=
- Run
docker-compose up
. - Open https://localhost:8080/.
Create and get games:
curl -X POST -H "Authorization: Basic <content>" -H "Content-Type: application/json" -d '{"name":"test game", "description":"example game", "userId": 1}' https://localhost:8080/games/game
curl -X GET -H "Authorization: Basic <content>" -H "Content-Type: application/json" https://localhost:8080/games/games\?gameId\=1
Create achievements for game:
curl -X POST -H "Authorization: Basic <content>" -H "Content-Type: application/json" -d '{"gameId":1, "name": "achievement name", "description":"example description"}' https://localhost:8080/achievements/achievementType
Get random generated uid for a client in case if unique identifiers are not provided by a device.
curl -X GET -H "Content-Type: application/json" https://localhost:8080/clients/generateUid
Post a new client, like device running game. Use this if unique uid is generated at a device side.
curl -X POST -H "Content-Type: application/json" -d '{"clientUid":"testclientuid", "nickname": "SomeNickName"}' https://localhost:8080/clients/client
Change client nickname:
curl -X PATCH -H "Content-Type: application/json" -d '{"clientUid":"testclientuid", "nickname": "SomeNickName2"}' https://localhost:8080/clients/nickname
Get scores, different ways:
curl -X GET -H "Content-Type: application/json" https://localhost:8080/scores/score\?gameId\=1\&clientUid\=testclientuid
curl -X GET -H "Content-Type: application/json" https://localhost:8080/scores/bestByGame\?gameId\=1
Post new score:
curl -X POST -H "Content-Type: application/json" -d '{"gameId":1, "clientUid": "testclientuid", "score": 100}' https://localhost:8080/scores/score
Post new achievement:
curl -X POST -H "Content-Type: application/json" -d '{"gameId":1, "clientUid": "testclientuid", "achievementTypeId": 1}' https://localhost:8080/achievements/achievement
Get achievements, different ways:
curl -X GET -H "Content-Type: application/json" https://localhost:8080/achievements/achievementsByGameId\?gameId\=1
curl -X GET -H "Content-Type: application/json" https://localhost:8080/achievements/achievementsByGameIdAndClientUid\?gameId\=1\&clientUid\=testclientuid
Create file docker-compose.yml
and paste in following contents:
version: '3'
services:
game-score-api:
container_name: game-score-api
image: ghcr.io/norkator/game-score-api:latest
ports:
- "8080:8080"
environment:
- DATABASE_URL=
- DATABASE_USERNAME=
- DATABASE_PASSWORD=
then run sudo docker-compose up --build
and you have pre-built container running.
rebuild with the latest changes (this deletes image):
sudo docker image ls
sudo docker image rm <IMAGE ID> -f
sudo docker-compose up --build
start as service docker-compose up -d --build
Notes and stuff for development.
- Implement skeleton structure.
- Create score controller.
- Create score model.
- Create score entity.
- Create score repository.
- Create score service.
- Learn migrations.
- Create score entity and other tables.
- Learn and create tests.
- Add a skeleton of actions builds pipeline.
- Add tests testing all controllers.
- Docker configurations.
- Create build pipeline.
- Github container registry image via actions.
- Craft fully functional game score api.
- Planning.
- Some simple authentication system.
- Planning /Basic auth?
- Multi-user basic auth.
- Documentations and drawings.
- Database structure drawing.
- Getting started.