-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
120 lines (96 loc) · 3.01 KB
/
index.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
118
119
120
require('dotenv').config();
const { accountManager, profileManager, sessionManager, timeManager, run } = require('./instances');
const express = require('express');
const cors = require('cors');
const app = express();
const port = process.env.PORT;
const accountRouter = require('./router/account');
const profileRouter = require('./router/profile');
const contestRouter = require('./router/contest');
const problemRouter = require('./router/problem');
const adminRouter = require('./router/admin');
const { APIResponse, APIError } = require('./modules/response');
app.set('trust proxy', true);
app.use(cors({
origin: true,
credentials: true
}));
app.use(express.json({ extended: true }));
app.use(express.urlencoded({ extended: true }));
app.use(sessionManager.session);
app.set('view engine', 'ejs');
app.set('views', './views');
app.use((req, res, next) => {
const time = timeManager.timestamp();
const protocol = req.protocol;
const method = req.method;
const path = req.path;
const query = req.query;
const body = req.body;
if (method == 'GET') {
console.log(`[${time}] ${protocol} ${method} ${path} ${JSON.stringify(query)}`);
} else {
console.log(`[${time}] ${protocol} ${method} ${path} ${JSON.stringify(body)}`);
}
next();
});
app.use('/api/account', accountRouter);
app.use('/api/profile', profileRouter);
app.use('/api/contest', contestRouter);
app.use('/api/problem', problemRouter);
app.use('/admin', adminRouter);
app.use((req, res) => {
res.status(404).json(new APIResponse(-1, 'Page Not Found'));
});
app.listen(port, '0.0.0.0', async () => {
console.log(`Auth Server listening on port ${port}`);
await run();
var accountResult = await accountManager.findAccountByPassword({
id: process.env.ADMIN_ID,
password: process.env.ADMIN_PASSWORD
});
if (accountResult.code == 0) {
console.log('Admin account already exists');
return;
}
// create admin account
accountResult = await accountManager.createAccount({
email: process.env.ADMIN_EMAIL,
id: process.env.ADMIN_ID,
password: process.env.ADMIN_PASSWORD,
authority: 1
}, process.env.SALT_SIZE);
if (accountResult instanceof APIError) {
console.log(accountResult);
return;
}
console.log('Admin account created');
var profileResult = await profileManager.findProfiles({
id: process.env.ADMIN_ID
});
if (profileResult instanceof APIError) {
console.log(profileResult);
return;
}
if (profileResult.data.length > 0) {
console.log('Admin profile already exists');
return;
}
// create admin profile
profileResult = await profileManager.createProfile({
id: process.env.ADMIN_ID,
email: process.env.ADMIN_EMAIL,
name: process.env.ADMIN_NAME,
organization: process.env.ADMIN_ORGANIZATION,
department: process.env.ADMIN_DEPARTMENT,
});
if (profileResult instanceof APIError) {
console.log(profileResult);
return;
}
if (profileResult.code == 0) {
console.log('Admin profile created');
} else {
console.log(profileResult);
}
});