Ignore:
Timestamp:
06/23/13 13:59:15 (12 years ago)
Author:
hendrikvanantwerpen
Message:

Working towards Heroku deployment, restructuring for that and to allow coffee builds.

File:
1 moved

Legend:

Unmodified
Added
Removed
  • Dev/trunk/src/server/app.js

    r462 r463  
    11var express = require("express");
     2var passport = require("passport"),
     3    passportLocal = require("passport-local");
    24var fs = require("fs");
    3 var https = require("https");
    4 var os = require("os");
    55var path = require("path");
    66var proxy = require("simple-http-proxy");
    77var _ = require("underscore");
    88
    9 function clientPath(relativePath) {
    10     return path.resolve('../client/'+relativePath);
     9function assertSetting(name, settings, validate) {
     10    if ( typeof settings[name] === 'undefined' ) {
     11        throw new Error("Required setting '"+name+"' undefined.");
     12    }
     13    if ( _.isFunction(validate) && !validate(settings[name]) ) {
     14        throw new Error("Setting '"+name+"' with value '"+settings[name]+"' is invalid.");
     15    }
    1116}
    1217
    13 var httpsOptions = {
    14     key: fs.readFileSync(path.resolve('../qed-server.key')),
    15     cert: fs.readFileSync(path.resolve('../qed-server.pem'))
     18exports.App = function(settings) {
     19
     20    assertSetting("couchDbURL", settings, _.isString);
     21   
     22    function clientPath(relativePath) {
     23        return path.resolve(__dirname+'/../client/'+relativePath);
     24    }
     25
     26    passport.use(new passportLocal.Strategy(function(username, password, done){
     27        if ( username === "igor" && password === "mayer" ) {
     28            done(null,{ username: "igor" });
     29        } else {
     30            done(null,false,{ message: 'Invalid credentials.' });
     31        }
     32    }));
     33
     34    var app = express();
     35    app.use(express.logger());
     36    app.use(express.compress());
     37    app.use(express.favicon());
     38
     39    app.use(express.cookieParser());
     40    app.use(express.bodyParser());
     41    app.use(express.session({ secret: "quasi experimental design" }));
     42
     43    // initialize passport
     44    app.use(passport.initialize());
     45    app.use(passport.session());
     46
     47    // static resources
     48    app.get('/', function(request, response){
     49        response.sendfile(clientPath('index.html'));
     50    });
     51    app.get('/*.html', function(request, response) {
     52        response.sendfile(clientPath(request.path));
     53    });
     54    _.each(['/dojo', '/dijit', '/dojox', '/qed', '/qed-client'], function(dir){
     55        app.use(dir, express.static(clientPath(dir)));
     56    });
     57
     58    // url to login (might work on others as well?)
     59    // you should then have a session to work with
     60    app.post('/api/login', passport.authenticate('local'));
     61
     62    // forward to couch
     63    app.use('/data/couch', passport.authenticate('local'), proxy(settings.couchDbURL));
     64
     65    return app;
     66
    1667};
    17 
    18 var app = express();
    19 app.use(express.logger());
    20 app.use(express.compress());
    21 app.use(express.favicon());
    22 app.get('/', function(request, response){
    23     response.sendfile(clientPath('index.html'));
    24 });
    25 app.get('/*.html', function(request, response) {
    26     response.sendfile(clientPath(request.path));
    27 });
    28 _.each(['/dojo', '/dijit', '/dojox', '/qed', '/qed-client'], function(dir){
    29     app.use(dir, express.static(clientPath(dir)));
    30 });
    31 app.use('/data/couch', proxy('http://localhost:5984/qed'));
    32 
    33 var server = https.createServer(httpsOptions, app);
    34 server.listen(8443);
    35 
    36 console.log(
    37     "Listening on " +
    38     _.chain(os.networkInterfaces())
    39      .map(function(value,key){ return value; })
    40      .flatten()
    41      .filter(function(intf){ return intf.family === "IPv4" && !intf.internal; })
    42      .reduce(function(urls, intf){ return urls+', http://'+intf.address+':8443/'; }, "http://127.0.0.1:8443/")
    43      .value() + ".");
Note: See TracChangeset for help on using the changeset viewer.