Changeset 466 for Dev/trunk/src/server/config
- Timestamp:
- 06/26/13 14:43:57 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/trunk/src/server/config/config-couchdb.js
r464 r466 1 1 var q = require('q'); 2 var HTTP = require('q-io/http'); 3 var URL = require('url'); 2 var request = require('../util/request'); 4 3 var _ = require('underscore'); 5 4 var util = require('util'); … … 27 26 } 28 27 29 function request(method,path,content) {28 function dbRequest(method,path,content) { 30 29 var url = couchDbURL+path; 31 var parsedURL = URL.parse(url);32 30 var options = { 33 url: url,34 31 method: method, 35 32 headers: { … … 37 34 'accept': 'application/json' 38 35 }, 39 body: { 40 forEach: function(callback) { 41 callback(JSON.stringify(content || {})); 42 } 43 } 36 body: content 44 37 }; 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 }); 61 }); 38 return request(url,options); 62 39 } 63 40 64 41 console.log("Configuring CouchDB for QED"); 65 42 console.log("Checking CouchDB version"); 66 return request('GET','')43 return dbRequest('GET','') 67 44 .then(function(res){ 68 45 if (res.version !== "1.2.0" ) { … … 73 50 }).then(function(res){ 74 51 console.log("Checking database 'qed'"); 75 return request('GET','qed')52 return dbRequest('GET','qed') 76 53 .then(function(res){ 77 54 console.log("Database 'qed' found."); 78 55 },function(err){ 79 56 console.log("Creating database 'qed'"); 80 return request('PUT','qed');57 return dbRequest('PUT','qed'); 81 58 }); 82 59 }).then(function(){ … … 92 69 case "update": 93 70 console.log(docUrl+" updating."); 94 return request('GET',docUrl)71 return dbRequest('GET',docUrl) 95 72 .then(function(oldDoc){ 96 73 _.extend(oldDoc,doc); 97 return request('PUT',docUrl,oldDoc);74 return dbRequest('PUT',docUrl,oldDoc); 98 75 },function(){ 99 return request('PUT',docUrl,doc);76 return dbRequest('PUT',docUrl,doc); 100 77 }); 101 78 break; … … 103 80 default: 104 81 console.log(docUrl+" replacing."); 105 return request('GET',docUrl)82 return dbRequest('GET',docUrl) 106 83 .then(function(oldDoc){ 107 84 _.extend(doc,_.pick(oldDoc,'_id','_rev')); 108 return request('PUT',docUrl,doc);85 return dbRequest('PUT',docUrl,doc); 109 86 },function(){ 110 return request('PUT',docUrl,doc);87 return dbRequest('PUT',docUrl,doc); 111 88 }); 112 89 break;
Note: See TracChangeset
for help on using the changeset viewer.