Changeset 464 for Dev/trunk/src/server/config
- Timestamp:
- 06/23/13 21:46:11 (12 years ago)
- Location:
- Dev/trunk/src/server/config
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/trunk/src/server/config/config-couchdb.js
r460 r464 1 1 var q = require('q'); 2 var http= require('q-io/http');3 var fs = require('q-io/fs');2 var HTTP = require('q-io/http'); 3 var URL = require('url'); 4 4 var _ = require('underscore'); 5 var util = require('util'); 5 6 6 var dbURL = "http://localhost:5984/";7 7 var designDocs = require('./couchdb-design-docs'); 8 8 9 function stringifyFunctions(value) { 10 if ( value === null ) { 11 return null; 12 } else if ( _.isArray(value) ) { 13 return value; 14 } else if ( _.isFunction(value) ) { 15 return value.toString(); 16 } else if ( _.isObject(value) ) { 17 value = _.clone(value); 18 _.each(value, function(propValue, propName){ 19 value[propName] = stringifyFunctions(propValue); 9 module.exports = function(couchDbURL) { 10 11 function stringifyFunctions(value) { 12 if ( value === null ) { 13 return null; 14 } else if ( _.isArray(value) ) { 15 return value; 16 } else if ( _.isFunction(value) ) { 17 return value.toString(); 18 } else if ( _.isObject(value) ) { 19 value = _.clone(value); 20 _.each(value, function(propValue, propName){ 21 value[propName] = stringifyFunctions(propValue); 22 }); 23 return value; 24 } else { 25 return value; 26 } 27 } 28 29 function request(method,path,content) { 30 var url = couchDbURL+path; 31 var parsedURL = URL.parse(url); 32 var options = { 33 url: url, 34 method: method, 35 headers: { 36 'content-type': 'application/json; charset=utf-8', 37 'accept': 'application/json' 38 }, 39 body: { 40 forEach: function(callback) { 41 callback(JSON.stringify(content || {})); 42 } 43 } 44 }; 45 // because q-io doesn't support auth properly, we have to 46 // build the f*ing wheel again. 47 if ( parsedURL.auth ) { 48 var auth = new Buffer(parsedURL.auth).toString("base64"); 49 options.headers.authorization = 'Basic '+auth; 50 } 51 return HTTP.request(options) 52 .then(function(res){ 53 return res.body.read().then(function(content){ 54 return JSON.parse(content.toString() || "{}"); 55 }); 56 },function(res){ 57 return res.body.read().then(function(error){ 58 console.warn(error); // q.all doesn't do errors, so let's show them here 59 return JSON.parse(error.toString() || "{}"); 60 }); 20 61 }); 21 return value;22 } else {23 return value;24 62 } 25 }26 63 27 function request(method,path,content) { 28 var url = dbURL+path; 29 return http.request({ 30 url: url, 31 method: method, 32 headers: { 33 'content-type': 'application/json; charset=utf-8', 34 'accept': 'application/json' 35 }, 36 body: { 37 forEach: function(callback) { 38 callback(JSON.stringify(content || {})); 39 } 64 console.log("Configuring CouchDB for QED"); 65 console.log("Checking CouchDB version"); 66 return request('GET','') 67 .then(function(res){ 68 if (res.version !== "1.2.0" ) { 69 console.log("Found "+res.version+", only tested with CouchDB 1.2.0"); 70 } else { 71 console.log("CouchDB 1.2.0 found"); 40 72 } 41 73 }).then(function(res){ 42 return res.body.read().then(function(content){ 43 return JSON.parse(content); 74 console.log("Checking database 'qed'"); 75 return request('GET','qed') 76 .then(function(res){ 77 console.log("Database 'qed' found."); 78 },function(err){ 79 console.log("Creating database 'qed'"); 80 return request('PUT','qed'); 44 81 }); 45 },function(res){ 46 return res.body.read().then(function(error){ 47 return JSON.parse(error); 48 }); 82 }).then(function(){ 83 console.log("Putting documents in database."); 84 designDocs = stringifyFunctions(designDocs); 85 return q.all(_.map(designDocs, function(doc,docUrl){ 86 var configAction = doc.__configAction; 87 delete doc.__configAction; 88 switch (configAction) { 89 case "ignore": 90 console.log(docUrl+" ignored."); 91 break; 92 case "update": 93 console.log(docUrl+" updating."); 94 return request('GET',docUrl) 95 .then(function(oldDoc){ 96 _.extend(oldDoc,doc); 97 return request('PUT',docUrl,oldDoc); 98 },function(){ 99 return request('PUT',docUrl,doc); 100 }); 101 break; 102 case "replace": 103 default: 104 console.log(docUrl+" replacing."); 105 return request('GET',docUrl) 106 .then(function(oldDoc){ 107 _.extend(doc,_.pick(oldDoc,'_id','_rev')); 108 return request('PUT',docUrl,doc); 109 },function(){ 110 return request('PUT',docUrl,doc); 111 }); 112 break; 113 } 114 })); 115 }).then(function(){ 116 console.log("Done!"); 117 },function(err){ 118 console.error("ERROR",err,err.stack); 49 119 }); 50 }51 120 52 console.log("Configuring CouchDB for QED"); 53 console.log("Checking CouchDB version"); 54 request('GET','') 55 .then(function(res){ 56 if (res.version !== "1.2.0" ) { 57 console.log("Found "+res.version+", only tested with CouchDB 1.2.0"); 58 } else { 59 console.log("CouchDB 1.2.0 found"); 60 } 61 console.log("Checking database 'qed'"); 62 }).then(function(res){ 63 return request('GET','qed') 64 .then(function(res){ 65 console.log("Database 'qed' found."); 66 },function(err){ 67 console.log("Creating database 'qed'"); 68 return request('PUT','qed'); 69 }); 70 }).then(function(){ 71 console.log("Putting documents in database."); 72 designDocs = stringifyFunctions(designDocs); 73 return q.all(_.map(designDocs, function(doc,docUrl){ 74 var configAction = doc.__configAction; 75 delete doc.__configAction; 76 switch (configAction) { 77 case "ignore": 78 console.log(docUrl+" ignored."); 79 case "update": 80 console.log(docUrl+" updating."); 81 return request('GET',docUrl) 82 .then(function(oldDoc){ 83 _.extend(oldDoc,doc); 84 return request('PUT',docUrl,oldDoc); 85 },function(){ 86 return request('PUT',docUrl,doc); 87 }); 88 case "replace": 89 default: 90 console.log(docUrl+" replacing."); 91 return request('GET',docUrl) 92 .then(function(oldDoc){ 93 _.extend(doc,_.pick(oldDoc,'_id','_rev')); 94 return request('PUT',docUrl,doc); 95 },function(){ 96 return request('PUT',docUrl,doc); 97 }); 98 } 99 })); 100 }).then(function(){ 101 console.log("Done!"); 102 },function(err){ 103 console.error("ERROR",err); 104 }); 121 }; -
Dev/trunk/src/server/config/couchdb-design-docs.js
r455 r464 1 1 module.exports = { 2 2 3 "_users/qed_admin": { 3 4 __configAction: "ignore", … … 8 9 type: "user" 9 10 }, 11 10 12 "qed/_security": { 11 13 __configAction: "ignore", … … 19 21 } 20 22 }, 23 21 24 "qed/_design/default": { 22 25 __configAction: "replace", … … 34 37 } 35 38 }, 39 36 40 "qed/_design/questions": { 37 41 __configAction: "replace", … … 81 85 } 82 86 }, 87 83 88 "qed/_design/surveys": { 84 89 __configAction: "replace", … … 100 105 } 101 106 }, 107 102 108 "qed/_design/responses": { 103 109 __configAction: "replace", … … 113 119 } 114 120 } 121 115 122 };
Note: See TracChangeset
for help on using the changeset viewer.