NodeJS/Express/Mongoose leaderboard backend for OispaHalla
Install dependencies with yarn install
and run the server with yarn dev
. If you want, use yarn dev:debug
to run the server with debugging enabled on port 1234
.
Alternatively build the project with yarn build
and run the compiled code with yarn start
.
Please run yarn format
before committing.
The following environment variables can be used to configure the server:
ADMIN_TOKEN
- Token used to authenticate admin requestsMONGO_URI
- URI to MongoDB database, if left blank the server will create an in-memory database with mongodb-memory-serverPAPERTRAIL_SERVER
andPAPERTRAIL_PORT
- If set, logs will be sent to PapertrailPORT
- Defaults to5000
This project uses dotenv to load environment variables from a .env
file at the root of the project.
GET - /alive
Returns a 200 status code if the server is alive.
Every request to the /scores
route is in the format of /scores/size/(size)
where the size is the board size. The size can be either 3
or 4
by default (defined in /src/routes/scores.ts
).
GET - /scores/size/(size)/
Returns all scores for the given size.
GET - /scores/size/(size)/(maxnum)
- GET
Returns the top maxnum
scores for the given size.
GET - /scores/size/(size)/count
Returns the number of scores for the given size.
GET/POST - /scores/size/(size)/token/(token?)
Returns the score for the given size and Firebase token. The token should be in the body for a POST request, or in the query for a GET request.
GET/POST - /scores/size/(size)/fetchboard/(maxnum)/(token?)
Returns the top maxnum
scores for the given size, the score for the Firebase token, and "rivals" of the user. The token should be in the body for a POST request, or in the query for a GET request. The POST request should be in the format of:
{
"token": "(token?)",
"rankplus": (rankplus?), // represents the number of ranks above the user's rank to include (=worse scores)
"rankminus": (rankminus?) // represents the number of ranks below the user's rank to include (= better scores)
}
POST - /scores/size/(size)/
Adds a new score to the database. The request should be in the format of:
{
"user": {
"screnName": "(screenName)", // can be used to update the user's current name
"token": "(token)"
},
"score": (score),
"breaks": (breaks), // currently not validated since the frontend doesn't keep track of breaks, so the leaderboard uses the one returned by HAC
"history": "(history)"
}
A successful request should return a 201 status code and the following body:
{
"message": "Score created successfully",
"createdScore": (the score that has been created, including the user and hash information)
"nameChanged": (true if the user's name has been updated, false otherwise)
}
All admin requests should have the query parameter token
set to an admin token e.g. /admin/user/name/jukkapekka?token=abcd
.
Admin tokens are validated either against the ADMIN_TOKEN env variable (if set) or the admin field from oispahalla.com token validation.
GET - /admin/user/(id/uid/name)/(value)
Returns the user by user MongoDB _id, user Firebase uid, or username.
GET - /admin/score/size/(size)/id/(id)
Returns a score by score MongoDB _id.
GET - /meta/verifyname/(name)/uid/(uid)
Returns a valid
boolean indicating whether the given name is available for the given user and if the name doesn't contain any profanity.
POST - /meta/changename/(token?)
Changes the user's name to the given name. The request should be in the format of:
{
"token": "(token?)",
"name": "(name)"
}