github.com/matm/etcd@v0.3.1-0.20140328024009-5b4a473f1453/mod/dashboard/app/scripts/common/services/etcd.js (about)

     1  'use strict';
     2  
     3  angular.module('etcd', [])
     4  
     5  .factory('EtcdV2', ['$http', '$q', function($http, $q) {
     6    var keyPrefix = '/v2/keys/'
     7    var statsPrefix = '/v2/stats/'
     8    var baseURL = '/v2/'
     9    var leaderURL = ''
    10  
    11    delete $http.defaults.headers.common['X-Requested-With'];
    12  
    13    function cleanupPath(path) {
    14      var parts = path.split('/');
    15      if (parts.length === 0) {
    16        return '';
    17      }
    18      parts = parts.filter(function(v){return v!=='';});
    19      parts = parts.join('/');
    20      return parts
    21    }
    22  
    23    function newKey(keyName) {
    24      var self = {};
    25      self.name = cleanupPath(keyName);
    26  
    27      self.getParent = function() {
    28        var parts = self.name.split('/');
    29        if (parts.length === 0) {
    30          return newKey('');
    31        }
    32        parts.pop();
    33        return newKey(parts.join('/'));
    34      };
    35  
    36      self.path = function() {
    37        var path = '/' + cleanupPath(keyPrefix + self.name);
    38        if (path === keyPrefix.substring(0, keyPrefix.length - 1)) {
    39          return keyPrefix
    40        }
    41        return path
    42      };
    43  
    44      self.get = function() {
    45        return $http.get(self.path());
    46      };
    47  
    48      self.set = function(keyValue) {
    49        return getLeader().then(function(leader) {
    50          return $http({
    51            url: leader + self.path(),
    52            data: $.param({value: keyValue}),
    53            method: 'PUT',
    54            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    55          });
    56        });
    57      };
    58  
    59      self.deleteKey = function(keyValue) {
    60        return getLeader().then(function(leader) {
    61          return $http({
    62            url: leader + self.path(),
    63            method: 'DELETE',
    64            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    65          });
    66        });
    67      };
    68  
    69      return self;
    70    }
    71  
    72    function newStat(statName) {
    73      var self = {};
    74      self.name = cleanupPath(statName);
    75  
    76      self.path = function() {
    77        return '/' + cleanupPath(statsPrefix + self.name);
    78      };
    79  
    80      self.get = function() {
    81        return $http.get(self.path());
    82      };
    83  
    84      return self
    85    }
    86  
    87    function getLeader() {
    88      return newStat('leader').get().then(function(response) {
    89        return newKey('/_etcd/machines/' + response.data.leader).get().then(function(response) {
    90          // TODO: do something better here p.s. I hate javascript
    91          var data = decodeURIComponent(response.data.node.value);
    92          data = data.replace(/&/g, "\",\"").replace(/=/g,"\":\"");
    93          data = JSON.parse('{"' + data + '"}');
    94          return data.etcd;
    95        });
    96      });
    97    }
    98  
    99    return {
   100      getStat: newStat,
   101      getKey: newKey,
   102    }
   103  }]);