Skip to content

Commit f6431e7

Browse files
committed
Improve controller
1 parent 9818eef commit f6431e7

File tree

10 files changed

+77
-83
lines changed

10 files changed

+77
-83
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "src/backend/public/algorithms"]
22
path = src/backend/public/algorithms
33
url = git@github.com:algorithm-visualizer/algorithms.git
4+
[submodule "src/backend/public/docs"]
5+
path = src/backend/public/docs
6+
url = git@github.com:algorithm-visualizer/tracers.wiki.git

src/backend/controllers/docs.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import express from 'express';
2+
import fs from 'fs';
3+
import path from 'path';
4+
import { NotFoundError } from '/common/error';
5+
6+
const router = express.Router();
7+
8+
const getPath = (...args) => path.resolve(__dirname, '..', 'public', 'docs', ...args);
9+
10+
const readDocs = () => {
11+
const createKey = name => name.slice(0, -3);
12+
const list = dirPath => fs.readdirSync(dirPath).filter(filename => /(\.md)$/.test(filename));
13+
return list(getPath()).map(docName => ({
14+
key: createKey(docName),
15+
name: docName,
16+
}));
17+
};
18+
19+
const docs = readDocs();
20+
21+
const getDocs = (req, res, next) => {
22+
res.json({ docs: docs });
23+
};
24+
25+
const getDoc = (req, res, next) => {
26+
const { docKey } = req.params;
27+
28+
const doc = docs.find(doc => doc.key === docKey);
29+
if (!doc) return next(new NotFoundError());
30+
31+
const docPath = getPath(doc.name);
32+
res.sendFile(docPath);
33+
};
34+
35+
router.route('/')
36+
.get(getDocs);
37+
38+
router.route('/:docKey')
39+
.get(getDoc);
40+
41+
export default router;

src/backend/controllers/index.js

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,4 @@
1-
import express from 'express';
2-
import { AuthorizationError, NotFoundError, PermissionError } from '/common/error';
3-
import auth from './auth';
4-
import category from './category';
5-
import compiler from './compiler';
6-
import wiki from './wiki';
7-
8-
const router = new express.Router();
9-
10-
router.use('/auth', auth);
11-
router.use('/category', category);
12-
router.use('/compiler', compiler);
13-
router.use('/wiki', wiki);
14-
router.use((req, res, next) => next(new NotFoundError()));
15-
router.use((err, req, res, next) => {
16-
const statusMap = [
17-
[AuthorizationError, 401],
18-
[PermissionError, 403],
19-
[NotFoundError, 404],
20-
[Error, 500],
21-
];
22-
const [, status] = statusMap.find(([Error]) => err instanceof Error);
23-
res.status(status);
24-
res.json({
25-
status,
26-
err,
27-
});
28-
console.error(err);
29-
});
30-
31-
export default router;
1+
export { default as auth } from './auth';
2+
export { default as categories } from './categories';
3+
export { default as compilers } from './compilers';
4+
export { default as docs } from './docs';

src/backend/controllers/wiki.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/backend/index.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,30 @@ import express from 'express';
22
import morgan from 'morgan';
33
import cookieParser from 'cookie-parser';
44
import bodyParser from 'body-parser';
5-
import controllers from '/controllers';
5+
import * as controllers from '/controllers';
6+
import { AuthorizationError, NotFoundError, PermissionError } from '/common/error';
67

78
const app = express();
89
app.use(morgan('tiny'));
910
app.use(cookieParser());
1011
app.use(bodyParser.json());
1112
app.use(bodyParser.urlencoded({ extended: true }));
12-
app.use(controllers);
13+
Object.keys(controllers).forEach(key => app.use(`/${key}`, controllers[key]));
14+
app.use((req, res, next) => next(new NotFoundError()));
15+
app.use((err, req, res, next) => {
16+
const statusMap = [
17+
[AuthorizationError, 401],
18+
[PermissionError, 403],
19+
[NotFoundError, 404],
20+
[Error, 500],
21+
];
22+
const [, status] = statusMap.find(([Error]) => err instanceof Error);
23+
res.status(status);
24+
res.json({
25+
status,
26+
err,
27+
});
28+
console.error(err);
29+
});
1330

1431
export default app;

src/backend/public/docs

Submodule docs added at 08dc9b2

src/frontend/apis/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ const PATCH = URL => {
5454
};
5555

5656
const CategoryApi = {
57-
getCategories: GET('/category'),
58-
getAlgorithm: GET('/category/:categoryKey/:algorithmKey'),
57+
getCategories: GET('/categories'),
58+
getAlgorithm: GET('/categories/:categoryKey/:algorithmKey'),
5959
};
6060

61-
const WikiApi = {
62-
getWikis: GET('/wiki'),
63-
getWiki: GET('/wiki/:wiki'),
61+
const DocApi = {
62+
getDocs: GET('/docs'),
63+
getDoc: GET('/docs/:docKey'),
6464
};
6565

6666
const GitHubApi = {
@@ -77,7 +77,7 @@ let jsWorker = null;
7777
const CompilerApi = {
7878
js: code => new Promise((resolve, reject) => {
7979
if (jsWorker) jsWorker.terminate();
80-
jsWorker = new Worker('/api/compiler/js');
80+
jsWorker = new Worker('/api/compilers/js');
8181
jsWorker.onmessage = e => resolve(e.data);
8282
jsWorker.onerror = reject;
8383
jsWorker.postMessage(code);
@@ -86,7 +86,7 @@ const CompilerApi = {
8686

8787
export {
8888
CategoryApi,
89-
WikiApi,
89+
DocApi,
9090
GitHubApi,
9191
CompilerApi,
9292
};

src/frontend/components/WikiViewer/index.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { WikiApi } from '/apis';
2+
import { DocApi } from '/apis';
33
import { MarkdownViewer } from '/components';
44
import { classes } from '/common/util';
55
import styles from './stylesheet.scss';
@@ -18,7 +18,7 @@ class WikiViewer extends React.Component {
1818
}
1919

2020
loadMarkdown(href) {
21-
WikiApi.getWiki(href)
21+
DocApi.getDoc(href)
2222
.then(source => this.setState({ source }));
2323
}
2424

0 commit comments

Comments
 (0)