v3: port static pages, make routes lib more flexible, share middlewares between v2 and v3, port v1, simplify server.js

This commit is contained in:
Matteo Pagliazzi
2016-03-30 17:20:01 +02:00
parent e54bd0f364
commit 6cbbdcdcbe
24 changed files with 420 additions and 267 deletions
+21
View File
@@ -0,0 +1,21 @@
let api = {};
/**
* @api {get} /status Get Habitica's status
* @apiVersion 3.0.0
* @apiName GetStatus
* @apiGroup Status
*
* @apiSuccess {status} string 'up' if everything is ok
*/
api.getStatus = {
method: 'GET',
url: '/status',
async handler (req, res) {
res.respond(200, {
status: 'up',
});
},
};
module.exports = api;
+73
View File
@@ -0,0 +1,73 @@
import locals from '../middlewares/api-v3/locals';
import getUserLanguage from '../middlewares/api-v3/getUserLanguage';
import _ from 'lodash';
const marked = require('marked');
let api = {};
const TOTAL_USER_COUNT = '1,100,000';
api.getFrontPage = {
method: 'GET',
url: '/',
middlewares: [getUserLanguage, locals],
async handler (req, res) {
if (!req.header('x-api-user') && !req.header('x-api-key') && !(req.session && req.session.userId)) {
return res.redirect('/static/front');
}
res.render('index.jade', {
title: 'Habitica | Your Life The Role Playing Game',
env: res.locals.habitrpg,
});
},
};
let staticPages = ['front', 'privacy', 'terms', 'api', 'features',
'videos', 'contact', 'plans', 'new-stuff', 'community-guidelines',
'old-news', 'press-kit', 'faq', 'overview', 'apps',
'clear-browser-data', 'merch'];
_.each(staticPages, (name) => {
api[`get${name}Page`] = {
method: 'GET',
url: `/static/${name}`,
middlewares: [getUserLanguage, locals],
async handler (req, res) {
res.render(`static/${name}.jade`, {
env: res.locals.habitrpg,
marked: marked,
userCount: TOTAL_USER_COUNT
});
},
};
});
let shareables = ['level-up', 'hatch-pet', 'raise-pet', 'unlock-quest', 'won-challenge', 'achievement'];
_.each(shareables, (name) => {
api[`get${name}ShareablePage`] = {
method: 'GET',
url: `/social/${name}`,
middlewares: [getUserLanguage, locals],
async handler (req, res) {
res.render(`social/${name}`, {
env: res.locals.habitrpg,
marked: marked,
userCount: TOTAL_USER_COUNT
});
},
};
});
api.redirectExtensionsPage = {
method: 'GET',
url: '/static/extensions',
async handler (req, res) {
res.redirect('http://habitica.wikia.com/wiki/App_and_Extension_Integrations');
},
};
module.exports = api;