Skip to content

Commit

Permalink
update(server): remove Redis, clustering. Convert session storage to …
Browse files Browse the repository at this point in the history
…Mongo.

upgrade to latest Express and related libraries
convert tasks to be triggered directly instead of via Redis+Clustering
convert cron to call new tasks
remove deprecated express-annotations
remove API docs since they depended upon express-annotations
convert rate metering to in-memory only since Redis is no longer an option
temporarily disable CSRF protection

Fixes gdg-x#96 gdg-x#80
  • Loading branch information
Splaktar committed Mar 17, 2017
1 parent 651b9f2 commit bbaed00
Show file tree
Hide file tree
Showing 19 changed files with 198 additions and 717 deletions.
8 changes: 2 additions & 6 deletions app/developer/developer.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

angular.module('gdgxHubApp').controller('DeveloperCtrl', DeveloperCtrl);

DeveloperCtrl.$inject = ['$scope', '$http'];
DeveloperCtrl.$inject = [];

function DeveloperCtrl($scope, $http) {
$http.get('/api/v1/rest').success(function(data) {
$scope.restDiscovery = data;
});
}
function DeveloperCtrl() {}
63 changes: 29 additions & 34 deletions app/developer/developer.html
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
<div class="container">
<div class="row">
<div class="span12">
<h2>API Specification</h2>
<div class="card">
<div class="card-wh-body">
{{restDiscovery.title}} Version: {{restDiscovery.version}}, BaseURL: {{restDiscovery.baseUrl}}<br/>
Status: <b>{{restDiscovery.status}}</b>
</div>
</div>
<div class="card">
<div class="card-wh-body">
<ul>
<li>All Routes support JSONP (just add your callback in the query, e.g. ?callback=myfunc)</li>
<li>Simple API Keys, OAuth &amp; will be introduced later.</li>
<li>The API is rate limited. Please check the X-RateLimit-* headers for details</li>
<li>API Responses might be cached. Check the X-Cacher-Hit header.</li>
<li>Please file any bug reports and/or feature requests at
<div class="row">
<div class="span12">
<h2>API Specification</h2>
<div class="card">
<div class="card-wh-body">
GDG-X Hub API Version: v1, BaseURL: https://hub.gdgx.io/api/v1/<br/>
Status: <b>alpha</b>
</div>
</div>
<div class="card">
<div class="card-wh-body">
<ul>
<li>All Routes support JSONP (just add your callback in the query, e.g. ?callback=myfunc)</li>
<li>Simple API Keys, OAuth &amp; will be introduced later.</li>
<li>The API is rate limited. Please check the X-RateLimit-* headers for details</li>
<li>API Responses might be cached. Check the X-Cacher-Hit header.</li>
<li>Please file any bug reports and/or feature requests at
<a href="https://github.com/gdg-x/hub/issues">https://github.com/gdg-x/hub/issues</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="row">
<div ng-repeat="route in restDiscovery.resources.url.methods" class="span6 card" style="min-height: 105px">
<div class="card-wh-body">
<div class="clearfix">
<span ng-class="route.method">{{route.method|uppercase}}</span>
<div class="pull-right"><b>{{route.path}}</b></div>
</div>
<br/>
<div class="pull-right" style="text-align: right; margin-bottom: 10px;">{{route.doc.summary}}</div>
</div>
</div>
</div>
</ul>
</div>
</div>
</div>
</div>
<div class="row">
<div class="span12 card">
<div class="card-wh-body" style="margin: 12px 32px 32px 32px; font-size: 24px">
Google Cloud Endpoints Support via OpenAPI Spec Coming Soon!
</div>
</div>
</div>
</div>
5 changes: 0 additions & 5 deletions lib/config/env/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,5 @@ module.exports = {
sender: 'GDG[x] Hub <[email protected]>',
transport: 'Direct',
error_recipient: process.env.ADMIN_EMAIL // jshint ignore:line
},
redis: {
host: process.env.REDIS_DB_HOST,
port: process.env.REDIS_DB_PORT,
password: process.env.REDIS_DB_PASSWORD
}
};
99 changes: 41 additions & 58 deletions lib/config/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,23 @@
var express = require('express'),
cookieParser = require('cookie-parser'),
session = require('express-session'),
methodOverride = require('method-override'),
favicon = require('serve-favicon'),
path = require('path'),
passport = require('passport'),
csrf = require('csrf'),
config = require('./config'),
redis = require('redis'),
RedisStore = require('connect-redis')(session);
csrf = require('csurf'),
config = require('./config');
var errorhandler = require('errorhandler');
var morgan = require('morgan');
var compression = require('compression');
var bodyParser = require('body-parser');
const MongoStore = require('connect-mongo')(session);

