-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.ts
More file actions
93 lines (77 loc) · 3.29 KB
/
Copy pathindex.ts
File metadata and controls
93 lines (77 loc) · 3.29 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { globSync } from 'glob';
import Router from '@koa/router';
import { getThisDirname } from '../lib/this-file-path';
import { CustomAppContext, CustomAppState } from '../custom-state';
import { createDeferredPromise } from '../lib/defer-promise';
const __dirname = getThisDirname(import.meta.url);
// we initialize and export the router immediately
// but we'll add routes to it here and in each routes file
export const router = new Router<CustomAppState, CustomAppContext>();
export type CustomRouter = Router<CustomAppState, CustomAppContext>;
router.get('/', async (ctx) => {
ctx.body = {
systemStatus: 'nope',
pickedFromRoot: DMNO_CONFIG.ROOT_ONLY,
envCheck: DMNO_CONFIG.API_ONLY || 'env-var-not-loaded',
dmnoTest: DMNO_CONFIG.PORT,
public: DMNO_PUBLIC_CONFIG.PUBLIC_EXAMPLE,
boolFlag_dmno: DMNO_CONFIG.BOOL_NUM_FLAG || '_empty_',
boolFlag_processEnv: process.env.BOOL_NUM_FLAG || '_empty_',
};
});
// special route used to check 500 error handling is working correctly
if (process.env.NODE_ENV === 'test') {
router.get('/boom', async (ctx) => {
// we'll look for this message in our tests to make sure it is not exposed
throw new Error('unexpected error - crash boom bang');
});
}
const routesLoadedDefer = createDeferredPromise();
export const routesLoaded = routesLoadedDefer.promise;
// automatically load all *.routes.ts files in this directory
// (need .js for when the files are built)
const routeFilePaths = globSync(`${__dirname}/**/*.routes.{js,ts}`);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
(async function loadRoutes() {
for (const routeFilePath of routeFilePaths) {
// NOTE this is async, but in practice it's fine
// if we see problems, we can switch over to importing manually...
await import(routeFilePath.replace(__dirname, './'));
}
routesLoadedDefer.resolve();
}());
class TestObj {
prop1 = DMNO_CONFIG.SECRET_FOO;
}
router.get('/redact-demo', async (ctx) => {
const obj = new TestObj();
console.log('secret foo = ', { o: [1, 2, DMNO_CONFIG.SECRET_FOO] });
// console.log('unmasked secret foo = ', unredact(DMNO_CONFIG.SECRET_FOO));
console.log('another secret = ', DMNO_CONFIG.ANOTHER_SECRET);
console.log(`secret value = ${DMNO_CONFIG.SECRET_FOO}`);
console.log({ method: 'console.log', 'secret value': DMNO_CONFIG.SECRET_FOO });
console.dir({ method: 'console.dir', 'secret value': DMNO_CONFIG.SECRET_FOO });
console.warn({ method: 'console.warn', 'secret value': DMNO_CONFIG.SECRET_FOO });
console.error({ method: 'console.error', 'secret value': DMNO_CONFIG.SECRET_FOO });
console.dir({ 'secret value': DMNO_CONFIG.SECRET_FOO });
console.log(['secret value', DMNO_CONFIG.SECRET_FOO, `secret = ${DMNO_CONFIG.SECRET_FOO}`]);
console.log(obj);
console.log({ obj });
ctx.body = { message: 'check logs to see redacted secrets' };
});
router.get('/interceptor-demo', async (ctx) => {
console.log('Hmm better send some debugging data to our logging service 📡');
const apiResp = await fetch('https://api.sampleapis.com/beers/ale', {
headers: {
secret: DMNO_CONFIG.STRIPE_SECRET_KEY,
'x-another': 'bloop',
},
});
const results = await apiResp.json();
ctx.body = results;
});
router.get('/leak-demo', async (ctx) => {
ctx.body = {
leaked: DMNO_CONFIG.SECRET_FOO,
};
});