This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.ts
68 lines (61 loc) · 2.2 KB
/
app.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
#!/usr/bin/env node
'use strict'
import * as express from "express";
import * as fs from "fs";
import * as path from "path";
import * as bodyParser from "body-parser";
import * as expressWinston from "express-winston";
import * as cors from "cors";
import * as connector from "./routes/connector";
import * as events from "./routes/events";
import * as remoteApis from "./routes/remoteApis";
import * as localApis from "./routes/localApis";
import * as config from "@varkes/configuration"
const VARKES_LOGO = path.resolve(__dirname, 'resources/logo.svg')
const LOGO_URL = "/logo";
const LOCAL_APIS_URL = "/local";
const REMOTE_APIS_URL = "/remote/apis";
const EVENTS_URL = "/events";
const CONNECTION = "/connection";
const BATCH_REGISTRATION = "/local/registration";
const pathToSwaggerUI = require("swagger-ui-dist").absolutePath()
const LOGGER = config.logger("api-server")
async function init(config: config.Config) {
let app = express()
app.use(bodyParser.json())
app.use(cors())
app.options('*', cors())
app.use(expressWinston.logger(LOGGER))
app.use(REMOTE_APIS_URL, remoteApis.router())
app.use(LOCAL_APIS_URL, localApis.router(config))
app.use(CONNECTION, connector.router())
app.use(EVENTS_URL, events.router())
app.use("/swagger-ui", express.static(pathToSwaggerUI))
app.get("/info", function (req, res) {
let info = {
appName: config.name,
links: {
logo: LOGO_URL,
localApis: LOCAL_APIS_URL + "/apis",
remoteApis: REMOTE_APIS_URL,
connection: CONNECTION,
registration: BATCH_REGISTRATION,
events: EVENTS_URL
}
}
res.status(200).send(info);
});
app.get(LOGO_URL, function (req, res) {
let img = fs.readFileSync(config.logo || VARKES_LOGO)
res.writeHead(200, { 'Content-Type': "image/svg+xml" })
res.end(img, 'binary')
})
app.get("/metadata", function (req, res) {
res.sendFile(path.resolve(__dirname, "resources/api.yaml"))
})
app.get("/console", function (req, res) {
res.sendFile(path.resolve(__dirname, "resources/console.html"))
})
return app;
}
export { init }