/**
* Express configuration
*/
module.exports = function (app) {

var redisClient;

if (config.redis) {
console.log('Using redis for Express...');
redisClient = redis.createClient(config.redis.port, config.redis.host);

if (config.redis.password) {
redisClient.auth(config.redis.password);
}

redisClient.on('ready', function () {
console.log('Redis is ready for use with Express.');
});
redisClient.on('error', function (err) {
console.error('ERR:REDIS:Express: ' + err);
});
}

app.configure('development', function () {
if (process.env.NODE_ENV !== 'production') {
app.use(require('connect-livereload')());

// Disable caching of scripts for easier testing
Expand All @@ -48,46 +34,43 @@ module.exports = function (app) {

app.use(express.static(path.join(config.root, '.tmp')));
app.use(express.static(path.join(config.root, 'app')));
app.use(express.errorHandler());
app.use(errorhandler());
app.set('views', config.root + '/app/views');

});
app.use(session({
cookie: { secure: false },
resave: false,
saveUninitialized: true,
store: new MongoStore({ url: process.env.MONGODB_DB_URL }),
secret: config.sessionSecret
}));
}

app.configure('production', function () {
app.use(express.favicon(path.join(config.root, 'public', 'favicon.ico')));
app.use(express.compress());
if (process.env.NODE_ENV === 'production') {
app.use(favicon(path.join(config.root, 'public', 'favicon.ico'), {}));
app.use(compression());
app.use(express.static(path.join(config.root, 'public'), {maxAge: 604800000}));
app.set('views', config.root + '/views');
});

app.configure(function () {
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(express.logger('[' + process.pid + '] :method :url :status :response-time ms - :res[content-length]'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(cookieParser());
app.use(csrf({cookie: true}));

if (config.redis) {
app.use(express.session({
store: new RedisStore({
client: redisClient
}),
secret: config.sessionSecret
}));
} else {
app.use(express.session({
secret: config.sessionSecret
}));
}
app.use(session({
cookie: { secure: true },
resave: false,
saveUninitialized: true,
store: new MongoStore({ url: process.env.MONGODB_DB_URL }),
secret: config.sessionSecret
}));
}

// Passport
app.use(passport.initialize());
app.use(passport.session());
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(morgan('[' + process.pid + '] :method :url :status :response-time ms - :res[content-length]'));
app.use(bodyParser.json({}));
app.use(bodyParser.urlencoded({ extended: false}));
app.use(methodOverride());
app.use(cookieParser());
// app.use(csrf({cookie: true}));

// Router needs to be last
app.use(app.router);
});
// Passport
app.use(passport.initialize());
app.use(passport.session());
};
48 changes: 0 additions & 48 deletions lib/controllers/api/documentation.js

This file was deleted.

49 changes: 6 additions & 43 deletions lib/controllers/api/index.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,27 @@
'use strict';

var express = require('express'),
documentation = require('./documentation.js'),
redis = require('redis'),
rate = require('express-rate'),
config = require('../../config/config'),
mongoose = require('mongoose'),
SimpleApiKey = mongoose.model('SimpleApiKey'),
annotations = require('express-annotations'),
Cacher = require('cacher'),
uuid = require('node-uuid'),
utils = require('./utils'),
request = require('superagent'),
CacherRedis = require('cacher-redis');
request = require('superagent');

module.exports = function (app) {
const EDGE_CACHE_MAX_AGE = 3600; // 1 hr
var versions = [];
var rateHandler;
var cacher;
var redisClient;

utils.fixCacher(Cacher);

if (config.redis) {
console.log('Using redis for API support...');

// Setup Redis client for API Rate limiting
redisClient = redis.createClient(config.redis.port, config.redis.host);

if (config.redis.password) {
redisClient.auth(config.redis.password);
}

redisClient.on('ready', function () {
console.log('Redis Client for API support is ready.');
});
redisClient.on('error', function (err) {
console.error('ERR:REDIS:Cacher: ' + err);
});

rateHandler = new rate.Redis.RedisRateHandler({client: redisClient});

cacher = new Cacher(new CacherRedis(redisClient));
} else {
console.log('Using in-memory');

// Fallback to In-Memory handler if Redis is not available
rateHandler = new rate.Memory.MemoryRateHandler();

// In-Memory Cache
cacher = new Cacher();
}
console.log('Using in-memory rate handler...');
rateHandler = new rate.Memory.MemoryRateHandler();
// In-Memory Cache
cacher = new Cacher();

var analyticsMiddleware = function (version) {
return function (req, res, next) {
Expand Down Expand Up @@ -151,20 +121,15 @@ module.exports = function (app) {
versions.push(version);

var impl = express();
annotations.extend(impl);

impl.use(apiKeyMiddleware);
impl.use(rateMiddleware);
impl.use(analyticsMiddleware(version));
impl.use(edgeCache);

impl.route = function (method, path, metadata) {
impl.route = function (method, path) {
var args = Array.prototype.slice.call(arguments);

if (metadata) {
impl.annotate(method + '-' + path, metadata);
}

impl[method](path, args.slice(3));
};

Expand All @@ -174,8 +139,6 @@ module.exports = function (app) {

require('./' + file)(impl, cacher);

documentation(version, app, impl);

app.get('/api/', function (req, res) {
res.redirect('/developers/api');
});
Expand Down
Loading

0 comments on commit bbaed00

Please sign in to comment.