forked from awslabs/fhir-works-on-aws-deployment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
36 lines (30 loc) · 1.16 KB
/
index.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
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import serverless from 'serverless-http';
import { generateServerlessRouter } from 'fhir-works-on-aws-routing';
import { getFhirConfig, genericResources } from './config';
const ensureAsyncInit = async (initPromise: Promise<any>): Promise<void> => {
try {
await initPromise;
} catch (e) {
console.error('Async initialization failed', e);
// Explicitly exit the process so that next invocation re-runs the init code.
// This prevents Lambda containers from caching a rejected init promise
process.exit(1);
}
};
async function asyncServerless() {
return serverless(generateServerlessRouter(await getFhirConfig(), genericResources), {
request(request: any, event: any) {
request.user = event.user;
},
});
}
const serverlessHandler: Promise<any> = asyncServerless();
exports.handler = async (event: any = {}, context: any = {}): Promise<any> => {
await ensureAsyncInit(serverlessHandler);
return (await serverlessHandler)(event, context);
};
export default ensureAsyncInit;