Skip to content

Commit

Permalink
Added support for Redis as session store (#4)
Browse files Browse the repository at this point in the history
* Added support for Redis as session store (via env variable SESSION_STORE_TYPE). Fixes Haufe-Lexware/wicked.haufe.io#83

* Added connect-redis to package.json. See Haufe-Lexware/wicked.haufe.io#83

* Redis sessions - read configuration from globals instead of directly from env vars.

* Better support for feature branch building.
  • Loading branch information
achwie authored and DonMartin76 committed Sep 15, 2017
1 parent b99728d commit 799959d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.template
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
FROM ${DOCKER_PREFIX}portal-env:${DOCKER_TAG}-onbuild${BUILD_ALPINE}
FROM ${DOCKER_PREFIX}portal-env:${DOCKER_ENV_TAG}${BUILD_ALPINE}

EXPOSE 3000
42 changes: 25 additions & 17 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,6 @@ var correlationIdHandler = wicked.correlationIdHandler();
var passport = require('passport');
var fs = require('fs');
var session = require('express-session');
var FileStore = require('session-file-store')(session);

// Use default options, see https://www.npmjs.com/package/session-file-store
var sessionStoreOptions = {};

var SECRET = 'ThisIsASecret';

// Session: 15 minutes
var sessionArgs = {
store: new FileStore(sessionStoreOptions),
secret: SECRET,
saveUninitialized: true,
resave: false,
cookie: {
maxAge: 15 * 60 * 1000
}
};

var app = express();
app.initialized = false;
Expand Down Expand Up @@ -100,6 +83,22 @@ app.use(bodyParser.urlencoded({ extended: false }));
app.initialize = function (done) {
debug('initialize()');

// The session type is configured via the globals.json sessionStore property,
// This is why this has to go in here instead of in the above initialization.
var sessionStore = require('./sessionstore')(app.portalGlobals, session);
var SECRET = 'ThisIsASecret';

// Session: 15 minutes
var sessionArgs = {
store: sessionStore,
secret: SECRET,
saveUninitialized: true,
resave: false,
cookie: {
maxAge: 15 * 60 * 1000
}
};

if (!wicked.isDevelopmentMode()) {
app.isProduction = true;
app.set('trust proxy', 1);
Expand All @@ -122,6 +121,15 @@ app.initialize = function (done) {

app.use(cookieParser(SECRET));
app.use(session(sessionArgs));
// Session checker middleware
app.use(function (req, res, next) {
if (!req.session) {
var err = new Error('Session not found (redis not available?)');
err.status = 500;
return next(err);
}
next(); // otherwise continue
});
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
Expand Down
13 changes: 13 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ if [ -z "$DOCKER_TAG" ]; then
export DOCKER_TAG=dev
fi

if [[ "haufelexware/wicked." == "$DOCKER_PREFIX" ]] && [[ "$1" == "--push" ]]; then
echo "INFO: Resolving portal-env base tag for target tag ${DOCKER_TAG}..."
docker pull haufelexware/wicked.portal-env:next-onbuild-alpine
export DOCKER_ENV_TAG=$(docker run --rm haufelexware/wicked.portal-env:next-onbuild-alpine node node_modules/portal-env/getMatchingTag.js haufelexware wicked.portal-env ${DOCKER_TAG})
if [ -z "$DOCKER_ENV_TAG" ]; then
echo "ERROR: Could not resolve portal-env base tag!"
exit 1
fi
else
export DOCKER_ENV_TAG=${DOCKER_TAG}-onbuild
fi

echo "INFO: Using base image tag ${DOCKER_ENV_TAG}"
echo "============================================"
echo "Building normal image..."
echo "============================================"
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"bootstrap": "3.3.7",
"chai": "3.5.0",
"connect-flash": "0.1.1",
"connect-redis": "3.3.0",
"cookie-parser": "1.4.3",
"cors": "2.7.1",
"csurf": "1.9.0",
Expand Down
53 changes: 0 additions & 53 deletions run-integration-tests.sh

This file was deleted.

37 changes: 37 additions & 0 deletions sessionstore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var debug = require('debug')('portal:sessionstore');

function initSessionStore(globals, session){
let sessionStoreType = 'file';
if (globals.sessionStore && globals.sessionStore.type) {
sessionStoreType = globals.sessionStore.type;
} else {
console.error('WARNING: Missing sessionStore global property, defaulting to file session store - THIS WILL NOT SCALE.');
globals.sessionStore = { type: 'file' };
}
debug('SESSION_STORE_TYPE: ' + sessionStoreType);

var sessionStoreOptions = {};
var SessionStore;
switch (sessionStoreType){
case 'file':
SessionStore = require('session-file-store')(session);
// Use default options for file session store, see https://www.npmjs.com/package/session-file-store
break;
case 'redis':
SessionStore = require('connect-redis')(session);
// Set options for Redis session store, see https://www.npmjs.com/package/connect-redis
sessionStoreOptions.host = globals.sessionStore.host || 'portal-redis';
sessionStoreOptions.port = globals.sessionStore.port || 6379;
if (globals.sessionStore.password)
sessionStoreOptions.pass = globals.sessionStore.password;
break;
default:
throw new Error("Invalid session-store type: '" + sessionStoreType + "'");
}

debug('Using session store \'' + sessionStoreType + '\' with options ' + JSON.stringify(sessionStoreOptions));

return new SessionStore(sessionStoreOptions);
}

module.exports = initSessionStore;

0 comments on commit 799959d

Please sign in to comment.