diff --git a/plugins/auth-backend/src/run.ts b/plugins/auth-backend/src/run.ts index a2c2601258..679f8ded2e 100644 --- a/plugins/auth-backend/src/run.ts +++ b/plugins/auth-backend/src/run.ts @@ -14,15 +14,12 @@ * limitations under the License. */ -import yn from 'yn'; import { getRootLogger } from '@backstage/backend-common'; import { startStandaloneServer } from './service/standaloneServer'; -const port = process.env.PLUGIN_PORT ? Number(process.env.PLUGIN_PORT) : 3003; -const enableCors = yn(process.env.PLUGIN_CORS, { default: false }); const logger = getRootLogger(); -startStandaloneServer({ port, enableCors, logger }).catch(err => { +startStandaloneServer({ logger }).catch(err => { logger.error(err); process.exit(1); }); @@ -31,3 +28,5 @@ process.on('SIGINT', () => { logger.info('CTRL+C pressed; exiting.'); process.exit(0); }); + +module.hot?.accept(); diff --git a/plugins/auth-backend/src/service/standaloneApplication.ts b/plugins/auth-backend/src/service/standaloneApplication.ts deleted file mode 100644 index cd81e32ccb..0000000000 --- a/plugins/auth-backend/src/service/standaloneApplication.ts +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2020 Spotify AB - * - * 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 { - errorHandler, - notFoundHandler, - requestLoggingHandler, -} from '@backstage/backend-common'; -import { Config } from '@backstage/config'; -import compression from 'compression'; -import cors from 'cors'; -import express from 'express'; -import helmet from 'helmet'; -import { Logger } from 'winston'; -import { createRouter } from './router'; - -export interface ApplicationOptions { - enableCors: boolean; - logger: Logger; - config: Config; -} - -export async function createStandaloneApplication( - options: ApplicationOptions, -): Promise { - const { enableCors, logger, config } = options; - const app = express(); - - app.use(helmet()); - if (enableCors) { - app.use(cors()); - } - app.use(compression()); - app.use(express.json()); - app.use(requestLoggingHandler()); - app.use('/', await createRouter({ logger, config })); - app.use(notFoundHandler()); - app.use(errorHandler()); - - return app; -} diff --git a/plugins/auth-backend/src/service/standaloneServer.ts b/plugins/auth-backend/src/service/standaloneServer.ts index c03fe44b40..d2372a7e91 100644 --- a/plugins/auth-backend/src/service/standaloneServer.ts +++ b/plugins/auth-backend/src/service/standaloneServer.ts @@ -14,15 +14,15 @@ * limitations under the License. */ +import Knex from 'knex'; import { Server } from 'http'; import { Logger } from 'winston'; -import { createStandaloneApplication } from './standaloneApplication'; import { ConfigReader } from '@backstage/config'; import { loadConfig } from '@backstage/config-loader'; +import { createRouter } from './router'; +import { createServiceBuilder, useHotMemoize } from '@backstage/backend-common'; export interface ServerOptions { - port: number; - enableCors: boolean; logger: Logger; } @@ -32,23 +32,31 @@ export async function startStandaloneServer( const logger = options.logger.child({ service: 'auth-backend' }); const config = ConfigReader.fromConfigs(await loadConfig()); - logger.debug('Creating application...'); - const app = await createStandaloneApplication({ - enableCors: options.enableCors, - logger, - config, + const database = useHotMemoize(module, () => { + const knex = Knex({ + client: 'sqlite3', + connection: ':memory:', + useNullAsDefault: true, + }); + knex.client.pool.on('createSuccess', (_eventId: any, resource: any) => { + resource.run('PRAGMA foreign_keys = ON', () => {}); + }); + return knex; }); logger.debug('Starting application server...'); - return await new Promise((resolve, reject) => { - const server = app.listen(options.port, (err?: Error) => { - if (err) { - reject(err); - return; - } + const router = await createRouter({ + logger, + config, + database, + }); - logger.info(`Listening on port ${options.port}`); - resolve(server); - }); + const service = createServiceBuilder(module) + .enableCors({ origin: 'http://localhost:3000', credentials: true }) + .addRouter('/auth', router); + + return await service.start().catch(err => { + logger.error(err); + process.exit(1); }); }