[328] | 1 | require([ |
---|
| 2 | 'dojo/_base/json', |
---|
| 3 | 'dojo/_base/lang', |
---|
| 4 | 'dojo/_base/xhr', |
---|
| 5 | 'dojo/parser', |
---|
| 6 | 'dojo/query', |
---|
| 7 | 'dijit/registry', |
---|
[408] | 8 | 'dojo/domReady!' |
---|
| 9 | ],function(json,lang,xhr,parser,query,registry){ |
---|
[361] | 10 | var logNode; |
---|
| 11 | var usernameInput, passwordInput, resetInput, configButton; |
---|
| 12 | var defaultUsername = "", defaultPassword = ""; |
---|
| 13 | var dbUrl = "../data/"; |
---|
| 14 | |
---|
[408] | 15 | parser.parse() |
---|
| 16 | .then(function(){ |
---|
| 17 | query("#log").forEach(function(n){logNode = n;}); |
---|
| 18 | usernameInput = registry.byId('username'); |
---|
| 19 | passwordInput = registry.byId('password'); |
---|
| 20 | resetInput = registry.byId('reset'); |
---|
| 21 | configButton = registry.byId('configure'); |
---|
[328] | 22 | |
---|
[408] | 23 | usernameInput.set('value',defaultUsername); |
---|
| 24 | passwordInput.set('value',defaultPassword); |
---|
| 25 | configButton.on('click',configure); |
---|
[328] | 26 | |
---|
[408] | 27 | log("Give CouchDB admin username & password and click 'Configure' to start.\nIf the database already exists, rft_admin password will suffice.",true); |
---|
| 28 | }); |
---|
[328] | 29 | |
---|
[361] | 30 | function configure(){ |
---|
[362] | 31 | var docs; |
---|
| 32 | |
---|
[328] | 33 | log("Configuring CouchDB for RFT:",true); |
---|
| 34 | |
---|
| 35 | var username = usernameInput.get('value'); |
---|
| 36 | var password = passwordInput.get('value'); |
---|
| 37 | var reset = resetInput.get('value'); |
---|
| 38 | |
---|
[362] | 39 | log("Downloading most recent configuration."); |
---|
| 40 | xhr('GET',{ |
---|
| 41 | url: 'docs.json', |
---|
| 42 | handleAs: 'json', |
---|
| 43 | sync: true |
---|
| 44 | },true) |
---|
| 45 | .then(function(result){ |
---|
| 46 | docs = result; |
---|
| 47 | }); |
---|
[361] | 48 | |
---|
[328] | 49 | function req(method,url,body) { |
---|
[361] | 50 | var args = { |
---|
| 51 | url: dbUrl+url, |
---|
[328] | 52 | contentType: 'application/json', |
---|
| 53 | handleAs: 'json', |
---|
[362] | 54 | sync: true |
---|
[328] | 55 | }; |
---|
| 56 | if ( !body || lang.isObject(body) ) { |
---|
| 57 | body = json.toJson(body || {}); |
---|
| 58 | } |
---|
| 59 | args.rawBody = body; |
---|
| 60 | if ( username ) { |
---|
| 61 | args.user = username; |
---|
| 62 | args.password = password; |
---|
| 63 | } |
---|
| 64 | return xhr(method,args,true); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | log("Checking CouchDB version"); |
---|
| 68 | req('GET','/') |
---|
| 69 | .then(function(res){ |
---|
| 70 | if (res.version !== "1.2.0" ) { |
---|
| 71 | log("Found "+res.version+", only tested with CouchDB 1.2.0") |
---|
| 72 | } else { |
---|
| 73 | log("CouchDB 1.2.0 found") |
---|
| 74 | } |
---|
| 75 | }); |
---|
| 76 | |
---|
| 77 | var exists = false; |
---|
| 78 | log("Checking database 'rft'"); |
---|
| 79 | req('GET','/rft') |
---|
[361] | 80 | .then(function(db){ |
---|
| 81 | log("Database 'rft' found.") |
---|
[328] | 82 | exists = true; |
---|
| 83 | }); |
---|
| 84 | if ( exists && reset ) { |
---|
| 85 | req('DELETE','/rft'); |
---|
| 86 | exists = false; |
---|
| 87 | } |
---|
| 88 | if ( !exists ) { |
---|
| 89 | log("Creating database 'rft'") |
---|
| 90 | req('PUT','/rft') |
---|
| 91 | }; |
---|
| 92 | |
---|
[361] | 93 | function processDoc(docUrl,doc){ |
---|
| 94 | var configAction = doc.__configAction; |
---|
| 95 | delete doc.__configAction; |
---|
| 96 | switch (configAction) { |
---|
| 97 | case "ignore": |
---|
| 98 | log(docUrl+" ignored."); |
---|
| 99 | return false; |
---|
| 100 | case "update": |
---|
| 101 | log(docUrl+" updating."); |
---|
| 102 | return req('GET',docUrl) |
---|
| 103 | .then(function(oldDoc){ |
---|
| 104 | lang.mixin(oldDoc,doc); |
---|
| 105 | return req('PUT',docUrl,oldDoc); |
---|
| 106 | },function(){ |
---|
| 107 | return req('PUT',docUrl,doc); |
---|
| 108 | }); |
---|
| 109 | case "replace": |
---|
| 110 | default: |
---|
| 111 | log(docUrl+" replacing."); |
---|
| 112 | return req('GET',docUrl) |
---|
| 113 | .then(function(oldDoc){ |
---|
| 114 | doc['_rev'] = oldDoc['_rev']; |
---|
| 115 | return req('PUT',docUrl,doc); |
---|
| 116 | },function(){ |
---|
| 117 | return req('PUT',docUrl,doc); |
---|
| 118 | }); |
---|
| 119 | } |
---|
| 120 | } |
---|
[328] | 121 | |
---|
[361] | 122 | for (var docUrl in docs) { |
---|
| 123 | processDoc(docUrl,docs[docUrl]); |
---|
| 124 | } |
---|
[360] | 125 | |
---|
[328] | 126 | log("Done!"); |
---|
[361] | 127 | } |
---|
[328] | 128 | |
---|
[361] | 129 | function log(text,overwrite) { |
---|
| 130 | if ( overwrite ) logNode.innerHTML = text |
---|
| 131 | else logNode.innerHTML = logNode.innerHTML + '\n' + text; |
---|
| 132 | } |
---|
| 133 | |
---|
[408] | 134 | }); |
---|