-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
154 lines (129 loc) · 3.98 KB
/
index.ts
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import * as Koa from 'koa';
import * as Router from 'koa-router';
import * as json from 'koa-json';
import * as logger from 'koa-logger';
import * as views from 'koa-views';
import * as bodyParser from 'koa-bodyparser';
import * as serve from 'koa-static';
import * as session from 'koa-session';
import * as Handlebars from 'handlebars';
import { getSpiritPageData, getDetailedEventData } from './helpers/transformer';
import requireAuth from './middleware/requireAuth';
import auth from './helpers/auth';
import adminRouter from './controllers/admin';
import escaperoomRouter from './controllers/escaperoom';
import { registerComponentsWithinDirectory } from './helpers/componentRegistration';
import config from './config';
import { getCurrentSpiritYear } from './helpers/utils';
const app = new Koa();
const router = new Router<Koa.DefaultState, Koa.Context>();
const sessionConfig: Partial<session.opts> = {
key: config.auth.cookieKeys[0],
maxAge: 1000 * 60 * 60 * 24 * 3, // 3 days
};
app.keys = config.auth.cookieKeys.slice(1);
app.use(session(sessionConfig, app));
app.use(json());
app.use(logger());
app.use(bodyParser());
app.use(
views(__dirname + '/../views', {
map: {
hbs: 'handlebars',
},
extension: 'hbs',
}),
);
Handlebars.registerHelper('json', function (context) {
return JSON.stringify(context);
});
registerComponentsWithinDirectory('./views/partials');
router.delete('/logout', async (ctx) => {
if (!ctx.session) {
return ctx.throw(
500,
JSON.stringify({
message: 'There was an error logging out',
}),
);
} else {
ctx.session = null;
}
ctx.body = {
success: true,
message: 'Logging out...',
};
});
router.post('/login', bodyParser(), async (ctx) => {
if (ctx.session && ctx.session.authed) return;
if (!ctx.request.body.username || !ctx.request.body.password) {
return ctx.throw(
401,
JSON.stringify({
message: 'Please provide both a username and password',
}),
);
}
let res;
try {
res = await auth(ctx.request.body.username, ctx.request.body.password);
} catch (e) {
return ctx.throw(
401,
JSON.stringify({
message: e.message,
}),
);
}
if (!ctx.session)
return ctx.throw(
500,
JSON.stringify({
message: 'There was an error logging in',
}),
);
ctx.session.authed = true;
ctx.session.isAdmin = res.isAdmin;
ctx.body = {
success: true,
message: res.message,
};
});
router.get('/event/:id', requireAuth, async (ctx, next) => {
const detailedEvent = await getDetailedEventData(ctx.params.id);
if (detailedEvent) {
ctx.body = {
success: true,
detailedEvent,
};
} else {
return ctx.throw(
401,
JSON.stringify({
message: `That event doesn't exist`,
}),
);
}
});
router.get('/archive/:year', requireAuth, async (ctx, next) => {
const requestedYear = ctx.params.year;
const currentYear = getCurrentSpiritYear();
if (requestedYear == currentYear) await ctx.redirect('/');
else if (requestedYear > currentYear) await ctx.redirect('/');
else await ctx.render('pages/spirit', await getSpiritPageData(ctx.params.year, ctx.session.isAdmin));
});
router.get('/', requireAuth, async (ctx, next) => {
try {
const spiritPageData = await getSpiritPageData(getCurrentSpiritYear(), ctx.session.isAdmin);
await ctx.render('pages/spirit', spiritPageData);
} catch (e) {
console.log(e);
}
});
app.use(router.routes());
app.use(adminRouter.routes());
app.use(escaperoomRouter.routes());
app.use(serve('./static', {}));
app.listen(config.server.port, () => {
console.log('Server running on port ' + config.server.port);
});