-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
152 lines (136 loc) · 4 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { Client as AidboxClient } from "@aidbox/sdk-r4";
import Fastify from "fastify";
import fastifyHealthcheck from "fastify-healthcheck";
import { getConfig } from "./config.js";
import { Config, Client, Request, Operations, HttpClient } from "./types.js";
import { dispatch } from "./dispatch.js";
import * as operations from "./operations.js";
import { patientData } from "./patientData.js";
const fastify = Fastify({ logger: true });
fastify.register(fastifyHealthcheck, { exposeUptime: true });
declare module "fastify" {
interface FastifyInstance {
config: Config;
}
interface FastifyRequest {
appToken: string;
aidboxClient: Client;
http: HttpClient;
appConfig: Config;
operations: Operations;
}
}
const main = async () => {
const config = getConfig();
const aidboxClient = new AidboxClient(config.aidbox.url, {
auth: {
method: "basic",
credentials: {
username: config.aidbox.client.id,
password: config.aidbox.client.secret,
},
},
});
const http = await aidboxClient.HTTPClient();
fastify.decorateRequest(
"appToken",
Buffer.from(`${config.app.baseUrl}:${config.app.secret}`).toString("base64")
);
fastify.addHook("onRequest", (request, reply, done) => {
request.aidboxClient = aidboxClient;
done();
});
fastify.addHook("onRequest", (request, reply, done) => {
request.http = http;
done();
});
fastify.addHook("onRequest", (request, reply, done) => {
request.appConfig = config;
done();
});
fastify.addHook("onRequest", (request, reply, done) => {
request.operations = operations as Operations;
done();
});
fastify.get("/", async function handler() {
return { message: "Hello my friend" };
});
fastify.route({
method: "POST",
url: config.app.callbackUrl.startsWith("/")
? config.app.callbackUrl
: `/${config.app.callbackUrl}`,
preHandler: (request, reply, done) => {
if (!request.headers.authorization) {
reply.statusCode = 401;
return reply.send({
error: { message: `Authorization header missing` },
});
}
const appId = config.app.id;
const appSecret = config.app.secret;
const appToken = Buffer.from(`${appId}:${appSecret}`).toString("base64");
const header = request.headers.authorization;
const token = header && header?.split(" ")?.[1];
if (token === appToken) {
return done();
}
reply.statusCode = 401;
reply.send({
error: { message: `Authorization failed for app [${appId}]` },
});
},
handler: (request, reply) => {
dispatch(request as Request, reply);
},
});
try {
let isAidboxReady = false;
let tryCount = 1;
while (!isAidboxReady && tryCount <= 100) {
fastify.log.info(`Check Aidbox Availability... ${tryCount}`);
let response;
try {
response = await http.get("health");
} catch (e: any) {
if (e?.response?.status === 401) {
fastify.log.error(`Please check your access policy for client`);
process.exit(1);
}
fastify.log.error(e.message);
}
if (response) {
isAidboxReady = true;
}
tryCount++;
await new Promise((resolve) => setTimeout(resolve, 1000));
}
await http.put(`App/${config.app.id}`, {
json: {
resourceType: "App",
type: "app",
apiVersion: 1,
endpoint: {
url: `${config.app.baseUrl}${
config.app.callbackUrl.startsWith("/")
? config.app.callbackUrl
: `/${config.app.callbackUrl}`
}`,
type: "http-rpc",
secret: config.app.secret,
},
operations,
},
});
// fastify.log.info("Upload Patient sample data!");
// await http.post(``, {
// json: patientData,
// timeout: 100000,
// });
await fastify.listen({ host: "0.0.0.0", port: config.app.port });
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
main();