Created
March 8, 2022 18:29
-
-
Save larkintuckerllc/4ec674bdd100f50d6ca2b1b8e907547f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* Hi! | |
* | |
* Note that this is an EXAMPLE Backstage backend. Please check the README. | |
* | |
* Happy hacking! | |
*/ | |
import Router from 'express-promise-router'; | |
import { | |
createServiceBuilder, | |
loadBackendConfig, | |
getRootLogger, | |
useHotMemoize, | |
notFoundHandler, | |
CacheManager, | |
DatabaseManager, | |
SingleHostDiscovery, | |
UrlReaders, | |
ServerTokenManager, | |
} from '@backstage/backend-common'; | |
import { TaskScheduler } from '@backstage/backend-tasks'; | |
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 search from './plugins/search'; | |
import { PluginEnvironment } from './types'; | |
import { ServerPermissionClient } from '@backstage/plugin-permission-node'; | |
import myPlugin from './plugins/my-plugin'; | |
function makeCreateEnv(config: Config) { | |
const root = getRootLogger(); | |
const reader = UrlReaders.default({ logger: root, config }); | |
const discovery = SingleHostDiscovery.fromConfig(config); | |
const cacheManager = CacheManager.fromConfig(config); | |
const databaseManager = DatabaseManager.fromConfig(config); | |
const tokenManager = ServerTokenManager.noop(); | |
const taskScheduler = TaskScheduler.fromConfig(config); | |
const permissions = ServerPermissionClient.fromConfig(config, { | |
discovery, | |
tokenManager, | |
}); | |
root.info(`Created UrlReader ${reader}`); | |
return (plugin: string): PluginEnvironment => { | |
const logger = root.child({ type: 'plugin', plugin }); | |
const database = databaseManager.forPlugin(plugin); | |
const cache = cacheManager.forPlugin(plugin); | |
const scheduler = taskScheduler.forPlugin(plugin); | |
return { | |
logger, | |
database, | |
cache, | |
config, | |
reader, | |
discovery, | |
tokenManager, | |
scheduler, | |
permissions, | |
}; | |
}; | |
} | |
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 searchEnv = useHotMemoize(module, () => createEnv('search')); | |
const appEnv = useHotMemoize(module, () => createEnv('app')); | |
const myPluginEnv = useHotMemoize(module, () => createEnv('my-plugin')); | |
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('/search', await search(searchEnv)); | |
apiRouter.use('/my-plugin', await myPlugin(config, myPluginEnv)); | |
// Add backends ABOVE this line; this 404 handler is the catch-all fallback | |
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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment