-
Notifications
You must be signed in to change notification settings - Fork 1
/
WebServer.ts
47 lines (38 loc) · 1.25 KB
/
WebServer.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
import 'reflect-metadata';
import Koa, { Context } from 'koa';
import Logger from 'koa-logger';
import JWT from 'koa-jwt';
import { useKoaServer } from 'routing-controllers';
import { init, koa2, User } from 'leanengine';
import { createAPI } from 'koagger';
import { UserRole, JWTData } from './model';
import controllers from './controller';
const {
LEANCLOUD_APP_ID: appId,
LEANCLOUD_APP_KEY: appKey,
LEANCLOUD_APP_MASTER_KEY: masterKey
} = process.env;
const { swagger, router } = createAPI({ controllers });
init({ appId, appKey, masterKey });
const app = new Koa()
.use(Logger())
.use(koa2())
.use(swagger())
.use(JWT({ secret: appKey, passthrough: true }));
useKoaServer(app, {
cors: { credentials: true },
authorizationChecker: (
{ context: { state } }: { context?: Context },
required_roles: (keyof typeof UserRole)[]
) => {
if (!state.user) return false;
const { roles } = state.user as JWTData;
return required_roles[0]
? roles?.some(role => required_roles.includes(role))
: true;
},
currentUserChecker: ({ context: { state } }: { context?: Context }) =>
User.become((state.user as JWTData).token),
...router
});
export default app;