moving to koa 2.0; + api
This commit is contained in:
1 parent
44724132c0
commit
0ffa403b60
17 files changed
+398
-339
No files matched your search
+5
-3
@@ -22,15 +22,17 @@
|
|||||||
"babel-preset-stage-0": "^6.5.0",
|
"babel-preset-stage-0": "^6.5.0",
|
||||||
"co": "^4.4.0",
|
"co": "^4.4.0",
|
||||||
"instagram-node": "^0.5.1",
|
"instagram-node": "^0.5.1",
|
||||||
"koa-bodyparser": "^2.0.1",
|
"koa": "^2.0.0-alpha.3",
|
||||||
|
"koa-bodyparser": "^3.0.0",
|
||||||
|
"koa-compose": "^3.1.0",
|
||||||
"koa-conditional-get": "^1.0.2",
|
"koa-conditional-get": "^1.0.2",
|
||||||
|
"koa-convert": "^1.2.0",
|
||||||
"koa-etag": "^2.0.0",
|
"koa-etag": "^2.0.0",
|
||||||
"koa-helmet": "^1.0.0",
|
"koa-helmet": "^1.0.0",
|
||||||
"koa-logger": "^1.2.2",
|
"koa-logger": "^1.2.2",
|
||||||
"koa-prerender": "^1.0.3",
|
"koa-prerender": "^1.0.3",
|
||||||
"koa-route": "^2.4.0",
|
"koa-route": "^3.1.0",
|
||||||
"koa-static": "^2.0.0",
|
"koa-static": "^2.0.0",
|
||||||
"koa": "^1.2.0",
|
|
||||||
"mz": "^2.3.1",
|
"mz": "^2.3.1",
|
||||||
"promised-mongo": "git+https://git@github.com/tmshv/promised-mongo",
|
"promised-mongo": "git+https://git@github.com/tmshv/promised-mongo",
|
||||||
"sitemap": "^1.5.0",
|
"sitemap": "^1.5.0",
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import {c} from '../core/db';
|
import {c} from '../core/db';
|
||||||
|
|
||||||
export function* getCollective(query={}, show={}) {
|
export async function getCollective(query={}, show={}) {
|
||||||
let data = yield c('collective').find(query, show);
|
let data = await c('collective').find(query, show);
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import {populate, scheduleDate} from '../models/schedule';
|
|||||||
import {shortifyName} from '../models/collective';
|
import {shortifyName} from '../models/collective';
|
||||||
import {courses} from '../models/course';
|
import {courses} from '../models/course';
|
||||||
|
|
||||||
export function* getSchedule(period, semester) {
|
export async function getSchedule(period, semester) {
|
||||||
let data = yield c('schedules').findOne({
|
let data = await c('schedules').findOne({
|
||||||
period: period,
|
period: period,
|
||||||
semester: semester
|
semester: semester
|
||||||
});
|
});
|
||||||
@@ -15,8 +15,8 @@ export function* getSchedule(period, semester) {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function* getScheduleList() {
|
export async function getScheduleList() {
|
||||||
let list = yield c('schedules')
|
let list = await c('schedules')
|
||||||
.find({}, {period: 1, semester: 1})
|
.find({}, {period: 1, semester: 1})
|
||||||
.toArray();
|
.toArray();
|
||||||
return list
|
return list
|
||||||
@@ -28,8 +28,8 @@ export function* getScheduleList() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function* populateSchedule(schedule) {
|
export async function populateSchedule(schedule) {
|
||||||
let collective = yield getCollective({}, {id: 1, name: 1});
|
let collective = await getCollective({}, {id: 1, name: 1});
|
||||||
let collective_dict = collective.reduce((dict, i) => {
|
let collective_dict = collective.reduce((dict, i) => {
|
||||||
dict[i.id] = i.name;
|
dict[i.id] = i.name;
|
||||||
return dict;
|
return dict;
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import {checkAuth} from '../core/service';
|
||||||
|
|
||||||
|
export function ensureServiceAuth() {
|
||||||
|
return async (ctx, next) => {
|
||||||
|
let service = ctx.request.headers['x-service-name'];
|
||||||
|
let token = ctx.request.headers['x-service-token'];
|
||||||
|
|
||||||
|
if (!checkAuth(service, token)) {
|
||||||
|
ctx.status = 401;
|
||||||
|
} else {
|
||||||
|
await next();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
+13
-13
@@ -1,26 +1,26 @@
|
|||||||
import {index, accepts} from './';
|
import {index, accepts} from './';
|
||||||
|
|
||||||
export default function () {
|
export default function () {
|
||||||
return function *(next) {
|
return async(ctx) => {
|
||||||
yield next;
|
if (ctx.status != 404) return;
|
||||||
|
|
||||||
if (404 != this.status) return;
|
ctx.status = 404;
|
||||||
|
ctx.body = 'lol found';
|
||||||
|
|
||||||
this.status = 404;
|
await accepts({
|
||||||
yield accepts({
|
|
||||||
'text/html': index(),
|
'text/html': index(),
|
||||||
|
'text/plain': async ctx => {
|
||||||
'text/plain': function *() {
|
ctx.type = 'text/plain';
|
||||||
this.type = 'text/plain';
|
ctx.body = 'Page not found';
|
||||||
this.body = 'Page not found';
|
|
||||||
},
|
},
|
||||||
|
|
||||||
'application/json': function *() {
|
'application/json': async ctx => {
|
||||||
this.type = 'application/json';
|
ctx.type = 'application/json';
|
||||||
this.body = {
|
ctx.body = {
|
||||||
|
error: 404,
|
||||||
message: 'Not Found'
|
message: 'Not Found'
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
})(ctx);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import {put} from 'koa-route';
|
||||||
|
import compose from 'koa-compose';
|
||||||
|
import {ensureServiceAuth} from '../../middlewares/ensureServiceAuth';
|
||||||
|
|
||||||
|
export function add() {
|
||||||
|
return put('/api/v1/feed', compose([
|
||||||
|
ensureServiceAuth(),
|
||||||
|
async ctx => {
|
||||||
|
let post = ctx.body;
|
||||||
|
ctx.body = {
|
||||||
|
pong: post
|
||||||
|
};
|
||||||
|
}
|
||||||
|
]));
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import {add as feedAdd} from './feed';
|
||||||
|
import compose from 'koa-compose';
|
||||||
|
|
||||||
|
export default function () {
|
||||||
|
return compose([
|
||||||
|
feedAdd()
|
||||||
|
]);
|
||||||
|
};
|
||||||
+17
-27
@@ -1,38 +1,28 @@
|
|||||||
import route from 'koa-route';
|
import route from 'koa-route';
|
||||||
import {c} from '../core/db';
|
import {c} from '../core/db';
|
||||||
import {accepts, index} from './';
|
import {accepts, index} from './';
|
||||||
import {sortBy} from '../utils/sort';
|
|
||||||
|
|
||||||
export default function (app) {
|
async function exists(ctx, id) {
|
||||||
|
let i = await article(id);
|
||||||
|
return i ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
function* exists(id) {
|
async function article(id){
|
||||||
let i = yield c('events').findOne({id: id});
|
let i = await c('events').findOne({id: id});
|
||||||
if (!i) {
|
if (!i) {
|
||||||
i = yield c('timeline').findOne({id: id});
|
i = await c('timeline').findOne({id: id});
|
||||||
}
|
|
||||||
return i ? true : false;
|
|
||||||
}
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
app.use(route.get('/article/:id', accepts({
|
export default function () {
|
||||||
//'text/html': function* (id){
|
return route.get('/article/:id', accepts({
|
||||||
// let test = yield exists(id);
|
|
||||||
// if(test){
|
|
||||||
// return yield index();
|
|
||||||
// }else{
|
|
||||||
// this.status = 404;
|
|
||||||
// return yield index();
|
|
||||||
// }
|
|
||||||
//},
|
|
||||||
'text/html': index(exists),
|
'text/html': index(exists),
|
||||||
'text/plain': index(exists),
|
'text/plain': index(exists),
|
||||||
'application/json': function* (id) {
|
'application/json': async (ctx, id) => {
|
||||||
let i = yield c('events').findOne({id: id});
|
let i = await article(id);
|
||||||
if (!i) {
|
if (!i) ctx.status = 404;
|
||||||
i = yield c('timeline').findOne({id: id});
|
else ctx.body = i;
|
||||||
}
|
|
||||||
|
|
||||||
if (!i) this.status = 404;
|
|
||||||
else this.body = i;
|
|
||||||
}
|
}
|
||||||
})));
|
}))
|
||||||
};
|
};
|
||||||
+62
-57
@@ -1,68 +1,73 @@
|
|||||||
|
import compose from 'koa-compose';
|
||||||
import route from 'koa-route';
|
import route from 'koa-route';
|
||||||
import {index, json, accepts} from './';
|
import {index, json, accepts} from './';
|
||||||
import {c} from '../core/db';
|
import {c} from '../core/db';
|
||||||
|
|
||||||
function* exists(id) {
|
async function exists(ctx, id) {
|
||||||
let i = yield c('documents').findOne({id: id});
|
let i = await c('documents').findOne({id: id});
|
||||||
return i ? true : false;
|
return i ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function (app) {
|
export default function () {
|
||||||
[
|
return compose([
|
||||||
[
|
documents(),
|
||||||
'/documents',
|
document(),
|
||||||
json(function *() {
|
type()
|
||||||
let query = {};
|
]);
|
||||||
this.body = yield c('documents')
|
|
||||||
.find(query)
|
|
||||||
.toArray();
|
|
||||||
})
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'/documents/type/:type',
|
|
||||||
function *(type) {
|
|
||||||
let types = {
|
|
||||||
'document': ['documents', {type: 'document'}, populateDocPreview],
|
|
||||||
'award': ['documents', {type: 'award'}, populateDocPreview]
|
|
||||||
};
|
|
||||||
|
|
||||||
if (type in types) {
|
|
||||||
let [collection, query, mapFn] = types[type];
|
|
||||||
|
|
||||||
let docs = yield c(collection)
|
|
||||||
.find(query)
|
|
||||||
.toArray();
|
|
||||||
|
|
||||||
if (mapFn) docs = yield docs.map(mapFn);
|
|
||||||
|
|
||||||
this.body = docs;
|
|
||||||
} else {
|
|
||||||
this.status = 404;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'/documents/:id',
|
|
||||||
accepts({
|
|
||||||
'text/html': index(exists),
|
|
||||||
'text/plain': index(exists),
|
|
||||||
'application/json': function* (id) {
|
|
||||||
let i = yield c('documents').findOne({id: id});
|
|
||||||
if (!i) this.status = 404;
|
|
||||||
else {
|
|
||||||
i = yield populateDocPreview(i);
|
|
||||||
this.body = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
]
|
|
||||||
].forEach(item => {
|
|
||||||
let [path, fn] = item;
|
|
||||||
app.use(route.get(path, fn));
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function* populateDocPreview(doc) {
|
function documents() {
|
||||||
doc.preview = yield c('images').findOne({_id: doc.preview});
|
return route.get('/documents', json(
|
||||||
|
async(ctx) => {
|
||||||
|
let query = {};
|
||||||
|
ctx.body = await c('documents')
|
||||||
|
.find(query)
|
||||||
|
.toArray();
|
||||||
|
}
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
function document() {
|
||||||
|
return route.get('/documents/:id', accepts({
|
||||||
|
'text/html': index(exists),
|
||||||
|
'text/plain': index(exists),
|
||||||
|
'application/json': async(ctx, id) => {
|
||||||
|
let i = await c('documents').findOne({id: id});
|
||||||
|
if (!i) ctx.status = 404;
|
||||||
|
else {
|
||||||
|
i = await populateDocPreview(i);
|
||||||
|
ctx.body = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
function type() {
|
||||||
|
return route.get('/documents/type/:type', async(ctx, type) => {
|
||||||
|
let types = {
|
||||||
|
'document': ['documents', {type: 'document'}, populateDocPreview],
|
||||||
|
'award': ['documents', {type: 'award'}, populateDocPreview]
|
||||||
|
};
|
||||||
|
|
||||||
|
if (type in types) {
|
||||||
|
let [collection, query, mapFn] = types[type];
|
||||||
|
|
||||||
|
let docs = await c(collection)
|
||||||
|
.find(query)
|
||||||
|
.toArray();
|
||||||
|
|
||||||
|
if (mapFn) docs = await Promise.all(
|
||||||
|
docs.map(mapFn)
|
||||||
|
);
|
||||||
|
|
||||||
|
ctx.body = docs;
|
||||||
|
} else {
|
||||||
|
ctx.status = 404;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function populateDocPreview(doc) {
|
||||||
|
doc.preview = await c('images').findOne({_id: doc.preview});
|
||||||
return doc;
|
return doc;
|
||||||
}
|
}
|
||||||
+13
-13
@@ -3,34 +3,34 @@ import {c} from '../core/db';
|
|||||||
import {json} from './';
|
import {json} from './';
|
||||||
import {sortBy} from '../utils/sort';
|
import {sortBy} from '../utils/sort';
|
||||||
|
|
||||||
export default function (app) {
|
let sortByDate = sortBy(
|
||||||
let sortByDate = sortBy(
|
i => (new Date(i.date))
|
||||||
i => (new Date(i.date))
|
.getTime()
|
||||||
.getTime()
|
);
|
||||||
);
|
|
||||||
|
|
||||||
app.use(route.get('/events/:page?', json(
|
export default function () {
|
||||||
function *() {
|
return route.get('/events/:page?', json(
|
||||||
let limit = parseInt(this.query['limit']) || 10;
|
async(ctx) => {
|
||||||
let skip = parseInt(this.query['skip']) || 0;
|
let limit = parseInt(ctx.query['limit']) || 10;
|
||||||
|
let skip = parseInt(ctx.query['skip']) || 0;
|
||||||
|
|
||||||
let query = {};
|
let query = {};
|
||||||
let documents = yield c('events')
|
let documents = await c('events')
|
||||||
.find(query)
|
.find(query)
|
||||||
.sort({date: -1})
|
.sort({date: -1})
|
||||||
.skip(skip)
|
.skip(skip)
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.toArray();
|
.toArray();
|
||||||
|
|
||||||
let total = yield c('events')
|
let total = await c('events')
|
||||||
.find(query).count();
|
.find(query).count();
|
||||||
|
|
||||||
this.body = {
|
ctx.body = {
|
||||||
skip: skip,
|
skip: skip,
|
||||||
limit: limit,
|
limit: limit,
|
||||||
total: total,
|
total: total,
|
||||||
data: documents
|
data: documents
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
)));
|
));
|
||||||
};
|
};
|
||||||
+41
-42
@@ -1,33 +1,14 @@
|
|||||||
import co from 'co';
|
import co from 'co';
|
||||||
|
import compose from 'koa-compose';
|
||||||
import route from 'koa-route';
|
import route from 'koa-route';
|
||||||
import {c} from '../core/db';
|
import {c} from '../core/db';
|
||||||
import {json} from './';
|
import {json} from './';
|
||||||
|
|
||||||
export default function (app) {
|
export default function () {
|
||||||
app.use(route.get('/gallery', json(
|
return compose([
|
||||||
function *() {
|
gallery(),
|
||||||
let query = {};
|
album()
|
||||||
let fields = ['_id', 'id', 'title', 'date'];
|
]);
|
||||||
//.reduce((f, i) => {
|
|
||||||
// f[i] = 1;
|
|
||||||
// return f;
|
|
||||||
//}, {});
|
|
||||||
|
|
||||||
let albums = yield c('albums')
|
|
||||||
.find(query)
|
|
||||||
.toArray();
|
|
||||||
|
|
||||||
//albums = albums.map(i => {
|
|
||||||
// return fields.reduce((a, key)=> {
|
|
||||||
// a[key] = i[key];
|
|
||||||
// return a;
|
|
||||||
// }, {});
|
|
||||||
//});
|
|
||||||
|
|
||||||
albums = yield albums.map(processAlbum);
|
|
||||||
this.body = albums;
|
|
||||||
}
|
|
||||||
)));
|
|
||||||
|
|
||||||
//app.use(route.get('/gallery/:year', json(
|
//app.use(route.get('/gallery/:year', json(
|
||||||
// function *(year) {
|
// function *(year) {
|
||||||
@@ -45,28 +26,46 @@ export default function (app) {
|
|||||||
// this.body = albums;
|
// this.body = albums;
|
||||||
// }
|
// }
|
||||||
//)));
|
//)));
|
||||||
|
};
|
||||||
|
|
||||||
app.use(route.get('/album/:id', json(
|
function gallery(){
|
||||||
function *(id) {
|
return route.get('/gallery', json(
|
||||||
//year = parseInt(year);
|
async(ctx) => {
|
||||||
//course = '/' + course;
|
let query = {};
|
||||||
//album = '/' + album;
|
let fields = ['_id', 'id', 'title', 'date'];
|
||||||
//let start = new Date(year, 0, 1);
|
//.reduce((f, i) => {
|
||||||
//let end = new Date(year, 11, 31);
|
// f[i] = 1;
|
||||||
//let query = {
|
// return f;
|
||||||
// date: {$gte: start, $lt: end},
|
//}, {});
|
||||||
// course_uri: course,
|
|
||||||
// uri: album
|
|
||||||
//};
|
|
||||||
|
|
||||||
let record = yield c('albums').findOne({
|
let albums = await c('albums')
|
||||||
|
.find(query)
|
||||||
|
.toArray();
|
||||||
|
|
||||||
|
//albums = albums.map(i => {
|
||||||
|
// return fields.reduce((a, key)=> {
|
||||||
|
// a[key] = i[key];
|
||||||
|
// return a;
|
||||||
|
// }, {});
|
||||||
|
//});
|
||||||
|
|
||||||
|
albums = await Promise.all(albums.map(processAlbum));
|
||||||
|
ctx.body = albums;
|
||||||
|
}
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
function album(){
|
||||||
|
return route.get('/album/:id', json(
|
||||||
|
async(ctx, id) => {
|
||||||
|
let record = await c('albums').findOne({
|
||||||
id: id
|
id: id
|
||||||
});
|
});
|
||||||
record = yield processAlbum(record);
|
record = await processAlbum(record);
|
||||||
this.body = record;
|
ctx.body = record;
|
||||||
}
|
}
|
||||||
)));
|
));
|
||||||
};
|
}
|
||||||
|
|
||||||
let processAlbum = album => co(function *() {
|
let processAlbum = album => co(function *() {
|
||||||
//album.url = `/gallery/{year}{course}{album}`
|
//album.url = `/gallery/{year}{course}{album}`
|
||||||
|
|||||||
+38
-38
@@ -1,5 +1,6 @@
|
|||||||
import fs from 'mz/fs';
|
import fs from 'mz/fs';
|
||||||
import route from 'koa-route';
|
import route from 'koa-route';
|
||||||
|
import compose from 'koa-compose';
|
||||||
|
|
||||||
import {map, assign} from '../utils/common';
|
import {map, assign} from '../utils/common';
|
||||||
import config from '../config';
|
import config from '../config';
|
||||||
@@ -16,19 +17,19 @@ import articles from './articles';
|
|||||||
|
|
||||||
let main = () => route.get('/', index());
|
let main = () => route.get('/', index());
|
||||||
|
|
||||||
export function routes(app){
|
export function routes(){
|
||||||
app.use(main());
|
return compose([
|
||||||
|
main(),
|
||||||
sitemap(app);
|
sitemap(),
|
||||||
schedule(app);
|
schedule(),
|
||||||
news(app);
|
news(),
|
||||||
gallery(app);
|
gallery(),
|
||||||
documents(app);
|
documents(),
|
||||||
events(app);
|
teachers(),
|
||||||
articles(app);
|
events(),
|
||||||
teachers(app);
|
articles(),
|
||||||
|
error404()
|
||||||
app.use(error404());
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function queryObject() {
|
export function queryObject() {
|
||||||
@@ -36,9 +37,9 @@ export function queryObject() {
|
|||||||
let toFalse = map(false, i => i === 'false');
|
let toFalse = map(false, i => i === 'false');
|
||||||
const processors = [toTrue, toFalse];
|
const processors = [toTrue, toFalse];
|
||||||
|
|
||||||
return function *(next) {
|
return async (ctx, next) => {
|
||||||
let query = this.query;
|
let query = ctx.query;
|
||||||
this.query = Object.keys(query)
|
ctx.query = Object.keys(query)
|
||||||
.reduce((query, key) => assign(
|
.reduce((query, key) => assign(
|
||||||
query,
|
query,
|
||||||
key,
|
key,
|
||||||
@@ -46,23 +47,22 @@ export function queryObject() {
|
|||||||
.reduce((value, fn) => fn(value), query[key])
|
.reduce((value, fn) => fn(value), query[key])
|
||||||
), query);
|
), query);
|
||||||
|
|
||||||
yield next;
|
await next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function accepts(routes, def) {
|
export function accepts(routes) {
|
||||||
return function *() {
|
return async function (ctx) {
|
||||||
let req = this.request;
|
let request = ctx.request;
|
||||||
let types = Object.keys(routes)
|
let types = Object.keys(routes)
|
||||||
.filter(type => req.accepts(type));
|
.filter(type => request.accepts(type));
|
||||||
|
|
||||||
if (types.length) {
|
if (types.length) {
|
||||||
let type = types[0];
|
let fn = routes[types[0]];
|
||||||
yield routes[type].apply(this, arguments);
|
|
||||||
} else if (def) {
|
await fn.apply(ctx, arguments);
|
||||||
yield def;
|
|
||||||
} else {
|
} else {
|
||||||
this.status = 406;
|
ctx.status = 406;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -70,21 +70,21 @@ export function accepts(routes, def) {
|
|||||||
export function index(fn) {
|
export function index(fn) {
|
||||||
let filename = config['defaultIndex'];
|
let filename = config['defaultIndex'];
|
||||||
|
|
||||||
return function *() {
|
return async function (ctx){
|
||||||
let test = true;
|
let test = true;
|
||||||
if(fn) test = yield fn.apply(this, arguments);
|
if (fn) test = await fn.apply(ctx, arguments);
|
||||||
|
|
||||||
if(!test) {
|
if (!test) ctx.status = 404;
|
||||||
this.status = 404;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.type = 'text/html';
|
ctx.type = 'text/html';
|
||||||
this.body = fs.createReadStream(filename);
|
ctx.body = fs.createReadStream(filename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const json = fn => accepts({
|
export function json(fn){
|
||||||
'text/html': index(),
|
return accepts({
|
||||||
'text/plain': index(),
|
'text/html': index(),
|
||||||
'application/json': fn
|
'text/plain': index(),
|
||||||
});
|
'application/json': fn
|
||||||
|
});
|
||||||
|
}
|
||||||
+36
-32
@@ -1,32 +1,38 @@
|
|||||||
|
import compose from 'koa-compose';
|
||||||
import route from 'koa-route';
|
import route from 'koa-route';
|
||||||
import {accepts, index} from './';
|
import {accepts} from './';
|
||||||
import {c} from '../core/db';
|
import {c} from '../core/db';
|
||||||
import {sortBy} from '../utils/sort';
|
import {sortBy} from '../utils/sort';
|
||||||
|
|
||||||
export default function (app) {
|
const sortNewsByDate = sortBy(
|
||||||
let sortNewsByDate = sortBy(
|
i => (new Date(i.date))
|
||||||
i => (new Date(i.date))
|
.getTime()
|
||||||
.getTime()
|
);
|
||||||
);
|
|
||||||
|
|
||||||
app.use(route.get('/news', accepts({
|
const redirect = async ctx => ctx.redirect('/');
|
||||||
'text/html': function *() {
|
|
||||||
this.redirect('/');
|
export default function () {
|
||||||
},
|
return compose([
|
||||||
'text/plain': function *() {
|
news(),
|
||||||
this.redirect('/');
|
pinned()
|
||||||
},
|
]);
|
||||||
'application/json': function *() {
|
};
|
||||||
let limit = parseInt(this.query['limit']) || 10;
|
|
||||||
let skip = parseInt(this.query['skip']) || 0;
|
function news() {
|
||||||
|
return route.get('/news', accepts({
|
||||||
|
'text/html': redirect,
|
||||||
|
'text/plain': redirect,
|
||||||
|
'application/json': async ctx => {
|
||||||
|
let limit = parseInt(ctx.query['limit']) || 10;
|
||||||
|
let skip = parseInt(ctx.query['skip']) || 0;
|
||||||
|
|
||||||
let query = {};
|
let query = {};
|
||||||
if (this.query.type) query.type = this.query.type;
|
if (ctx.query.type) query.type = ctx.query.type;
|
||||||
|
|
||||||
let pinned = [];
|
let pinned = [];
|
||||||
if(skip == 0) {
|
if(skip == 0) {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
pinned = yield c('timeline')
|
pinned = await c('timeline')
|
||||||
.find({until: {$gte: now}})
|
.find({until: {$gte: now}})
|
||||||
.toArray()
|
.toArray()
|
||||||
.then(i => i.sort(sortNewsByDate));
|
.then(i => i.sort(sortNewsByDate));
|
||||||
@@ -35,7 +41,7 @@ export default function (app) {
|
|||||||
let sid = id => id + '';
|
let sid = id => id + '';
|
||||||
let pinnedIds = pinned.map(p => sid(p['_id']));
|
let pinnedIds = pinned.map(p => sid(p['_id']));
|
||||||
|
|
||||||
let posts = yield c('timeline')
|
let posts = await c('timeline')
|
||||||
.find(query)
|
.find(query)
|
||||||
.sort({date: -1})
|
.sort({date: -1})
|
||||||
.skip(skip)
|
.skip(skip)
|
||||||
@@ -45,23 +51,21 @@ export default function (app) {
|
|||||||
i => pinnedIds.indexOf(sid(i['_id'])) < 0
|
i => pinnedIds.indexOf(sid(i['_id'])) < 0
|
||||||
));
|
));
|
||||||
|
|
||||||
this.body = pinned.concat(posts);
|
ctx.body = pinned.concat(posts);
|
||||||
}
|
}
|
||||||
})));
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
app.use(route.get('/news/pinned', accepts({
|
function pinned(){
|
||||||
'text/html': function *() {
|
return route.get('/news/pinned', accepts({
|
||||||
this.redirect('/');
|
'text/html': redirect,
|
||||||
},
|
'text/plain': redirect,
|
||||||
'text/plain': function *() {
|
'application/json': async ctx => {
|
||||||
this.redirect('/');
|
|
||||||
},
|
|
||||||
'application/json': function *() {
|
|
||||||
let now = new Date();
|
let now = new Date();
|
||||||
this.body = yield c('timeline')
|
ctx.body = await c('timeline')
|
||||||
.find({until: {$gte: now}})
|
.find({until: {$gte: now}})
|
||||||
.toArray()
|
.toArray()
|
||||||
.then(i => i.sort(sortNewsByDate));
|
.then(i => i.sort(sortNewsByDate));
|
||||||
}
|
}
|
||||||
})));
|
}));
|
||||||
};
|
}
|
||||||
+39
-32
@@ -1,38 +1,45 @@
|
|||||||
|
import compose from 'koa-compose';
|
||||||
|
import co from 'co';
|
||||||
import route from 'koa-route';
|
import route from 'koa-route';
|
||||||
import {json} from './';
|
import {json} from './';
|
||||||
|
|
||||||
import {getSchedule, getScheduleList, populateSchedule} from '../core/schedule';
|
import {getSchedule, getScheduleList, populateSchedule} from '../core/schedule';
|
||||||
|
|
||||||
export default function (app) {
|
export default function () {
|
||||||
app.use(
|
return compose([
|
||||||
route.get('/schedule/list', json(
|
list(),
|
||||||
function *() {
|
schedule()
|
||||||
this.type = 'application/json';
|
]);
|
||||||
let data = yield getScheduleList();
|
|
||||||
if (!data) this.status = 404;
|
|
||||||
else this.body = data;
|
|
||||||
}
|
|
||||||
))
|
|
||||||
);
|
|
||||||
|
|
||||||
app.use(
|
|
||||||
route.get('/schedule/:period?/:semester?', json(
|
|
||||||
function *(period, semester) {
|
|
||||||
this.type = 'application/json';
|
|
||||||
|
|
||||||
let data = yield getSchedule(period, semester);
|
|
||||||
if (!data) {
|
|
||||||
this.status = 404;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let doPopulate = this.query.populate || false;
|
|
||||||
if (doPopulate) {
|
|
||||||
data.schedule = yield populateSchedule(data.schedule);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.body = data;
|
|
||||||
}
|
|
||||||
))
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function list() {
|
||||||
|
return route.get('/schedule/list', json(
|
||||||
|
async(ctx) => {
|
||||||
|
ctx.type = 'application/json';
|
||||||
|
let data = await getScheduleList();
|
||||||
|
if (!data) ctx.status = 404;
|
||||||
|
else ctx.body = data;
|
||||||
|
}
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
function schedule() {
|
||||||
|
return route.get('/schedule/:period?/:semester?', json(
|
||||||
|
async(ctx, period, semester) => {
|
||||||
|
ctx.type = 'application/json';
|
||||||
|
|
||||||
|
let data = await getSchedule(period, semester);
|
||||||
|
if (!data) {
|
||||||
|
ctx.status = 404;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let doPopulate = ctx.query.populate || false;
|
||||||
|
if (doPopulate) {
|
||||||
|
data.schedule = await populateSchedule(data.schedule);
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.body = data;
|
||||||
|
}
|
||||||
|
))
|
||||||
|
}
|
||||||
+21
-23
@@ -5,41 +5,39 @@ import {c} from '../core/db';
|
|||||||
import {homeUrl, sitemapCacheTime} from '../config';
|
import {homeUrl, sitemapCacheTime} from '../config';
|
||||||
import menu from '../models/menu';
|
import menu from '../models/menu';
|
||||||
|
|
||||||
export default function (app) {
|
export default function () {
|
||||||
app.use(route.get('/sitemap.xml', function *() {
|
return route.get('/sitemap.xml', async ctx => {
|
||||||
let urls = yield [
|
let urls = await Promise.all([
|
||||||
getMenuUrls(),
|
getMenuUrls(),
|
||||||
getGalleryUrls(),
|
getGalleryUrls(),
|
||||||
getTeacherUrls(),
|
getTeacherUrls(),
|
||||||
getEventsUrls(),
|
getEventsUrls(),
|
||||||
getNewsUrls()
|
getNewsUrls()
|
||||||
];
|
]);
|
||||||
urls = urls.reduce((urls, i) => urls.concat(i));
|
urls = urls.reduce((urls, i) => urls.concat(i));
|
||||||
|
|
||||||
let sm = sitemap.createSitemap({
|
let map = sitemap.createSitemap({
|
||||||
hostname: homeUrl,
|
hostname: homeUrl,
|
||||||
cacheTime: sitemapCacheTime,
|
cacheTime: sitemapCacheTime,
|
||||||
urls: urls
|
urls: urls
|
||||||
});
|
});
|
||||||
|
|
||||||
this.set('Content-Type', 'application/xml');
|
ctx.set('Content-Type', 'application/xml');
|
||||||
this.body = yield new Promise(resolve => sm.toXML(resolve));
|
ctx.body = map.toString();
|
||||||
}));
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
function *getMenuUrls() {
|
async function getMenuUrls(frequency='daily') {
|
||||||
return menu
|
return menu
|
||||||
.filter(i => 'url' in i)
|
.filter(i => 'url' in i)
|
||||||
.map(function (item) {
|
.map(i => ({
|
||||||
return {
|
url: i.url,
|
||||||
url: item.url,
|
changefreq: frequency
|
||||||
changefreq: 'daily'
|
}));
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function *getGalleryUrls() {
|
async function getGalleryUrls() {
|
||||||
let docs = yield c('albums')
|
let docs = await c('albums')
|
||||||
.find({})
|
.find({})
|
||||||
.toArray();
|
.toArray();
|
||||||
|
|
||||||
@@ -51,8 +49,8 @@ function *getGalleryUrls() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function *getTeacherUrls() {
|
async function getTeacherUrls() {
|
||||||
let docs = yield c('collective')
|
let docs = await c('collective')
|
||||||
.find({})
|
.find({})
|
||||||
.toArray();
|
.toArray();
|
||||||
|
|
||||||
@@ -64,8 +62,8 @@ function *getTeacherUrls() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function *getEventsUrls() {
|
async function getEventsUrls() {
|
||||||
let docs = yield c('events')
|
let docs = await c('events')
|
||||||
.find({})
|
.find({})
|
||||||
.toArray();
|
.toArray();
|
||||||
|
|
||||||
@@ -77,8 +75,8 @@ function *getEventsUrls() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function *getNewsUrls() {
|
async function getNewsUrls() {
|
||||||
let docs = yield c('timeline')
|
let docs = await c('timeline')
|
||||||
.find({type: 'post'})
|
.find({type: 'post'})
|
||||||
.toArray();
|
.toArray();
|
||||||
|
|
||||||
|
|||||||
+48
-34
@@ -1,4 +1,5 @@
|
|||||||
import co from 'co';
|
import co from 'co';
|
||||||
|
import compose from 'koa-compose';
|
||||||
import route from 'koa-route';
|
import route from 'koa-route';
|
||||||
import {index, json} from './';
|
import {index, json} from './';
|
||||||
import {sortByPattern} from '../utils/sort';
|
import {sortByPattern} from '../utils/sort';
|
||||||
@@ -7,42 +8,55 @@ import {c} from '../core/db';
|
|||||||
let split = d => s => s.split(d);
|
let split = d => s => s.split(d);
|
||||||
let toArray = split(',');
|
let toArray = split(',');
|
||||||
|
|
||||||
export default function (app) {
|
export default function () {
|
||||||
app.use(route.get('/collective', index()));
|
return compose([
|
||||||
|
collective(),
|
||||||
app.use(route.get('/teacher/list', json(
|
list(),
|
||||||
function *() {
|
teacher()
|
||||||
this.type = 'application/json';
|
]);
|
||||||
let pattern = toArray(this.query.sort || '');
|
|
||||||
|
|
||||||
let data = yield c('collective').find();
|
|
||||||
if (!data) {
|
|
||||||
this.status = 404;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let collective = yield data
|
|
||||||
.map(processProfile);
|
|
||||||
|
|
||||||
this.body = sortByPattern(collective, pattern, i => i.id);
|
|
||||||
}
|
|
||||||
)));
|
|
||||||
|
|
||||||
app.use(route.get('/teacher/:id', json(
|
|
||||||
function *(id) {
|
|
||||||
this.type = 'application/json';
|
|
||||||
|
|
||||||
let data = yield c('collective').findOne({id: id});
|
|
||||||
if (!data) {
|
|
||||||
this.status = 404;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.body = yield processProfile(data);
|
|
||||||
}
|
|
||||||
)))
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function collective() {
|
||||||
|
return route.get('/collective', index());
|
||||||
|
}
|
||||||
|
|
||||||
|
function list() {
|
||||||
|
return route.get('/teacher/list', json(
|
||||||
|
async(ctx) => {
|
||||||
|
ctx.type = 'application/json';
|
||||||
|
let pattern = toArray(ctx.query.sort || '');
|
||||||
|
|
||||||
|
let data = await c('collective').find();
|
||||||
|
if (!data) {
|
||||||
|
ctx.status = 404;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let collective = await Promise.all(
|
||||||
|
data.map(processProfile)
|
||||||
|
);
|
||||||
|
|
||||||
|
ctx.body = sortByPattern(collective, pattern, i => i.id);
|
||||||
|
}
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
function teacher() {
|
||||||
|
return route.get('/teacher/:id', json(
|
||||||
|
async (ctx, id) => {
|
||||||
|
ctx.type = 'application/json';
|
||||||
|
|
||||||
|
let data = await c('collective').findOne({id: id});
|
||||||
|
if (!data) {
|
||||||
|
ctx.status = 404;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.body = await processProfile(data);
|
||||||
|
}
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
let processProfile = profile => co(function *() {
|
let processProfile = profile => co(function *() {
|
||||||
let imageId = profile.picture;
|
let imageId = profile.picture;
|
||||||
profile.picture = yield c('images').findOne({_id: imageId});
|
profile.picture = yield c('images').findOne({_id: imageId});
|
||||||
|
|||||||
+20
-17
@@ -1,7 +1,8 @@
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import koa from 'koa';
|
import Koa from 'koa';
|
||||||
import serve from 'koa-static';
|
import serve from 'koa-static';
|
||||||
import logger from 'koa-logger';
|
import logger from 'koa-logger';
|
||||||
|
import convert from 'koa-convert';
|
||||||
import conditional from 'koa-conditional-get';
|
import conditional from 'koa-conditional-get';
|
||||||
import etag from 'koa-etag';
|
import etag from 'koa-etag';
|
||||||
import prerender from 'koa-prerender';
|
import prerender from 'koa-prerender';
|
||||||
@@ -11,40 +12,42 @@ import bodyParser from 'koa-bodyparser';
|
|||||||
import config from './config';
|
import config from './config';
|
||||||
import {redirectionTable} from './config';
|
import {redirectionTable} from './config';
|
||||||
import {routes, queryObject} from './routes';
|
import {routes, queryObject} from './routes';
|
||||||
// import api from './routes/api';
|
import api from './routes/api';
|
||||||
import {redirect} from './routes/redirect';
|
import {redirect} from './routes/redirect';
|
||||||
|
|
||||||
const dirPublic = path.join(__dirname, '../public');
|
const dirPublic = path.join(__dirname, '../public');
|
||||||
const dirTemplates = path.join(__dirname, 'templates');
|
const dirTemplates = path.join(__dirname, 'templates');
|
||||||
|
|
||||||
export function server(){
|
export function server(){
|
||||||
const app = koa();
|
const $ = convert;
|
||||||
|
|
||||||
|
const app = new Koa();
|
||||||
app.proxy = true;
|
app.proxy = true;
|
||||||
|
|
||||||
app.use(bodyParser());
|
app.use(bodyParser());
|
||||||
app.use(logger());
|
app.use($(logger()));
|
||||||
// api(app);
|
app.use(api());
|
||||||
app.use(conditional());
|
app.use($(conditional()));
|
||||||
app.use(etag());
|
app.use($(etag()));
|
||||||
app.use(prerenderRmFragment());
|
app.use(prerenderRmFragment());
|
||||||
app.use(prerender(config.prerender));
|
app.use($(prerender(config.prerender)));
|
||||||
app.use(serve(dirPublic));
|
app.use($(serve(dirPublic)));
|
||||||
app.use(serve(dirTemplates));
|
app.use($(serve(dirTemplates)));
|
||||||
app.use(redirect(redirectionTable));
|
app.use($(redirect(redirectionTable)));
|
||||||
app.use(helmet());
|
app.use($(helmet()));
|
||||||
app.use(queryObject());
|
app.use(queryObject());
|
||||||
routes(app);
|
app.use(routes());
|
||||||
|
|
||||||
return app;
|
return app;
|
||||||
}
|
}
|
||||||
|
|
||||||
function prerenderRmFragment(){
|
function prerenderRmFragment(){
|
||||||
return function *(next) {
|
return async (ctx, next) => {
|
||||||
yield* next;
|
await next();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let xp = this.response.header['x-prerender'] == 'true';
|
let xp = ctx.response.header['x-prerender'] == 'true';
|
||||||
if (xp) this.body = this.body.replace('<meta name="fragment" content="!">', '');
|
if (xp) ctx.body = ctx.body.replace('<meta name="fragment" content="!">', '');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user