Skip to content

Commit d582176

Browse files
committed
refactor(core-http-utils): merge server utils into an HttpServer class
1 parent eceab20 commit d582176

File tree

21 files changed

+275
-349
lines changed

21 files changed

+275
-349
lines changed

__tests__/unit/core-webhooks/server.test.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import { Container, Identifiers, interfaces } from "@packages/core-kernel/src/io
55
import { Enums } from "@packages/core-kernel/src";
66
import { Database } from "@packages/core-webhooks/src/database";
77
import { dirSync, setGracefulCleanup } from "tmp";
8-
import { Server } from "@hapi/hapi";
98
import { startServer } from "@packages/core-webhooks/src/server";
109

10+
// FIX: Types have separate declarations of a private property 'configRepository'.
11+
// This error shows up if we try to resolve "HttpServer" from the "core-http-utils/src" directory.
12+
import { HttpServer } from "../../../node_modules/@arkecosystem/core-http-utils/dist";
13+
1114
const postData = {
1215
event: Enums.Events.State.BlockForged,
1316
target: "https://httpbin.org/post",
@@ -26,30 +29,30 @@ const postData = {
2629
],
2730
};
2831

29-
const request = async (server, method, path, payload = {}) => {
32+
const request = async (server: HttpServer, method, path, payload = {}) => {
3033
const response = await server.inject({ method, url: `http://localhost:4004/api/${path}`, payload });
3134

32-
return { body: response.result, status: response.statusCode };
35+
return { body: response.result as any, status: response.statusCode };
3336
};
3437

3538
const createWebhook = (server, data?: any) => request(server, "POST", "webhooks", data || postData);
3639

37-
let server: Server;
38-
let app: Application;
40+
let server: HttpServer;
3941
let container: interfaces.Container;
4042

4143
beforeEach(async () => {
4244
container = new Container();
43-
container.snapshot();
4445

45-
app = new Application(container);
46+
const app: Application = new Application(container);
4647
app.bind(Identifiers.LogService).toConstantValue({ info: jest.fn(), debug: jest.fn() });
4748
app.bind("path.cache").toConstantValue(dirSync().name);
4849

4950
app.bind<Database>("webhooks.db")
5051
.to(Database)
5152
.inSingletonScope();
5253

54+
container.snapshot();
55+
5356
server = await startServer(app, {
5457
host: "0.0.0.0",
5558
port: 4004,

packages/core-api/src/server.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,55 @@
1+
import { HttpServer } from "@arkecosystem/core-http-utils";
12
import { Providers } from "@arkecosystem/core-kernel";
2-
import expandHomeDir from "expand-home-dir";
3-
import { readFileSync } from "fs";
43

5-
import { Server } from "./server";
4+
import Handlers from "./handlers";
5+
import { preparePlugins } from "./plugins";
66

77
export class ServiceProvider extends Providers.ServiceProvider {
88
public async register(): Promise<void> {
99
this.app.bind("api.options").toConstantValue(this.config());
1010

1111
if (this.config().get("server.http.enabled")) {
12-
this.app
13-
.bind<Server>("api.http")
14-
.to(Server)
15-
.inSingletonScope();
16-
17-
const options: {
18-
enabled: boolean;
19-
host: string;
20-
port: number;
21-
} = { ...this.config().get("server.http") };
22-
23-
delete options.enabled;
24-
25-
await this.app.get<Server>("api.http").init(options, this.config().get("plugins"));
12+
await this.buildServer("http");
2613
}
2714

2815
if (this.config().get("server.https.enabled")) {
29-
this.app
30-
.bind<Server>("api.https")
31-
.to(Server)
32-
.inSingletonScope();
33-
34-
const options: {
35-
enabled: boolean;
36-
host: string;
37-
port: number;
38-
tls: {
39-
key: string;
40-
cert: string;
41-
};
42-
} = { ...this.config().get("server.https") };
43-
44-
delete options.enabled;
45-
46-
options.tls.key = readFileSync(expandHomeDir(options.tls.key)).toString();
47-
options.tls.cert = readFileSync(expandHomeDir(options.tls.cert)).toString();
48-
49-
await this.app.get<Server>("api.https").init(options, this.config().get("plugins"));
16+
await this.buildServer("https");
5017
}
5118
}
5219

5320
public async boot(): Promise<void> {
5421
if (this.config().get("server.http.enabled")) {
55-
await this.app.get<Server>("api.http").start();
22+
await this.app.get<HttpServer>("api.http").start();
5623
}
5724

5825
if (this.config().get("server.https.enabled")) {
59-
await this.app.get<Server>("api.https").start();
26+
await this.app.get<HttpServer>("api.https").start();
6027
}
6128
}
6229

6330
public async dispose(): Promise<void> {
6431
if (this.config().get("server.http.enabled")) {
65-
await this.app.get<Server>("api.http").stop();
32+
await this.app.get<HttpServer>("api.http").stop();
6633
}
6734

6835
if (this.config().get("server.https.enabled")) {
69-
await this.app.get<Server>("api.https").stop();
36+
await this.app.get<HttpServer>("api.https").stop();
7037
}
7138
}
39+
40+
private async buildServer(type: string): Promise<void> {
41+
this.app
42+
.bind<HttpServer>(`api.${type}`)
43+
.to(HttpServer)
44+
.inSingletonScope();
45+
46+
const server: HttpServer = this.app.get<HttpServer>(`api.${type}`);
47+
await server.init(`Public API (${type.toUpperCase()})`, this.config().get(`server.${type}`));
48+
await server.register(preparePlugins(this.config().get("plugins")));
49+
50+
await server.register({
51+
plugin: Handlers,
52+
routes: { prefix: "/api" },
53+
});
54+
}
7255
}

packages/core-elasticsearch/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class ServiceProvider extends Providers.ServiceProvider {
1818

1919
await watchIndices(this.config().get("chunkSize"));
2020

21-
this.app.bind("elasticsearch").toConstantValue(await startServer(this.config().get("server")));
21+
this.app.bind("elasticsearch").toConstantValue(await startServer(this.app, this.config().get("server")));
2222
}
2323

2424
public async dispose(): Promise<void> {

packages/core-elasticsearch/src/server.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { createServer, mountServer } from "@arkecosystem/core-http-utils";
1+
import { HttpServer } from "@arkecosystem/core-http-utils";
2+
import { Contracts } from "@arkecosystem/core-kernel";
23
import Boom from "@hapi/boom";
34
import Joi from "@hapi/joi";
45

56
import { client } from "./client";
67

7-
export const startServer = async config => {
8-
const server = await createServer({
8+
export const startServer = async (app: Contracts.Kernel.Application, config) => {
9+
const server = app.resolve<HttpServer>(HttpServer);
10+
11+
await server.init("Elasticsearch API", {
912
host: config.host,
1013
port: config.port,
1114
});
@@ -76,5 +79,7 @@ export const startServer = async config => {
7679
},
7780
]);
7881

79-
return mountServer("Elasticsearch API", server);
82+
await server.start();
83+
84+
return server;
8085
};

0 commit comments

Comments
 (0)