-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.ts
45 lines (37 loc) · 1.22 KB
/
router.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { errorHandler, PluginEndpointDiscovery } from '@backstage/backend-common';
import { Config } from '@backstage/config';
import express from 'express';
import Router from 'express-promise-router';
import { Logger } from 'winston';
export interface RouterOptions {
logger: Logger;
config?: Config;
discovery?: PluginEndpointDiscovery;
}
export async function createRouter(
options: RouterOptions,
): Promise<express.Router> {
const { logger, config, discovery } = options;
const router = Router();
router.use(express.json());
router.get('/health', (_, response) => {
logger.info('PONG!');
response.json({ status: 'ok' });
});
// use Config API
router.get('/config/:configId', (request, response) => {
logger.info('Read config');
const {configId} = request.params;
const cfg = config.get(`test-plugin.${configId}`)
response.json({ status: 'ok' , value: cfg});
});
// use Discovery API
router.get('/user', async (_, res) => {
const authUrl = await discovery.getExternalBaseUrl('proxy');
const response = await fetch(`${authUrl}/github/user`);
const data = await response.json();
res.send({ response: data.name });
});
router.use(errorHandler());
return router;
}