This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
86 lines (69 loc) · 2.42 KB
/
index.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
74
75
76
77
78
79
80
81
82
83
84
85
86
// Config first
import "dotenv/config";
// Augment Polkadot Types First
import "@frequency-chain/api-augment";
import * as openapiBackend from "openapi-backend";
import Express from "express";
import morgan from "morgan";
import swaggerUi from "swagger-ui-express";
import cors from "cors";
import type { Request } from "openapi-backend";
import * as auth from "./handlers/auth.js";
import * as content from "./handlers/content.js";
import * as graph from "./handlers/graph.js";
import * as profile from "./handlers/profile.js";
import openapiJson from "./openapi.json" assert { type: "json" };
import { getApi } from "./services/frequency.js";
import { getAccountFromAuth } from "./services/auth.js";
// Support BigInt JSON
(BigInt.prototype as any).toJSON = function () {
return this.toString();
};
const app = Express();
app.use(Express.json());
// TODO: See if we want to generate the OpenAPI doc instead of spec first
const api = new openapiBackend.OpenAPIBackend({
definition: "openapi.json",
handlers: {
...auth,
...content,
...graph,
...profile,
validationFail: async (c, req: Express.Request, res: Express.Response) =>
res.status(400).json({ err: c.validation.errors }),
notFound: async (c, req: Express.Request, res: Express.Response) =>
res.status(404).json({ err: "not found" }),
},
});
api.init();
// cors
app.use(cors());
// logging
app.use(morgan("combined"));
// Swagger UI
app.use("/docs", swaggerUi.serve, swaggerUi.setup(openapiJson));
// Simple Token Auth
api.registerSecurityHandler("tokenAuth", async (c) => {
if (typeof c.request.headers.authorization !== "string") return false;
const token = c.request.headers.authorization.split(" ")[1];
const account = await getAccountFromAuth(token);
if (account === null) return false;
// truthy return values are interpreted as auth success
// you can also add any auth information to the return value
return account;
});
api.register("unauthorizedHandler", (_c, _req, res) => {
return res.status(401).send();
});
// use as express middleware
app.use((req, res) => api.handleRequest(req as Request, req, res));
const port = parseInt(process.env.PORT || "0") || "5005";
// start server
app.listen(port, () => {
getApi().catch((e) => {
console.error("Error connecting to Frequency Node!!", e.message);
});
console.info(
`api listening at http://localhost:${port}\nOpenAPI Docs at http://localhost:${port}/docs`,
);
});