generated from learn-ark/dapp-core-module-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.ts
67 lines (53 loc) · 1.83 KB
/
server.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
import { app } from "@arkecosystem/core-container";
import { createServer, mountServer, plugins } from "@arkecosystem/core-http-utils";
import { Logger } from "@arkecosystem/core-interfaces";
import Hapi from "@hapi/hapi";
import * as handlers from "./handlers";
export class Server {
private logger = app.resolvePlugin<Logger.ILogger>("logger");
private http: any;
public constructor(private readonly config: any) {
this.config = config;
}
public async start(): Promise<void> {
const options = {
host: this.config.host,
port: this.config.port,
};
if (this.config.enabled) {
this.http = await createServer(options);
this.http.app.config = this.config;
await Server.registerRoutes("HTTP", this.http);
}
// TODO: add SSL support. See plugin `core/packages/core-api` for more information
}
public async stop(): Promise<void> {
if (this.http) {
this.logger.info(`Stopping Custom HTTP Server`);
await this.http.stop();
}
}
public async restart(): Promise<void> {
if (this.http) {
await this.http.stop();
await this.http.start();
}
}
public instance(type: string): Hapi.Server {
return this[type];
}
private static async registerRoutes(name: string, server: Hapi.Server): Promise<void> {
await server.register({
plugin: plugins.corsHeaders,
});
server.route({
method: "GET",
path: "/",
handler() {
return { data: "Hello ARKies!" };
},
});
server.route([{ method: "GET", path: "/config", ...handlers.config }]);
await mountServer(`Custom HTTP Public ${name.toUpperCase()} API`, server);
}
}