import Router from 'express-promise-router' ; import { createServiceBuilder , loadBackendConfig , getRootLogger , useHotMemoize , notFoundHandler , CacheManager , SingleConnectionDatabaseManager , SingleHostDiscovery , UrlReaders , } from '@backstage/backend-common' ; import { Config } from '@backstage/config' ; import app from './plugins/app' ; import auth from './plugins/auth' ; import catalog from './plugins/catalog' ; import scaffolder from './plugins/scaffolder' ; import proxy from './plugins/proxy' ; import techdocs from './plugins/techdocs' ; import aws from './plugins/aws' ; import argocd from './plugins/argocd' ; import { PluginEnvironment } from './types' ; function makeCreateEnv ( config ) { const root = getRootLogger ( ) ; const reader = UrlReaders . default ( { logger : root , config } ) ; const discovery = SingleHostDiscovery . fromConfig ( config ) ; root . info ( `Created UrlReader ${ reader }` ) ; const databaseManager = SingleConnectionDatabaseManager . fromConfig ( config ) ; const cacheManager = CacheManager . fromConfig ( config ) ; return ( plugin ) : PluginEnvironment => { const logger = root . child ( { type : 'plugin' , plugin } ) ; const database = databaseManager . forPlugin ( plugin ) ; const cache = cacheManager . forPlugin ( plugin ) ; return { logger , database , cache , config , reader , discovery } ; } ; } async function main ( ) { const config = await loadBackendConfig ( { argv : process . argv , logger : getRootLogger ( ) , } ) ; const createEnv = makeCreateEnv ( config ) ; const catalogEnv = useHotMemoize ( module , ( ) => createEnv ( 'catalog' ) ) ; const scaffolderEnv = useHotMemoize ( module , ( ) => createEnv ( 'scaffolder' ) ) ; const authEnv = useHotMemoize ( module , ( ) => createEnv ( 'auth' ) ) ; const proxyEnv = useHotMemoize ( module , ( ) => createEnv ( 'proxy' ) ) ; const techdocsEnv = useHotMemoize ( module , ( ) => createEnv ( 'techdocs' ) ) ; const appEnv = useHotMemoize ( module , ( ) => createEnv ( 'app' ) ) ; const awsEnv = useHotMemoize ( module , ( ) => createEnv ( 'aws' ) ) ; const argocdEnv = useHotMemoize ( module , ( ) => createEnv ( 'argocd' ) ) ; const apiRouter = Router ( ) ; apiRouter . use ( '/catalog' , await catalog ( catalogEnv ) ) ; apiRouter . use ( '/scaffolder' , await scaffolder ( scaffolderEnv ) ) ; apiRouter . use ( '/auth' , await auth ( authEnv ) ) ; apiRouter . use ( '/techdocs' , await techdocs ( techdocsEnv ) ) ; apiRouter . use ( '/proxy' , await proxy ( proxyEnv ) ) ; apiRouter . use ( '/aws' , await aws ( awsEnv ) ) ; apiRouter . use ( '/argocd' , await argocd ( argocdEnv ) ) ; apiRouter . use ( notFoundHandler ( ) ) ; const service = createServiceBuilder ( module ) . loadConfig ( config ) . addRouter ( '/api' , apiRouter ) . addRouter ( '' , await app ( appEnv ) ) ; await service . start ( ) . catch ( err => { console . log ( err ) ; process . exit ( 1 ) ; } ) ; } module . hot ?. accept ( ) ; main ( ) . catch ( error => { console . error ( `Backend failed to start up, ${ error }` ) ; process . exit ( 1 ) ; } ) ;