-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
47 lines (41 loc) · 1.33 KB
/
main.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
import {
Config,
ConfigEnvars,
createCollectRequestMatcherFromConfig,
EnvBool,
loadConfigOrThrow,
simplifyConfig,
} from "./config.ts";
import { createRequestMatcherHandler } from "./requests.ts";
function onConfigLoaded(config: Config): void {
if (EnvBool.parse(Deno.env.get(ConfigEnvars.show_config) ?? "")) {
console.log("Server starting with config:");
console.log(JSON.stringify(simplifyConfig(config), undefined, 2));
}
}
export type LoadConfigAndServeOptions = {
signal?: AbortSignal;
onConfigLoaded?: (config: Config) => void;
kv?: Deno.Kv;
};
/** Load configuration from environment variables and run the proxy server.
*
* The process exits if the config fails to load. (This is intended to be run
* from the process's main entrypoint.)
*/
export async function loadConfigAndServe(
{ onConfigLoaded: onConfigLoaded_ = onConfigLoaded, signal, kv }:
LoadConfigAndServeOptions = {},
): Promise<Deno.HttpServer<Deno.NetAddr>> {
const config = await loadConfigOrThrow();
onConfigLoaded_(config);
const matcher = await createCollectRequestMatcherFromConfig(config, { kv });
const handler = createRequestMatcherHandler(matcher);
return Deno.serve(
{ port: config.listen.port, hostname: config.listen.hostname, signal },
handler,
);
}
if (import.meta.main) {
await loadConfigAndServe();
}