source: Dev/trunk/src/server/config/config-couchdb.js @ 468

Last change on this file since 468 was 468, checked in by hendrikvanantwerpen, 12 years ago

Improved authentication

  • Authentication cannot be cancelled, removing a lot of weird race conditions.
  • We offer a request provider that has automatic retry in case of authentication failures.
  • Reduced dependency on LoginDialog? by having it act independent based on events. Other modules can just depend on 'session'.
File size: 3.4 KB
RevLine 
[468]1var q = require('q'),
2    request = require('../util/q-request'),
3    _ = require('underscore'),
4    util = require('util');
[455]5
6var designDocs = require('./couchdb-design-docs');
7
[464]8module.exports = function(couchDbURL) {
9
10    function stringifyFunctions(value) {
11        if ( value === null ) {
12            return null;
13        } else if ( _.isArray(value) ) {
14            return value;
15        } else if ( _.isFunction(value) ) {
16            return value.toString();
17        } else if ( _.isObject(value) ) {
18            value = _.clone(value);
19            _.each(value, function(propValue, propName){
20                value[propName] = stringifyFunctions(propValue);
21            });
22            return value;
23        } else {
24            return value;
25        }
[455]26    }
27
[466]28    function dbRequest(method,path,content) {
[464]29        var url = couchDbURL+path;
30        var options = {
31            method: method,
32            headers: {
33                'content-type': 'application/json; charset=utf-8',
34                'accept': 'application/json'
35            },
[468]36            body: JSON.stringify(content)
[464]37        };
[468]38        //console.log('req',url,options);
39        return request(url,options).then(function(res){
40            return JSON.parse(res);
41        }, function(err){
42            return JSON.parse(err);
43        });
[464]44    }
[455]45
[464]46    console.log("Configuring CouchDB for QED");
47    console.log("Checking CouchDB version");
[466]48    return dbRequest('GET','')
[455]49    .then(function(res){
[464]50        if (res.version !== "1.2.0" ) {
51            console.log("Found "+res.version+", only tested with CouchDB 1.2.0");
52        } else {
53            console.log("CouchDB 1.2.0 found");
54        }
55    }).then(function(res){
56        console.log("Checking database 'qed'");
[466]57        return dbRequest('GET','qed')
[464]58        .then(function(res){
59            console.log("Database 'qed' found.");
60        },function(err){
61            console.log("Creating database 'qed'");
[466]62            return dbRequest('PUT','qed');
[464]63        });
64    }).then(function(){
65        console.log("Putting documents in database.");
66        designDocs = stringifyFunctions(designDocs);
67        return q.all(_.map(designDocs, function(doc,docUrl){
68            var configAction = doc.__configAction;
69            delete doc.__configAction;
70            switch (configAction) {
71                case "ignore":
72                    console.log(docUrl+" ignored.");
73                break;
74                case "update":
75                    console.log(docUrl+" updating.");
[466]76                    return dbRequest('GET',docUrl)
[464]77                    .then(function(oldDoc){
78                        _.extend(oldDoc,doc);
[466]79                        return dbRequest('PUT',docUrl,oldDoc);
[464]80                    },function(){
[466]81                        return dbRequest('PUT',docUrl,doc);
[464]82                    });
83                break;
84                case "replace":
85                default:
86                    console.log(docUrl+" replacing.");
[466]87                    return dbRequest('GET',docUrl)
[464]88                    .then(function(oldDoc){
89                        _.extend(doc,_.pick(oldDoc,'_id','_rev'));
[466]90                        return dbRequest('PUT',docUrl,doc);
[464]91                    },function(){
[466]92                        return dbRequest('PUT',docUrl,doc);
[464]93                    });
94                break;
95            }
96        }));
[468]97    }).then(function(results){
[464]98        console.log("Done!");
[455]99    },function(err){
[464]100        console.error("ERROR",err,err.stack);
[455]101    });
[464]102
103};
Note: See TracBrowser for help on using the repository browser.