-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.ts
More file actions
67 lines (50 loc) · 1.97 KB
/
app.ts
File metadata and controls
67 lines (50 loc) · 1.97 KB
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
import express from "express";
import { OpenAPIBackend, Document } from 'openapi-backend';
import yaml from "js-yaml";
import fs from "fs";
import path from "node:path";
import swaggerUi from "swagger-ui-express";
import { routes } from "./routes.ts";
import { SetupLogging } from "./logging.ts";
import nocache from "nocache";
import { createClient } from 'redis';
import { Config } from "./config.ts";
import {Request} from "openapi-backend";
export const redisClient = createClient({
url: `redis://${process.env.APP_OPTIONS_RedisHost}`
}).on('error', err => console.log('Redis Client Error: ', err));
export type CacheClient = typeof redisClient;
Do();
async function Do() {
if (Config().AppOptions.RedisHost) {
await redisClient.connect();
}
SetupLogging();
const app = express();
app.use(nocache());
const port = process.env.PORT ?? 8080;
// TODO: fix/determine why OpenAPIBackend is having issues loading files on its own...
const doc = yaml.load(fs.readFileSync(path.resolve(__dirname, 'openapi.yaml'), 'utf8'));
const castDoc = doc as Document;
const api = new OpenAPIBackend({ definition: castDoc });
api.register({
...routes,
validationFail: (c, _, res) => res.status(400).json({ err: c.validation.errors }),
notFound: (c, _, res) => res.status(404).json({ err: 'not found' }),
});
app.use(express.json());
app.use('/docs', swaggerUi.serve, swaggerUi.setup(doc as swaggerUi.JsonObject));
app.use((req, res) => {
api.handleRequest(req as Request, req, res).catch((reason) => {
console.log(reason);
res.status(500).json({ err: 'An internal error occurred :( Please ask the maintainers of the running application to check the logs.' })
})
});
console.log({
HostPort: port,
ForwardingGitHubRequestsTo: process.env.GITHUB_PROXY ?? "Not forwarding",
ForwardingGroupRequestsTo: process.env.SOURCE_PROXY ?? "Not forwarding",
RedisCacheHost: process.env.APP_OPTIONS_RedisHost ?? "No cache"
})
app.listen(port);
}