etcd.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. angular.module('etcd', [])
  3. .factory('EtcdV2', ['$http', function($http) {
  4. var keyPrefix = '/v2/keys/'
  5. var statsPrefix = '/v2/stats/'
  6. var baseURL = '/v2/'
  7. delete $http.defaults.headers.common['X-Requested-With'];
  8. function cleanupPath(path) {
  9. var parts = path.split('/');
  10. if (parts.length === 0) {
  11. return '';
  12. }
  13. parts = parts.filter(function(v){return v!=='';});
  14. parts = parts.join('/');
  15. return parts
  16. }
  17. function newKey(keyName) {
  18. var self = {};
  19. self.name = cleanupPath(keyName);
  20. self.getParent = function() {
  21. var parts = self.name.split('/');
  22. if (parts.length === 0) {
  23. return newKey('');
  24. }
  25. parts.pop();
  26. return newKey(parts.join('/'));
  27. };
  28. self.path = function() {
  29. var path = '/' + cleanupPath(keyPrefix + self.name);
  30. if (path === keyPrefix.substring(0, keyPrefix.length - 1)) {
  31. return keyPrefix
  32. }
  33. return path
  34. };
  35. self.get = function() {
  36. return $http.get(self.path());
  37. };
  38. self.set = function(keyValue) {
  39. return $http({
  40. url: self.path(),
  41. data: $.param({value: keyValue}),
  42. method: 'PUT',
  43. headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  44. });
  45. };
  46. self.deleteKey = function(keyValue) {
  47. return $http({
  48. url: self.path(),
  49. method: 'DELETE',
  50. headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  51. });
  52. };
  53. return self;
  54. }
  55. function newStat(statName) {
  56. var self = {};
  57. self.name = cleanupPath(statName);
  58. self.path = function() {
  59. return '/' + cleanupPath(statsPrefix + self.name);
  60. };
  61. self.get = function() {
  62. return $http.get(self.path());
  63. };
  64. return self
  65. }
  66. return {
  67. getStat: newStat,
  68. getKey: newKey
  69. }
  70. }]);