-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
64 lines (54 loc) · 1.58 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
import Koa from 'koa'
import bodyParser from 'koa-bodyparser'
import hooks from './lib/routes/webhooks'
import graphql from './lib/routes/graphql'
import log from './lib/middlewares/logs'
import rescue from './lib/middlewares/rescue'
import i18n from './lib/middlewares/i18n'
import * as initializers from './lib/initializers'
import webhookJob from './lib/jobs/webhook.job'
import { ApolloServer } from 'apollo-server-koa'
import config from './lib/config'
import {
GoodchatApp,
KoaChatContext
} from './lib/typings/goodchat'
/**
* Creates a goodchat Koa application
*
* ```typescript
* const [app] = goodchat()
*
* app.listen(8000, () => ...)
* ```
*
* @exports
* @returns {Promise<GoodchatApp>}
*/
export const goodchat = async () : Promise<[GoodchatApp, ApolloServer]> => {
const app : GoodchatApp = new Koa();
// ----------------------
// Initializers
// ----------------------
await initializers.boot(config);
// ----------------------
// Bootstrap
// ----------------------
app.use((ctx: KoaChatContext, next: Koa.Next) => {
ctx.config = config;
return next();
});
app.use(log());
app.use(rescue());
app.use(i18n());
app.use(bodyParser());
app.use(hooks({
callback: (ev) => {
webhookJob.queue.add(ev.type, ev, { delay: config.jobs.delay });
}
}));
const gqlServer = await graphql();
app.use(gqlServer.getMiddleware());
return [app, gqlServer];
}
export default goodchat