source files -> src/ folder

This commit is contained in:
tmshv committed 2016-01-09 17:26:44 +03:00
1 parent f006207112
commit d25f4439fa
144 files changed

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
/**
* Created by tmshv on 01/03/15.
*/
var route = require("koa-route");
var router = require("./");
module.exports = function (app) {
app.use(function *(next) {
yield next;
if (404 != this.status) return;
this.status = 404;
yield router.accepts({
"text/html": router.index(),
"text/plain": function *() {
this.type = "text/plain";
this.body = "Page not found";
},
"application/json": function *() {
this.type = "application/json";
this.body = {
message: "Not Found"
};
}
});
});
};
+83
View File
@@ -0,0 +1,83 @@
/**
* Created by tmshv on 22/11/14.
*/
var co = require("co");
var route = require("koa-route");
var router = require("./");
var db = require("../core/db");
module.exports = function (app) {
app.use(route.get("/gallery", router.accepts({
"text/html": router.index(),
"text/plain": router.index(),
"application/json": function *() {
var query = {};
var albums = yield db.c("albums")
.find(query)
.toArray();
albums = yield albums.map(processAlbum);
this.body = albums;
}
})));
app.use(route.get("/gallery/:year", router.accepts({
"text/html": router.index(),
"text/plain": router.index(),
"application/json": function *(year) {
year = parseInt(year);
var start = new Date(year, 0, 1);
var end = new Date(year, 11, 31);
var query = {
date: {$gte: start, $lt: end}
};
var albums = yield db.c("albums")
.find(query)
.toArray();
albums = yield albums.map(processAlbum);
this.body = albums;
}
})));
app.use(route.get("/gallery/:year/:course/:album", router.accepts({
"text/html": router.index(),
"text/plain": router.index(),
"application/json": function *(year, course, album) {
year = parseInt(year);
course = '/' + course;
album = '/' + album;
var start = new Date(year, 0, 1);
var end = new Date(year, 11, 31);
var query = {
date: {$gte: start, $lt: end},
course_uri: course,
uri: album
};
var a = yield db.c("albums").findOne(query);
a = yield processAlbum(a);
this.body = a;
}
})));
};
function processAlbum(album) {
return co(function *() {
album.url = '/gallery/{year}{course}{album}'
.replace('{year}', album.date.getFullYear())
.replace('{course}', album.course_uri)
.replace('{album}', album.uri);
album.content = yield album.content.map(function (product_id) {
return co(function *() {
var product = yield db.c("products").findOne({_id: product_id});
if (product) return product;
else return null;
});
});
return album;
});
}
+40
View File
@@ -0,0 +1,40 @@
/**
* Created by tmshv on 01/03/15.
*/
var path = require("path");
var fs = require("mz/fs");
var config = require("../config");
function accepts(routes, def) {
return function *() {
var req = this.request;
var types = Object.keys(routes)
.filter(function (type) {
return req.accepts(type);
});
if (types.length) {
var type = types[0];
yield routes[type].apply(this, arguments);
} else if (def) {
yield def;
} else {
this.status = 406;
}
};
}
function index(filename) {
if (!filename) filename = config['defaultIndex'];
return function *() {
this.type = "text/html";
this.body = fs.createReadStream(filename);
}
}
module.exports = {
index: index,
accepts: accepts
};
+92
View File
@@ -0,0 +1,92 @@
/**
* Created by tmshv on 22/11/14.
*/
var route = require("koa-route");
var router = require("./");
var db = require("../core/db");
module.exports = function (app) {
app.use(route.get("/news", router.accepts({
"text/html": function *() {
this.redirect("/");
},
"text/plain": function *() {
this.redirect("/");
},
"application/json": function *() {
var count = parseInt(this.query["count"]) || 10;
var portion = parseInt(this.query["portion"]) || 0;
var skip = portion * count;
var query = {};
if (this.query.type) query.type = this.query.type;
var pinned = [];
if(portion == 0) {
var now = new Date();
pinned = yield db.c("posts").find({until: {$gte: now}}).toArray()
.then(function (items) {
return items.sort(sortNewsByDate);
});
}
var pinned_ids = pinned.map(function (p) {
return p["_id"]+"";
});
var posts = yield db.c("posts").find(query)
.sort({date: -1})
.skip(skip)
.limit(count)
.toArray()
.then(function (items) {
return items.filter(function (i) {
var id = i["_id"]+"";
return pinned_ids.indexOf(id) < 0;
});
});
this.body = pinned.concat(posts);
//this.body = pinned_ids
}
})));
app.use(route.get("/news/pinned", router.accepts({
"text/html": function *() {
this.redirect("/");
},
"text/plain": function *() {
this.redirect("/");
},
"application/json": function *() {
var now = new Date();
var pinned = yield db.c("posts").find({until: {$gte: now}}).toArray()
.then(function (items) {
return items.sort(sortNewsByDate);
});
this.body = pinned;
}
})));
app.use(route.get("/news/:uri", router.accepts({
"text/html": router.index(),
"text/plain": router.index(),
"application/json": function *(uri) {
var item = yield db.c("posts").findOne({uri: uri});
if (item) {
this.body = item;
} else {
this.status = 404;
}
}
})));
};
function sortNewsByDate(i1, i2) {
var t1 = new Date(i1.date).getTime();
var t2 = new Date(i2.date).getTime();
if (t1 < t2) return 1;
if (t1 > t2) return -1;
return 0;
}
+63
View File
@@ -0,0 +1,63 @@
/**
* Created by tmshv on 22/11/14.
*/
var route = require("koa-route");
var router = require("./");
var db = require("../core/db");
var schedule = require("../models/schedule");
var populate = require("../utils/populate").populate;
const course = require("../models/course");
const team = require("../models/team");
module.exports = function (app) {
app.use(
route.get("/schedule/:period?/:semester?", router.accepts(
{
"text/html": router.index(),
"text/plain": router.index(),
"application/json": function *(period, semester) {
this.type = "application/json";
var doPopulate = this.query.populate || false;
var data = yield db.c("schedules").findOne({
period: period,
semester: semester
});
if(!data) {
this.status = 404;
return;
}
if(doPopulate) {
data.schedule = schedule.populate(data.schedule, [
populate(team.short, "teacher"),
populate(course.name, "lesson")
]);
}
this.body = data;
}
}
))
);
app.use(
route.get("/schedules", router.accepts(
{
"text/html": router.index(),
"text/plain": router.index(),
"application/json": function *() {
this.type = "application/json";
var data = yield db.c("schedules").find({}, {period:1 , semester: 1}).toArray();
if(!data) this.status = 404;
else this.body = data;
}
}
))
);
};
+57
View File
@@ -0,0 +1,57 @@
/**
* Created by tmshv on 22/11/14.
*/
var co = require("co");
var sitemap = require("sitemap");
var route = require("koa-route");
var router = require("./");
var db = require("../core/db");
module.exports = function (app) {
app.use(route.get("/sitemap.xml", function *() {
var urls = yield [menu(), gallery()];
urls = urls.reduce(function (urls, i) {
return urls.concat(i);
});
var sm = sitemap.createSitemap({
hostname: 'http://art.shlisselburg.org',
cacheTime: 600000, // 600 sec - cache purge period
urls: urls
});
var data = yield new Promise(function (resolve) {
sm.toXML(resolve);
});
this.set('Content-Type', 'application/xml');
this.body = data;
}));
};
function *menu(){
return require('../models/menu.json')
.filter(function(item){
return 'url' in item
})
.map(function (item) {
return {
url: item.url,
changefreq: 'daily'
}
});
}
function *gallery(){
var albums = yield db.c("albums")
.find({})
.toArray();
return albums.map(function (album) {
return {
url: `/gallery/${album.date.getFullYear()}${album.course_uri}${album.uri}`,
changefreq: 'monthly'
}
});
}