+ CLOUD PAGES; style updated; other changes

This commit is contained in:
tmshv committed 2016-11-12 05:17:10 +03:00
1 parent dad12f52f4
commit edf5fbf4bd
27 files changed
+339 -161

No files matched your search

+66 -55
View File
@@ -1,12 +1,14 @@
import fs from 'mz/fs';
import {readFile} from 'async-file';
import handlebars from 'handlebars';
import route from 'koa-route';
import compose from 'koa-compose';
import {map, assign} from '../utils/common';
import config from '../config';
import {index as indexFile} from '../config';
import sitemap from './sitemap';
import schedule from './schedule';
import {getPageUrls} from '../core/pages';
import news from './news';
import gallery from './gallery';
import error404 from './404';
@@ -14,77 +16,86 @@ import documents from './documents';
import teachers from './teachers';
import events from './events';
import articles from './articles';
import pages from './pages';
let main = () => route.get('/', index());
export function routes(){
return compose([
main(),
sitemap(),
schedule(),
news(),
gallery(),
documents(),
teachers(),
events(),
articles(),
error404()
]);
export function routes() {
return compose([
main(),
sitemap(),
schedule(),
news(),
gallery(),
documents(),
teachers(),
events(),
articles(),
pages(),
error404()
]);
}
export function queryObject() {
let toTrue = map(true, i => i === 'true');
let toFalse = map(false, i => i === 'false');
const processors = [toTrue, toFalse];
let toTrue = map(true, i => i === 'true');
let toFalse = map(false, i => i === 'false');
const processors = [toTrue, toFalse];
return async (ctx, next) => {
let query = ctx.query;
ctx.query = Object.keys(query)
.reduce((query, key) => assign(
query,
key,
processors
.reduce((value, fn) => fn(value), query[key])
), query);
return async(ctx, next) => {
let query = ctx.query;
ctx.query = Object
.keys(query)
.reduce((query, key) => assign(
query,
key,
processors
.reduce((value, fn) => fn(value), query[key])
), query);
await next();
}
await next();
}
}
export function accepts(routes) {
return async function (ctx) {
let request = ctx.request;
let types = Object.keys(routes)
.filter(type => request.accepts(type));
return async function (ctx) {
let request = ctx.request;
let types = Object.keys(routes)
.filter(type => request.accepts(type));
if (types.length) {
let fn = routes[types[0]];
if (types.length) {
let fn = routes[types[0]];
await fn.apply(ctx, arguments);
} else {
ctx.status = 406;
}
};
await fn.apply(ctx, arguments);
} else {
ctx.status = 406;
}
};
}
export function index(fn) {
let filename = config['defaultIndex'];
return async function (ctx) {
let test = true;
if (fn) test = await fn.apply(ctx, arguments);
if (!test) ctx.status = 404;
return async function (ctx){
let test = true;
if (fn) test = await fn.apply(ctx, arguments);
const data = await getInitialData();
const source = await readFile(indexFile, 'utf-8');
const template = handlebars.compile(source);
if (!test) ctx.status = 404;
ctx.type = 'text/html';
ctx.body = fs.createReadStream(filename);
}
ctx.type = 'text/html';
ctx.body = template({data: JSON.stringify(data)});
}
}
export function json(fn){
return accepts({
'text/html': index(),
'text/plain': index(),
'application/json': fn
});
export function json(fn) {
return accepts({
'text/html': index(),
'text/plain': index(),
'application/json': fn
});
}
async function getInitialData() {
const pages = await getPageUrls();
return {pages: pages};
}
+24
View File
@@ -0,0 +1,24 @@
import route from 'koa-route';
import {c} from '../core/db';
import {accepts, index} from './';
async function exists(ctx) {
const page = await getPage(ctx.path);
return Boolean(page);
}
async function getPage(path) {
return await c('pages').findOne({url: path});
}
export default function () {
return route.get('*', accepts({
'text/html': index(exists),
'text/plain': index(exists),
'application/json': async(ctx) => {
const page = await getPage(ctx.path);
if (!page) ctx.status = 404;
else ctx.body = page;
}
}))
};