This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
57 lines (51 loc) · 1.78 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
import { IS_PRODUCTION } from '@/constants';
import { rateLimitMiddleware } from '@/middlewares';
import { getRouterPath } from '@/utils';
import { Express } from 'express';
import { marked } from 'marked';
import hljs from 'highlight.js';
import fs from 'fs';
import path from 'path';
import { homePath, homeRouter } from './home';
import { authPath, authRouter } from './auth';
marked.setOptions({
highlight: (code, lang) => {
const language = hljs.getLanguage(lang) ? lang : 'plaintext';
return hljs.highlight(code, { language }).value;
},
langPrefix: 'hljs language-',
});
export default (app: Express) => {
if (IS_PRODUCTION) {
app.use(getRouterPath(), rateLimitMiddleware);
}
app.use(authPath, authRouter);
app.use(homePath, homeRouter);
app.use(getRouterPath('/api-docs'), (request, response, next) => {
const baseUrl = request.baseUrl.toLowerCase();
const index = baseUrl.lastIndexOf('/');
const fileName = ['api-docs', 'index'].includes(baseUrl.slice(index + 1))
? 'README'
: baseUrl.slice(index + 1);
const filePath = path.resolve(process.cwd(), 'docs', `${fileName}.md`);
if (fs.existsSync(filePath)) {
response.send(
`<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/styles/github-dark.css">${marked.parse(
fs.readFileSync(filePath, { encoding: 'utf-8' }),
)}`,
);
} else {
next({ status: 404 });
}
});
app.use('*', (request, response, next) => {
next({
status: 404,
message: `No matching routes for ${request.method} ${request.originalUrl}. Do you mean ${
request.method
} ${getRouterPath(request.originalUrl)}?`,
});
});
};
export { authPath, authRouter } from './auth';
export { homePath, homeRouter } from './home';