From f6431e7a47a29b38a0358cb3e88d98fdcdbc41ed Mon Sep 17 00:00:00 2001 From: Jason Park Date: Fri, 13 Jul 2018 20:04:33 +0900 Subject: [PATCH] Improve controller --- .gitmodules | 3 ++ .../{category.js => categories.js} | 0 .../controllers/{compiler.js => compilers.js} | 0 src/backend/controllers/docs.js | 41 +++++++++++++++++++ src/backend/controllers/index.js | 35 ++-------------- src/backend/controllers/wiki.js | 41 ------------------- src/backend/index.js | 21 +++++++++- src/backend/public/docs | 1 + src/frontend/apis/index.js | 14 +++---- src/frontend/components/WikiViewer/index.jsx | 4 +- 10 files changed, 77 insertions(+), 83 deletions(-) rename src/backend/controllers/{category.js => categories.js} (100%) rename src/backend/controllers/{compiler.js => compilers.js} (100%) create mode 100644 src/backend/controllers/docs.js delete mode 100644 src/backend/controllers/wiki.js create mode 160000 src/backend/public/docs diff --git a/.gitmodules b/.gitmodules index 1edad4b..4ca55de 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [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 diff --git a/src/backend/controllers/category.js b/src/backend/controllers/categories.js similarity index 100% rename from src/backend/controllers/category.js rename to src/backend/controllers/categories.js diff --git a/src/backend/controllers/compiler.js b/src/backend/controllers/compilers.js similarity index 100% rename from src/backend/controllers/compiler.js rename to src/backend/controllers/compilers.js diff --git a/src/backend/controllers/docs.js b/src/backend/controllers/docs.js new file mode 100644 index 0000000..ef46dc5 --- /dev/null +++ b/src/backend/controllers/docs.js @@ -0,0 +1,41 @@ +import express from 'express'; +import fs from 'fs'; +import path from 'path'; +import { NotFoundError } from '/common/error'; + +const router = express.Router(); + +const getPath = (...args) => path.resolve(__dirname, '..', 'public', 'docs', ...args); + +const readDocs = () => { + const createKey = name => name.slice(0, -3); + const list = dirPath => fs.readdirSync(dirPath).filter(filename => /(\.md)$/.test(filename)); + return list(getPath()).map(docName => ({ + key: createKey(docName), + name: docName, + })); +}; + +const docs = readDocs(); + +const getDocs = (req, res, next) => { + res.json({ docs: docs }); +}; + +const getDoc = (req, res, next) => { + const { docKey } = req.params; + + const doc = docs.find(doc => doc.key === docKey); + if (!doc) return next(new NotFoundError()); + + const docPath = getPath(doc.name); + res.sendFile(docPath); +}; + +router.route('/') + .get(getDocs); + +router.route('/:docKey') + .get(getDoc); + +export default router; \ No newline at end of file diff --git a/src/backend/controllers/index.js b/src/backend/controllers/index.js index 1ca054a..753c155 100644 --- a/src/backend/controllers/index.js +++ b/src/backend/controllers/index.js @@ -1,31 +1,4 @@ -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'; diff --git a/src/backend/controllers/wiki.js b/src/backend/controllers/wiki.js deleted file mode 100644 index c42cbc5..0000000 --- a/src/backend/controllers/wiki.js +++ /dev/null @@ -1,41 +0,0 @@ -import express from 'express'; -import fs from 'fs'; -import path from 'path'; -import { NotFoundError } from '/common/error'; - -const router = express.Router(); - -const getPath = (...args) => path.resolve(__dirname, '..', 'public', 'algorithm-visualizer.wiki', ...args); - -const readWikis = () => { - 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, - })); -}; - -const wikis = readWikis(); - -const getWikis = (req, res, next) => { - res.json({ wikis }); -}; - -const getWiki = (req, res, next) => { - const { wikiKey } = req.params; - - const wiki = wikis.find(wiki => wiki.key === wikiKey); - if (!wiki) return next(new NotFoundError()); - - const wikiPath = getPath(wiki.name); - res.sendFile(wikiPath); -}; - -router.route('/') - .get(getWikis); - -router.route('/:wikiKey') - .get(getWiki); - -export default router; \ No newline at end of file diff --git a/src/backend/index.js b/src/backend/index.js index e97306d..8d9ea66 100644 --- a/src/backend/index.js +++ b/src/backend/index.js @@ -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 diff --git a/src/backend/public/docs b/src/backend/public/docs new file mode 160000 index 0000000..08dc9b2 --- /dev/null +++ b/src/backend/public/docs @@ -0,0 +1 @@ +Subproject commit 08dc9b2369dd65a69fa5e3d9eb0e2c268fdb94fd diff --git a/src/frontend/apis/index.js b/src/frontend/apis/index.js index 476bb44..bfec75c 100644 --- a/src/frontend/apis/index.js +++ b/src/frontend/apis/index.js @@ -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 diff --git a/src/frontend/components/WikiViewer/index.jsx b/src/frontend/components/WikiViewer/index.jsx index 3e5ba05..55c3eea 100644 --- a/src/frontend/components/WikiViewer/index.jsx +++ b/src/frontend/components/WikiViewer/index.jsx @@ -1,5 +1,5 @@ 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 })); } -- GitLab