提交 f6431e7a 编写于 作者: J Jason Park

Improve controller

上级 9818eeff
[submodule "src/backend/public/algorithms"] [submodule "src/backend/public/algorithms"]
path = src/backend/public/algorithms path = src/backend/public/algorithms
url = git@github.com:algorithm-visualizer/algorithms.git url = git@github.com:algorithm-visualizer/algorithms.git
[submodule "src/backend/public/docs"]
path = src/backend/public/docs
url = git@github.com:algorithm-visualizer/tracers.wiki.git
...@@ -5,37 +5,37 @@ import { NotFoundError } from '/common/error'; ...@@ -5,37 +5,37 @@ import { NotFoundError } from '/common/error';
const router = express.Router(); const router = express.Router();
const getPath = (...args) => path.resolve(__dirname, '..', 'public', 'algorithm-visualizer.wiki', ...args); const getPath = (...args) => path.resolve(__dirname, '..', 'public', 'docs', ...args);
const readWikis = () => { const readDocs = () => {
const createKey = name => name.slice(0, -3); const createKey = name => name.slice(0, -3);
const list = dirPath => fs.readdirSync(dirPath).filter(filename => /(\.md)$/.test(filename)); const list = dirPath => fs.readdirSync(dirPath).filter(filename => /(\.md)$/.test(filename));
return list(getPath()).map(wikiName => ({ return list(getPath()).map(docName => ({
key: createKey(wikiName), key: createKey(docName),
name: wikiName, name: docName,
})); }));
}; };
const wikis = readWikis(); const docs = readDocs();
const getWikis = (req, res, next) => { const getDocs = (req, res, next) => {
res.json({ wikis }); res.json({ docs: docs });
}; };
const getWiki = (req, res, next) => { const getDoc = (req, res, next) => {
const { wikiKey } = req.params; const { docKey } = req.params;
const wiki = wikis.find(wiki => wiki.key === wikiKey); const doc = docs.find(doc => doc.key === docKey);
if (!wiki) return next(new NotFoundError()); if (!doc) return next(new NotFoundError());
const wikiPath = getPath(wiki.name); const docPath = getPath(doc.name);
res.sendFile(wikiPath); res.sendFile(docPath);
}; };
router.route('/') router.route('/')
.get(getWikis); .get(getDocs);
router.route('/:wikiKey') router.route('/:docKey')
.get(getWiki); .get(getDoc);
export default router; export default router;
\ No newline at end of file
import express from 'express'; export { default as auth } from './auth';
import { AuthorizationError, NotFoundError, PermissionError } from '/common/error'; export { default as categories } from './categories';
import auth from './auth'; export { default as compilers } from './compilers';
import category from './category'; export { default as docs } from './docs';
import compiler from './compiler';
import wiki from './wiki';
const router = new express.Router();
router.use('/auth', auth);
router.use('/category', category);
router.use('/compiler', compiler);
router.use('/wiki', wiki);
router.use((req, res, next) => next(new NotFoundError()));
router.use((err, req, res, next) => {
const statusMap = [
[AuthorizationError, 401],
[PermissionError, 403],
[NotFoundError, 404],
[Error, 500],
];
const [, status] = statusMap.find(([Error]) => err instanceof Error);
res.status(status);
res.json({
status,
err,
});
console.error(err);
});
export default router;
\ No newline at end of file
...@@ -2,13 +2,30 @@ import express from 'express'; ...@@ -2,13 +2,30 @@ import express from 'express';
import morgan from 'morgan'; import morgan from 'morgan';
import cookieParser from 'cookie-parser'; import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser'; import bodyParser from 'body-parser';
import controllers from '/controllers'; import * as controllers from '/controllers';
import { AuthorizationError, NotFoundError, PermissionError } from '/common/error';
const app = express(); const app = express();
app.use(morgan('tiny')); app.use(morgan('tiny'));
app.use(cookieParser()); app.use(cookieParser());
app.use(bodyParser.json()); app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.urlencoded({ extended: true }));
app.use(controllers); Object.keys(controllers).forEach(key => app.use(`/${key}`, controllers[key]));
app.use((req, res, next) => next(new NotFoundError()));
app.use((err, req, res, next) => {
const statusMap = [
[AuthorizationError, 401],
[PermissionError, 403],
[NotFoundError, 404],
[Error, 500],
];
const [, status] = statusMap.find(([Error]) => err instanceof Error);
res.status(status);
res.json({
status,
err,
});
console.error(err);
});
export default app; export default app;
\ No newline at end of file
Subproject commit 08dc9b2369dd65a69fa5e3d9eb0e2c268fdb94fd
...@@ -54,13 +54,13 @@ const PATCH = URL => { ...@@ -54,13 +54,13 @@ const PATCH = URL => {
}; };
const CategoryApi = { const CategoryApi = {
getCategories: GET('/category'), getCategories: GET('/categories'),
getAlgorithm: GET('/category/:categoryKey/:algorithmKey'), getAlgorithm: GET('/categories/:categoryKey/:algorithmKey'),
}; };
const WikiApi = { const DocApi = {
getWikis: GET('/wiki'), getDocs: GET('/docs'),
getWiki: GET('/wiki/:wiki'), getDoc: GET('/docs/:docKey'),
}; };
const GitHubApi = { const GitHubApi = {
...@@ -77,7 +77,7 @@ let jsWorker = null; ...@@ -77,7 +77,7 @@ let jsWorker = null;
const CompilerApi = { const CompilerApi = {
js: code => new Promise((resolve, reject) => { js: code => new Promise((resolve, reject) => {
if (jsWorker) jsWorker.terminate(); if (jsWorker) jsWorker.terminate();
jsWorker = new Worker('/api/compiler/js'); jsWorker = new Worker('/api/compilers/js');
jsWorker.onmessage = e => resolve(e.data); jsWorker.onmessage = e => resolve(e.data);
jsWorker.onerror = reject; jsWorker.onerror = reject;
jsWorker.postMessage(code); jsWorker.postMessage(code);
...@@ -86,7 +86,7 @@ const CompilerApi = { ...@@ -86,7 +86,7 @@ const CompilerApi = {
export { export {
CategoryApi, CategoryApi,
WikiApi, DocApi,
GitHubApi, GitHubApi,
CompilerApi, CompilerApi,
}; };
\ No newline at end of file
import React from 'react'; import React from 'react';
import { WikiApi } from '/apis'; import { DocApi } from '/apis';
import { MarkdownViewer } from '/components'; import { MarkdownViewer } from '/components';
import { classes } from '/common/util'; import { classes } from '/common/util';
import styles from './stylesheet.scss'; import styles from './stylesheet.scss';
...@@ -18,7 +18,7 @@ class WikiViewer extends React.Component { ...@@ -18,7 +18,7 @@ class WikiViewer extends React.Component {
} }
loadMarkdown(href) { loadMarkdown(href) {
WikiApi.getWiki(href) DocApi.getDoc(href)
.then(source => this.setState({ source })); .then(source => this.setState({ source }));
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册