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

Improve controller

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