Compare commits
20 Commits
c89fdacafa
...
renovate/e
Author | SHA1 | Date | |
---|---|---|---|
518d87a94d | |||
6a058acd58 | |||
78c16354d3 | |||
415b9f8928 | |||
35d78dc617 | |||
43bdcfe9e1 | |||
fef36b8210 | |||
0430186f7e | |||
3f7b723311 | |||
93ed914eb2 | |||
8d43fce7d4 | |||
d23bf136d4 | |||
af5eeab5a7 | |||
1f9cbc683e | |||
33b30e402f | |||
f6b6d6174a | |||
9cd066b14c | |||
c1820c66c3 | |||
3b49efb839 | |||
386cef160a |
1
.env-example
Normal file
1
.env-example
Normal file
@ -0,0 +1 @@
|
||||
INFISICAL_TOKEN=set me
|
28
TODO.md
Normal file
28
TODO.md
Normal file
@ -0,0 +1,28 @@
|
||||
# Todo list
|
||||
|
||||
Leaderboards:
|
||||
|
||||
- [ ] ScoreSaber
|
||||
- [ ] BeatLeader
|
||||
- [ ] AccSaber?
|
||||
|
||||
Score fetching:
|
||||
|
||||
- Fetch and store all scores from the websocket
|
||||
- Update every players scores every week so we can ensure we have every score stored. (last 20 pages?)
|
||||
|
||||
Player data fetching:
|
||||
|
||||
- Update player data every hour
|
||||
|
||||
BeatSaver data:
|
||||
|
||||
- API Route: "/beatsaver/maps?hashes=id1,id2, etc"
|
||||
- Fetch map data from their api if we haven't already cached it. Cached maps should last for a week or 2 (longer if ranked?)
|
||||
|
||||
Routes:
|
||||
|
||||
- Leaderboard curve for the given Leaderboard
|
||||
- Player data for the given Leaderboard
|
||||
- Player scores for the given Leaderboard: <br/>
|
||||
add a query to add extra score data from BeatLeader (eg: per hand acc, etc)
|
@ -5,19 +5,22 @@
|
||||
"main": "dist/index.js",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"build": "tsup src/index.ts --format cjs",
|
||||
"build": "tsup src/index.ts --format cjs --minify terser",
|
||||
"start": "node dist/index.js",
|
||||
"dev": "nodemon --exec ts-node src/index.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"dotenv": "^16.3.1",
|
||||
"express": "^4.18.2",
|
||||
"infisical-node": "^1.5.0",
|
||||
"mongoose": "^8.0.1",
|
||||
"tsup": "^8.0.0",
|
||||
"typescript": "^5.2.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/express": "^4.17.21",
|
||||
"nodemon": "^3.0.1",
|
||||
"ts-node": "^10.9.1"
|
||||
"terser": "^5.24.0",
|
||||
"ts-node": "^10.9.1",
|
||||
"tsup": "^8.0.0"
|
||||
}
|
||||
}
|
||||
|
2412
pnpm-lock.yaml
generated
2412
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
6
renovate.json
Normal file
6
renovate.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
||||
"extends": [
|
||||
"local>Fascinated/renovate-config"
|
||||
]
|
||||
}
|
41
src/database/models/player.ts
Normal file
41
src/database/models/player.ts
Normal file
@ -0,0 +1,41 @@
|
||||
import mongoose, { Model } from "mongoose";
|
||||
const { Schema } = mongoose;
|
||||
|
||||
const schema = new Schema({
|
||||
/**
|
||||
* The ID of the player
|
||||
*/
|
||||
_id: String,
|
||||
|
||||
scoresaber: {
|
||||
name: String,
|
||||
profilePicture: String,
|
||||
country: String,
|
||||
pp: Number,
|
||||
rank: Number,
|
||||
countryRank: Number,
|
||||
role: String,
|
||||
badges: [
|
||||
{
|
||||
image: String,
|
||||
description: String,
|
||||
},
|
||||
],
|
||||
histories: String,
|
||||
scoreStats: {
|
||||
totalScore: Number,
|
||||
totalRankedScore: Number,
|
||||
averageRankedAccuracy: Number,
|
||||
totalPlayCount: Number,
|
||||
rankedPlayCount: Number,
|
||||
replaysWatched: Number,
|
||||
},
|
||||
permissions: Number,
|
||||
banned: Boolean,
|
||||
inactive: Boolean,
|
||||
},
|
||||
});
|
||||
|
||||
export const PlayerSchema =
|
||||
(mongoose.models.Player as Model<typeof schema>) ||
|
||||
mongoose.model("Player", schema);
|
11
src/database/mongo.ts
Normal file
11
src/database/mongo.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import mongoose from "mongoose";
|
||||
import { Secrets } from "../secrets/secrets";
|
||||
|
||||
/**
|
||||
* Connects to the mongo database
|
||||
*
|
||||
* @returns a promise that resolves when the connection is established
|
||||
*/
|
||||
export function connectMongo() {
|
||||
return mongoose.connect(Secrets.MONGO_URI);
|
||||
}
|
33
src/index.ts
33
src/index.ts
@ -1,4 +1,33 @@
|
||||
import dotenv from "dotenv";
|
||||
import { connectMongo } from "./database/mongo";
|
||||
import { initSecrets } from "./secrets/secrets";
|
||||
import { SsrServer } from "./server/impl/ssrServer";
|
||||
import { checkEnvironmentVariables } from "./util/envVariables";
|
||||
import { createInfisicalClient } from "./util/secrets";
|
||||
|
||||
// Init the SSR Server
|
||||
const server = new SsrServer();
|
||||
// Load the environment variables
|
||||
dotenv.config();
|
||||
|
||||
const isValid = checkEnvironmentVariables("INFISICAL_TOKEN");
|
||||
if (!isValid) {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
export const InfisicalClient = createInfisicalClient(
|
||||
process.env.INFISICAL_TOKEN!
|
||||
);
|
||||
|
||||
console.log("---");
|
||||
console.log(
|
||||
"NOTE: If the infisical secret is invalid, it will say secrets are missing."
|
||||
);
|
||||
console.log("If this happens please check the env variable and/or the secret");
|
||||
console.log("---");
|
||||
// Load the secrets first so we ensure they are valid before starting the server
|
||||
initSecrets().then(async () => {
|
||||
await connectMongo();
|
||||
console.log("Connected to mongo");
|
||||
|
||||
// Init the SSR Server
|
||||
new SsrServer();
|
||||
});
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Request, Response } from "express";
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { Route } from "../route";
|
||||
|
||||
export default class TestRoute extends Route {
|
||||
@ -9,7 +9,7 @@ export default class TestRoute extends Route {
|
||||
});
|
||||
}
|
||||
|
||||
async handle(req: Request, res: Response) {
|
||||
async handle(req: Request, res: Response, next: NextFunction) {
|
||||
res.send("Hello World!");
|
||||
}
|
||||
}
|
||||
|
48
src/route/messages.ts
Normal file
48
src/route/messages.ts
Normal file
@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Creates the base message for web responses
|
||||
*
|
||||
* @param message the message to send
|
||||
* @returns the message
|
||||
*/
|
||||
function baseMessage(error: boolean, message: string) {
|
||||
return {
|
||||
error,
|
||||
message,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an error message for web responses
|
||||
*
|
||||
* @param message the message to send
|
||||
* @returns the message
|
||||
*/
|
||||
function errorMessage(message: string) {
|
||||
return baseMessage(true, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a success message for web responses
|
||||
*
|
||||
* @param message the message to send
|
||||
* @returns the message
|
||||
*/
|
||||
function successMessage(message: string) {
|
||||
return baseMessage(false, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a message for an internal server error
|
||||
*
|
||||
* @returns the message
|
||||
*/
|
||||
function internalServerError() {
|
||||
return errorMessage("Internal Server Error");
|
||||
}
|
||||
|
||||
export const ServerMessages = {
|
||||
baseMessage,
|
||||
errorMessage,
|
||||
successMessage,
|
||||
internalServerError,
|
||||
};
|
@ -1,4 +1,4 @@
|
||||
import { Request, Response } from "express";
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
|
||||
type Method = "GET" | "POST" | "PUT" | "DELETE" | "ALL";
|
||||
|
||||
@ -31,8 +31,6 @@ export abstract class Route {
|
||||
method = "GET";
|
||||
}
|
||||
this.method = method;
|
||||
|
||||
console.log(`Created route handler for ${path}`);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -41,7 +39,7 @@ export abstract class Route {
|
||||
* @param req the request
|
||||
* @param res the response
|
||||
*/
|
||||
abstract handle(req: Request, res: Response): void;
|
||||
abstract handle(req: Request, res: Response, next: NextFunction): void;
|
||||
|
||||
/**
|
||||
* Get the path of the route
|
||||
|
34
src/secrets/secrets.ts
Normal file
34
src/secrets/secrets.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { GetOptions } from "infisical-node/src/types/InfisicalClient";
|
||||
import { InfisicalClient } from "..";
|
||||
|
||||
let MONGO_URI: string;
|
||||
|
||||
/**
|
||||
* Initialize the secrets
|
||||
*/
|
||||
export async function initSecrets() {
|
||||
const options: GetOptions = {
|
||||
environment: process.env.NODE_ENV === "production" ? "main" : "dev",
|
||||
path: "/",
|
||||
type: "shared",
|
||||
};
|
||||
|
||||
const mongoUri = (await InfisicalClient.getSecret("MONGO_URI", options))
|
||||
.secretValue;
|
||||
|
||||
if (!mongoUri) {
|
||||
console.log("MONGO_URI not set in secrets");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
MONGO_URI = mongoUri;
|
||||
}
|
||||
|
||||
/**
|
||||
* All of the Infisical secrets
|
||||
*/
|
||||
export const Secrets = {
|
||||
get MONGO_URI() {
|
||||
return MONGO_URI;
|
||||
},
|
||||
};
|
@ -8,12 +8,4 @@ export class SsrServer extends Server {
|
||||
routes: [new TestRoute()],
|
||||
});
|
||||
}
|
||||
|
||||
public preInit(): void {
|
||||
console.log("preInit");
|
||||
}
|
||||
|
||||
public postInit(): void {
|
||||
console.log("postInit");
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import express, { Express } from "express";
|
||||
import { ServerMessages } from "../route/messages";
|
||||
import { Route } from "../route/route";
|
||||
|
||||
type ServerConfig = {
|
||||
@ -32,16 +33,34 @@ export default class Server {
|
||||
this.server = express();
|
||||
this.preInit();
|
||||
|
||||
// Setup route logging
|
||||
this.server.use((req, res, next) => {
|
||||
// TODO: make this look better?
|
||||
console.log(`[${req.method}] ${req.path}`);
|
||||
next();
|
||||
});
|
||||
|
||||
// Handle the routes
|
||||
for (const route of this.routes) {
|
||||
this.server.all(route.getPath(), (req, res, next) => {
|
||||
if (req.method.toUpperCase() !== route.getMethod().toUpperCase()) {
|
||||
return next(); // Skip this method
|
||||
}
|
||||
route.handle(req, res);
|
||||
try {
|
||||
route.handle(req, res, next);
|
||||
} catch (ex) {
|
||||
console.error(ex);
|
||||
// Inform the user that an internal server error occurred
|
||||
res.status(500).json(ServerMessages.internalServerError());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Log the registered routes
|
||||
console.log(`Registered ${this.routes.length} routes`);
|
||||
for (const route of this.routes) {
|
||||
console.log(` - ${route.getMethod().toUpperCase()} ${route.getPath()}`);
|
||||
}
|
||||
|
||||
// Handle unknown routes
|
||||
this.server.all("*", (req, res) => {
|
||||
@ -50,7 +69,7 @@ export default class Server {
|
||||
|
||||
// Start listening on the specified port
|
||||
this.server.listen(this.port, () => {
|
||||
console.log(`Server listening on port ${this.port}`);
|
||||
console.log(`Server listening on http://localhost:${this.port}`);
|
||||
this.postInit();
|
||||
});
|
||||
}
|
||||
|
15
src/util/envVariables.ts
Normal file
15
src/util/envVariables.ts
Normal file
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Checks if all environment variables are set
|
||||
*
|
||||
* @param variables the environment variables to check
|
||||
* @returns true if all variables are set, false otherwise
|
||||
*/
|
||||
export function checkEnvironmentVariables(...variables: string[]): boolean {
|
||||
let allVariablesSet = true;
|
||||
variables.forEach((variable) => {
|
||||
if (!process.env[variable]) {
|
||||
throw new Error(`${variable} not set in environment variables`);
|
||||
}
|
||||
});
|
||||
return allVariablesSet;
|
||||
}
|
16
src/util/secrets.ts
Normal file
16
src/util/secrets.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import InfisicalClient from "infisical-node";
|
||||
|
||||
/**
|
||||
* Create an infisical client
|
||||
*
|
||||
* @param token the infisical token
|
||||
* @returns the infisical client
|
||||
*/
|
||||
function createInfisicalClient(token: string) {
|
||||
return new InfisicalClient({
|
||||
token,
|
||||
siteURL: "https://secrets.fascinated.cc",
|
||||
});
|
||||
}
|
||||
|
||||
export { createInfisicalClient };
|
Reference in New Issue
Block a user