Skip to content

Commit

Permalink
refactor(core-http-utils): merge server utils into an HttpServer class
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Faust committed Sep 3, 2019
1 parent eceab20 commit d582176
Show file tree
Hide file tree
Showing 21 changed files with 275 additions and 349 deletions.
17 changes: 10 additions & 7 deletions __tests__/unit/core-webhooks/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import { Container, Identifiers, interfaces } from "@packages/core-kernel/src/io
import { Enums } from "@packages/core-kernel/src";
import { Database } from "@packages/core-webhooks/src/database";
import { dirSync, setGracefulCleanup } from "tmp";
import { Server } from "@hapi/hapi";
import { startServer } from "@packages/core-webhooks/src/server";

// FIX: Types have separate declarations of a private property 'configRepository'.
// This error shows up if we try to resolve "HttpServer" from the "core-http-utils/src" directory.
import { HttpServer } from "../../../node_modules/@arkecosystem/core-http-utils/dist";

const postData = {
event: Enums.Events.State.BlockForged,
target: "https://httpbin.org/post",
Expand All @@ -26,30 +29,30 @@ const postData = {
],
};

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

return { body: response.result, status: response.statusCode };
return { body: response.result as any, status: response.statusCode };
};

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

let server: Server;
let app: Application;
let server: HttpServer;
let container: interfaces.Container;

beforeEach(async () => {
container = new Container();
container.snapshot();

app = new Application(container);
const app: Application = new Application(container);
app.bind(Identifiers.LogService).toConstantValue({ info: jest.fn(), debug: jest.fn() });
app.bind("path.cache").toConstantValue(dirSync().name);

app.bind<Database>("webhooks.db")
.to(Database)
.inSingletonScope();

container.snapshot();

server = await startServer(app, {
host: "0.0.0.0",
port: 4004,
Expand Down
67 changes: 0 additions & 67 deletions packages/core-api/src/server.ts

This file was deleted.

67 changes: 25 additions & 42 deletions packages/core-api/src/service-provider.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,55 @@
import { HttpServer } from "@arkecosystem/core-http-utils";
import { Providers } from "@arkecosystem/core-kernel";
import expandHomeDir from "expand-home-dir";
import { readFileSync } from "fs";

import { Server } from "./server";
import Handlers from "./handlers";
import { preparePlugins } from "./plugins";

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

if (this.config().get("server.http.enabled")) {
this.app
.bind<Server>("api.http")
.to(Server)
.inSingletonScope();

const options: {
enabled: boolean;
host: string;
port: number;
} = { ...this.config().get("server.http") };

delete options.enabled;

await this.app.get<Server>("api.http").init(options, this.config().get("plugins"));
await this.buildServer("http");
}

if (this.config().get("server.https.enabled")) {
this.app
.bind<Server>("api.https")
.to(Server)
.inSingletonScope();

const options: {
enabled: boolean;
host: string;
port: number;
tls: {
key: string;
cert: string;
};
} = { ...this.config().get("server.https") };

delete options.enabled;

options.tls.key = readFileSync(expandHomeDir(options.tls.key)).toString();
options.tls.cert = readFileSync(expandHomeDir(options.tls.cert)).toString();

await this.app.get<Server>("api.https").init(options, this.config().get("plugins"));
await this.buildServer("https");
}
}

public async boot(): Promise<void> {
if (this.config().get("server.http.enabled")) {
await this.app.get<Server>("api.http").start();
await this.app.get<HttpServer>("api.http").start();
}

if (this.config().get("server.https.enabled")) {
await this.app.get<Server>("api.https").start();
await this.app.get<HttpServer>("api.https").start();
}
}

public async dispose(): Promise<void> {
if (this.config().get("server.http.enabled")) {
await this.app.get<Server>("api.http").stop();
await this.app.get<HttpServer>("api.http").stop();
}

if (this.config().get("server.https.enabled")) {
await this.app.get<Server>("api.https").stop();
await this.app.get<HttpServer>("api.https").stop();
}
}

private async buildServer(type: string): Promise<void> {
this.app
.bind<HttpServer>(`api.${type}`)
.to(HttpServer)
.inSingletonScope();

const server: HttpServer = this.app.get<HttpServer>(`api.${type}`);
await server.init(`Public API (${type.toUpperCase()})`, this.config().get(`server.${type}`));
await server.register(preparePlugins(this.config().get("plugins")));

await server.register({
plugin: Handlers,
routes: { prefix: "/api" },
});
}
}
2 changes: 1 addition & 1 deletion packages/core-elasticsearch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class ServiceProvider extends Providers.ServiceProvider {

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

this.app.bind("elasticsearch").toConstantValue(await startServer(this.config().get("server")));
this.app.bind("elasticsearch").toConstantValue(await startServer(this.app, this.config().get("server")));
}

public async dispose(): Promise<void> {
Expand Down
13 changes: 9 additions & 4 deletions packages/core-elasticsearch/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { createServer, mountServer } from "@arkecosystem/core-http-utils";
import { HttpServer } from "@arkecosystem/core-http-utils";
import { Contracts } from "@arkecosystem/core-kernel";
import Boom from "@hapi/boom";
import Joi from "@hapi/joi";

import { client } from "./client";

export const startServer = async config => {
const server = await createServer({
export const startServer = async (app: Contracts.Kernel.Application, config) => {
const server = app.resolve<HttpServer>(HttpServer);

await server.init("Elasticsearch API", {
host: config.host,
port: config.port,
});
Expand Down Expand Up @@ -76,5 +79,7 @@ export const startServer = async config => {
},
]);

return mountServer("Elasticsearch API", server);
await server.start();

return server;
};
Loading

0 comments on commit d582176

Please sign in to comment.