-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
73 lines (58 loc) · 1.95 KB
/
mod.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
import { type IProxyConfig, IContext } from "./types.ts";
//@deno-types="npm:@types/express"
import express, { json, urlencoded, RequestHandler } from "npm:express";
//@deno-types="npm:@types/cors"
import cors from "npm:cors";
//@deno-types="npm:@types/morgan"
import morgan from "npm:morgan";
import { green, yellow } from "https://deno.land/[email protected]/fmt/colors.ts";
import { logRegisteredRoutes, updateContext, watch } from "./helpers.ts";
import { routerFactory } from "./router.ts";
import "https://deno.land/[email protected]/dotenv/load.ts";
// Const
const app = express();
const CONFIG_SCOPE = Deno.env.get("CONFIG_SCOPE");
const CONFIG_FILE_NAME = CONFIG_SCOPE
? `proxy.${CONFIG_SCOPE}.config.json`
: "proxy.config.json";
const PATCH_FILE_NAME = CONFIG_SCOPE
? `proxy.${CONFIG_SCOPE}.patch.json`
: "proxy.patch.json";
// Config
const defaultConfig: IProxyConfig = {
port: 8080,
proxyUrl: "",
};
const context: IContext = {
config: {} as IProxyConfig,
patchFile: {},
};
context.config.verbose &&
console.info(`Using ${green(CONFIG_SCOPE || "default")} scope`);
await updateContext(context, CONFIG_FILE_NAME, PATCH_FILE_NAME, defaultConfig);
context.config.watch && watch(() => {
console.clear();
context.config.verbose && console.group(green(`Detected change in cwd, executing update:`));
updateContext(context, CONFIG_FILE_NAME, PATCH_FILE_NAME, defaultConfig)
context.config.verbose && console.groupEnd();
});
// Middleware
if (context.config.verbose) app.use(morgan("dev"));
app.use(
cors({
origin: "*",
credentials: true,
}) as unknown as RequestHandler
);
app.use(json());
app.use(urlencoded({ extended: true }));
// Router
app.use(routerFactory(context));
// Server
app.listen(context.config.port, () => {
logRegisteredRoutes(context);
console.info(
`Proxy listening on ${yellow(`http://localhost:${context.config.port}`)}`
);
context.config.verbose && context.config.watch && console.info(`Watching for changes...`);
});