1 | var express = require("express"); |
---|
2 | var passport = require("passport"), |
---|
3 | passportLocal = require("passport-local"); |
---|
4 | var fs = require("fs"); |
---|
5 | var path = require("path"); |
---|
6 | var proxy = require("./simple-http-proxy"); |
---|
7 | var _ = require("underscore"); |
---|
8 | |
---|
9 | function 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 | } |
---|
16 | } |
---|
17 | |
---|
18 | exports.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'); |
---|
61 | |
---|
62 | // forward to couch |
---|
63 | app.use('/data/couch', proxy(settings.couchDbURL)); |
---|
64 | |
---|
65 | return app; |
---|
66 | |
---|
67 | }; |
---|