aboutsummaryrefslogtreecommitdiff
path: root/src/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/index.js')
-rw-r--r--src/index.js64
1 files changed, 33 insertions, 31 deletions
diff --git a/src/index.js b/src/index.js
index 9e59e62..0e19769 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,15 +1,14 @@
-const server = require('./server');
const Sequelize = require('sequelize');
const fs = require('fs');
const path = require('path');
const jwt = require('jsonwebtoken');
-
+// Load the config and configurable parameteres
const config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config.json')));
-
-const dbCreds = config.database;
+const dbCreds = config.database
const secret = config.jwt_secret;
+// An object to help sign and verify jwt cookies
const jwtFunctions = {
sign: function (message) {
return jwt.sign({ value: message }, secret);
@@ -19,6 +18,7 @@ const jwtFunctions = {
}
}
+// Create the database object
const database = new Sequelize(dbCreds.database, undefined, undefined, {
logging(str) {
console.debug(`DB:${str}`);
@@ -35,43 +35,44 @@ const database = new Sequelize(dbCreds.database, undefined, undefined, {
idle: 10000,
},
});
-
+// Connect to database
database.authenticate().then(() => {
- console.debug(`database connection successful: ${dbCreds.database}`);
+ console.debug(`database connection successful`);
}, (e) => console.log(e));
+// Create a sync helper function for the database
async function sync(alter, force, callback) {
await database.sync({ alter, force, logging: console.log });
}
-function setUpModels() {
- const models = {
- "scores": database.define('score', {
- username: {
- type: Sequelize.STRING,
- allowNull: false,
- },
- score: {
- type: Sequelize.INTEGER,
- allowNull: false,
- },
- game: {
- type: Sequelize.STRING,
- allowNull: false,
- },
- uuid: {
- type: Sequelize.STRING,
- allowNull: false
- }
- })
- }
- return models;
+// Create ORM models and sync database
+const models = {
+ "scores": database.define('score', {
+ username: {
+ type: Sequelize.STRING,
+ allowNull: false,
+ },
+ score: {
+ type: Sequelize.INTEGER,
+ allowNull: false,
+ },
+ game: {
+ type: Sequelize.STRING,
+ allowNull: false,
+ },
+ uuid: {
+ type: Sequelize.STRING,
+ allowNull: false
+ }
+ })
}
-
-const models = setUpModels();
sync();
+// Set up main routes
+const server = require('./server');
server.setUpRoutes(models, jwtFunctions, database);
+
+// Load routes for each game
server.load("./ur/server", models, jwtFunctions, database)
server.load("./quadrowple/server", models, jwtFunctions, database)
server.load("./snake/server", models, jwtFunctions, database)
@@ -79,5 +80,6 @@ server.load("./stacker/server", models, jwtFunctions, database)
server.load("./pinball/server", models, jwtFunctions, database)
server.load("./math/server", models, jwtFunctions, database)
server.load("./pp/server", models, jwtFunctions, database)
-server.listen(config.port);
+// Start the server
+server.listen(config.port);