-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.ts
72 lines (61 loc) · 2.13 KB
/
entry.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
// Refer to tencent cloud document: https://cloud.tencent.com/document/product/583/47274
const SCF_RUNTIME_API: string | undefined = Deno.env.get('SCF_RUNTIME_API');
const SCF_RUNTIME_API_PORT: string | undefined = Deno.env.get(
'SCF_RUNTIME_API_PORT',
);
const READY_URL = `http://${SCF_RUNTIME_API}:${SCF_RUNTIME_API_PORT}/runtime/init/ready`;
const EVENT_URL = `http://${SCF_RUNTIME_API}:${SCF_RUNTIME_API_PORT}/runtime/invocation/next`;
const RESPONSE_URL = `http://${SCF_RUNTIME_API}:${SCF_RUNTIME_API_PORT}/runtime/invocation/response`;
const ERROR_URL = `http://${SCF_RUNTIME_API}:${SCF_RUNTIME_API_PORT}/runtime/invocation/error`;
import { app } from './src/main.jsx';
const PORT = 9000;
async function post(url = '', data = {}) {
// Default options are marked with *
const response = await fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
body: JSON.stringify(data),
});
return response.text();
}
async function forwardEventToRequest(event: any) {
// event to get request
console.log(
'++++++++ Req Url +++++++',
`http://localhost:${PORT}/${event.path}`,
);
// TODO: it just a demo for index path, we should transfer for all paths.
const response = await fetch(`http://localhost:${PORT}/${event.path}`, {
method: 'GET',
});
const body = await response.text();
const apigwReturn = {
statusCode: 200,
body: body,
headers: {
'Content-Type': 'text/html; charset=UTF-8',
},
isBase64Encoded: false,
};
return apigwReturn;
}
async function run() {
// 2. loop for event
// get event
const eventObj: any = await fetch(EVENT_URL);
const event = await eventObj.json();
await app.start({ port: PORT });
const apigwReturn = await forwardEventToRequest(event);
await app.close();
if (!event) {
const error = await post(ERROR_URL, { msg: 'error handling event' });
console.log(`Error response: ${error}`);
} else {
console.log(`Send Invoke Response: ${event}`);
await post(RESPONSE_URL, apigwReturn);
}
}
// 1. post ready -- finish initialization
post(READY_URL, { msg: 'deno ready' }).then(() => {
console.log(`Initialize finish`);
run();
});