/
App.ts
64 lines (52 loc) · 1.78 KB
/
App.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
/// <reference path="mali.d.ts" />
import mongoose from 'mongoose';
import fs from 'fs';
import * as grpc from 'grpc';
import Mali from 'mali';
import PostService from './services/Post';
import HealthService from './services/Health';
function auth(apiKey: string) {
return async function(ctx: any, next: any) {
const apiKeyProvided:string = ctx.request.get('x-api-key');
if (!apiKeyProvided ||apiKeyProvided !== apiKey) {
console.error(`Invalid apiKey: ${apiKey} when trying to service ${ctx.fullName}`);
throw new Error('invalid.apiKey');
}
await next();
}
}
class App {
server: any;
databaseUrl: string;
port: number;
public constructor(databaseUrl: string, port: number) {
this.databaseUrl = databaseUrl;
this.port = port;
}
public async start(this: App) {
await mongoose.connect(this.databaseUrl, { useNewUrlParser: true });
this.server = new Mali()
this.server.on('error', (err:any, ctx:any) => {
console.error('server error for call %s of type %s', ctx.name, ctx.type, err);
})
this.server.addService(PostService.protoPath, 'PostService');
this.server.addService(HealthService.protoPath);
this.server.use('PostService', auth('myapikey'));
this.server.use(PostService.implementation)
this.server.use(HealthService.implementation);
const credentials = grpc.ServerCredentials.createSsl(
fs.readFileSync(__dirname + '/cert/ca.crt'),
[{
cert_chain: fs.readFileSync(__dirname + '/cert/server.crt'),
private_key: fs.readFileSync(__dirname + '/cert/server.key')
}],
true);
this.server.start(`0.0.0.0:${this.port}`, credentials);
//this.server.start();
console.info('server started');
}
public async stop() {
this.server.stop()
}
}
export default App;