From d56bd5dfef0985f363027b157b36fb300f73ea47 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 5 Jan 2023 14:32:21 +0100 Subject: [PATCH] backend-app-api: add startHttpServer helper Signed-off-by: Patrik Oldsberg --- .../backend-app-api/src/lib/http/index.ts | 9 ++- .../src/lib/http/startHttpServer.ts | 62 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 packages/backend-app-api/src/lib/http/startHttpServer.ts diff --git a/packages/backend-app-api/src/lib/http/index.ts b/packages/backend-app-api/src/lib/http/index.ts index 0717194f75..94ab61f6c8 100644 --- a/packages/backend-app-api/src/lib/http/index.ts +++ b/packages/backend-app-api/src/lib/http/index.ts @@ -14,5 +14,12 @@ * limitations under the License. */ +export { createHttpServer } from './createHttpServer'; +export { startHttpServer } from './startHttpServer'; +export type { StartHttpServerOptions } from './startHttpServer'; export { readHttpServerOptions } from './config'; -export type { HttpServerOptions, HttpServerCertificateOptions } from './types'; +export type { + HttpServerOptions, + HttpServerCertificateOptions, + ExtendedHttpServer, +} from './types'; diff --git a/packages/backend-app-api/src/lib/http/startHttpServer.ts b/packages/backend-app-api/src/lib/http/startHttpServer.ts new file mode 100644 index 0000000000..64d0be3f1e --- /dev/null +++ b/packages/backend-app-api/src/lib/http/startHttpServer.ts @@ -0,0 +1,62 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { + ConfigService, + RootLifecycleService, + RootLoggerService, +} from '@backstage/backend-plugin-api'; +import { RequestListener } from 'http'; +import { readHttpServerOptions } from './config'; +import { createHttpServer } from './createHttpServer'; + +/** + * Options for {@link startHttpServer}. + * + * @public + */ +export interface StartHttpServerOptions { + config: ConfigService; + logger: RootLoggerService; + lifecycle: RootLifecycleService; +} + +/** + * Starts up an HTTP server, as well as registers a shutdown handler that stops the server. + * + * @public + */ +export async function startHttpServer( + listener: RequestListener, + options: StartHttpServerOptions, +) { + const { config, logger, lifecycle } = options; + + const server = await createHttpServer( + listener, + readHttpServerOptions(config.getOptionalConfig('backend')), + { logger }, + ); + + lifecycle.addShutdownHook({ + async fn() { + await server.stop(); + }, + labels: { type: 'httpServer' }, + }); + + await server.start(); +}