aboutsummaryrefslogtreecommitdiff
path: root/src/index.js
blob: 9e509280b6aeb20ea4cc923be9138ef6591f940e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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 secret = config.jwt_secret;

// An object to help sign and verify jwt cookies
const jwtFunctions = {
  sign: function (message) {
    return jwt.sign({ value: message }, secret);
  },
  verify: function (token) {
    return jwt.verify(token, secret).value;
  }
}

// Create the database object
const database = new Sequelize(dbCreds.database, undefined, undefined, {
  logging(str) {
    console.debug(`DB:${str}`);
  },
  dialectOptions: {
    charset: 'utf8mb4',
    multipleStatements: true,
  },
  storage: './database.sqlite',
  dialect: 'sqlite',
  pool: {
    max: 5,
    min: 0,
    idle: 10000,
  },
});
// Connect to database
database.authenticate().then(() => {
  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 });
}

// 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
    }
  })
}
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)
server.load("./stacker/server", models, jwtFunctions, database)
server.load("./pinball/server", models, jwtFunctions, database)
server.load("./math/server", models, jwtFunctions, database)
server.load("./cosmic-cargo/server", models, jwtFunctions, database)
server.load("./quiz-bunny/server", models, jwtFunctions, database)
server.load("./pp/server", models, jwtFunctions, database)
server.load("./sim/server", models, jwtFunctions, database)
server.load("./paperflight/server", models, jwtFunctions, database)

// Start the server
server.listen(config.port);