-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
117 lines (98 loc) · 3.22 KB
/
main.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// TODO(jhawley): Name variables consistently somehow :(
var https = require('https');
var express = require('express');
var fs = require('fs');
var bodyParser = require('body-parser');
// TODO(jhawley): Move this to correct file
var character_generator = require('./character_generator.js');
var generator_editor = require('./generator_editor.js');
function main() {
// Server init
var options = {
key: fs.readFileSync('certs/key.pem'),
cert: fs.readFileSync('certs/cert.pem')
};
var port = 3000;
var app = express();
// TODO(jhawley): remove this
app.get('/jsonStatic', (req, res) => {
fs.readFile('moreJson.txt', (err, data) => {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(data);
});
});
// Static file servers
app.use(express.static('pages'));
app.use(express.static('style'));
app.use(express.static('scripts'));
app.use(express.static('scripts/components/compiled'));
// Character Generator
app.get('/json', (req, res) => {
character_generator.loadInitialGeneratorState(req.query.characterId)
.then(data => {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(data));
})
.catch(error => {
// TODO(jhawley): Replace this with a better error
console.log(error);
res.writeHead(500);
res.end('Fail!');
});
});
app.post('/saveCharacter', bodyParser.json(), (req, res) => {
character_generator.saveGeneratorState(req.body.characterId,
req.body.characterInfo,
req.body.purchasedHeaders,
req.body.purchasedSkills)
.then(success => {
if (success) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({success: true}));
} else {
res.writeHead(500, {'Content-Type': 'application/json'});
res.end(JSON.stringify({success: false}));
}
})
.catch(error => {
console.log(error);
res.writeHead(500, {'Content-Type': 'application/json'});
res.end(JSON.stringify({success: false, unexpected: true}));
});
});
// Generator Editor
app.get('/loadEditable/characters', bodyParser.json(), (req, res) => {
generator_editor.loadCharacters()
.then(data =>{
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(data));
})
.catch(error => {
// TODO(jhawley): Replace this with a better error
console.log(error);
res.writeHead(500);
res.end('Fail!');
})
});
app.get('/loadEditable/headers', bodyParser.json(), (req, res) => {
generator_editor.loadHeaders()
.then(data =>{
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(data));
})
.catch(error => {
// TODO(jhawley): Replace this with a better error
console.log(error);
res.writeHead(500);
res.end('Fail!');
})
});
app.post('/login', (req, res) => {
// TODO(jhawley): Extract post request params?
res.writeHead(200);
res.end("Woohoo!");
});
var server = https.createServer(options, app)
.listen(port);
}
main();