-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core-http-utils): merge server utils into an HttpServer class
- Loading branch information
Brian Faust
committed
Sep 3, 2019
1 parent
eceab20
commit d582176
Showing
21 changed files
with
275 additions
and
349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.