routes index eslinted
This commit is contained in:
1 parent
331b326f87
commit
75cb144506
1 file changed
+42
-42
+42
-42
@@ -1,24 +1,24 @@
|
|||||||
import {readFile} from 'async-file';
|
import {readFile} from 'async-file'
|
||||||
import handlebars from 'handlebars';
|
import handlebars from 'handlebars'
|
||||||
import route from 'koa-route';
|
import route from 'koa-route'
|
||||||
import compose from 'koa-compose';
|
import compose from 'koa-compose'
|
||||||
|
|
||||||
import {map, assign} from '../utils/common';
|
import {map, assign} from '../utils/common'
|
||||||
import {index as indexFile} from '../config';
|
import {index as indexFile} from '../config'
|
||||||
|
|
||||||
import sitemap from './sitemap';
|
import sitemap from './sitemap'
|
||||||
import schedule from './schedule';
|
import schedule from './schedule'
|
||||||
import {getPageUrls} from '../core/pages';
|
import {getPageUrls} from '../core/pages'
|
||||||
import news from './news';
|
import news from './news'
|
||||||
import gallery from './gallery';
|
import gallery from './gallery'
|
||||||
import error404 from './404';
|
import error404 from './404'
|
||||||
import documents from './documents';
|
import documents from './documents'
|
||||||
import teachers from './teachers';
|
import teachers from './teachers'
|
||||||
import events from './events';
|
import events from './events'
|
||||||
import articles from './articles';
|
import articles from './articles'
|
||||||
import pages from './pages';
|
import pages from './pages'
|
||||||
|
|
||||||
let main = () => route.get('/', index());
|
let main = () => route.get('/', index())
|
||||||
|
|
||||||
export function routes(store) {
|
export function routes(store) {
|
||||||
return compose([
|
return compose([
|
||||||
@@ -33,16 +33,16 @@ export function routes(store) {
|
|||||||
articles(),
|
articles(),
|
||||||
pages(),
|
pages(),
|
||||||
error404()
|
error404()
|
||||||
]);
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
export function queryObject() {
|
export function queryObject() {
|
||||||
let toTrue = map(true, i => i === 'true');
|
let toTrue = map(true, i => i === 'true')
|
||||||
let toFalse = map(false, i => i === 'false');
|
let toFalse = map(false, i => i === 'false')
|
||||||
const processors = [toTrue, toFalse];
|
const processors = [toTrue, toFalse]
|
||||||
|
|
||||||
return async(ctx, next) => {
|
return async(ctx, next) => {
|
||||||
let query = ctx.query;
|
let query = ctx.query
|
||||||
ctx.query = Object
|
ctx.query = Object
|
||||||
.keys(query)
|
.keys(query)
|
||||||
.reduce((query, key) => assign(
|
.reduce((query, key) => assign(
|
||||||
@@ -50,40 +50,40 @@ export function queryObject() {
|
|||||||
key,
|
key,
|
||||||
processors
|
processors
|
||||||
.reduce((value, fn) => fn(value), query[key])
|
.reduce((value, fn) => fn(value), query[key])
|
||||||
), query);
|
), query)
|
||||||
|
|
||||||
await next();
|
await next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function accepts(routes) {
|
export function accepts(routes) {
|
||||||
return async function (ctx) {
|
return async function (ctx) {
|
||||||
let request = ctx.request;
|
let request = ctx.request
|
||||||
let types = Object.keys(routes)
|
let types = Object.keys(routes)
|
||||||
.filter(type => request.accepts(type));
|
.filter(type => request.accepts(type))
|
||||||
|
|
||||||
if (types.length) {
|
if (types.length) {
|
||||||
let fn = routes[types[0]];
|
let fn = routes[types[0]]
|
||||||
|
|
||||||
await fn.apply(ctx, arguments);
|
await fn.apply(ctx, arguments)
|
||||||
} else {
|
} else {
|
||||||
ctx.status = 406;
|
ctx.status = 406
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function index(fn) {
|
export function index(fn) {
|
||||||
return async function (ctx) {
|
return async function (ctx) {
|
||||||
let test = true;
|
let test = true
|
||||||
if (fn) test = await fn.apply(ctx, arguments);
|
if (fn) test = await fn.apply(ctx, arguments)
|
||||||
if (!test) ctx.status = 404;
|
if (!test) ctx.status = 404
|
||||||
|
|
||||||
const data = await getInitialData();
|
const data = await getInitialData()
|
||||||
const source = await readFile(indexFile, 'utf-8');
|
const source = await readFile(indexFile, 'utf-8')
|
||||||
const template = handlebars.compile(source);
|
const template = handlebars.compile(source)
|
||||||
|
|
||||||
ctx.type = 'text/html';
|
ctx.type = 'text/html'
|
||||||
ctx.body = template({data: JSON.stringify(data)});
|
ctx.body = template({data: JSON.stringify(data)})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,10 +92,10 @@ export function json(fn) {
|
|||||||
'text/html': index(),
|
'text/html': index(),
|
||||||
'text/plain': index(),
|
'text/plain': index(),
|
||||||
'application/json': fn
|
'application/json': fn
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getInitialData() {
|
async function getInitialData() {
|
||||||
const pages = await getPageUrls();
|
const pages = await getPageUrls()
|
||||||
return {pages: pages};
|
return {pages: pages}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user