-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.js
More file actions
113 lines (102 loc) · 3.05 KB
/
methods.js
File metadata and controls
113 lines (102 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
if (Meteor.isServer) {
Meteor.methods({
"apitoolsAuth" : function(key, monitorID) {
host = "https://" + key + "@" + monitorID + ".my.apitools.com/";
response = HTTP.get(host, { auth: key+":"+monitorID });
return readCookie("XSRF-TOKEN", response.headers['set-cookie']);
},
"apitoolsListServices" : function(key, monitorID) {
host = "https://" + key + "@" + monitorID + ".my.apitools.com/api/services";
response = HTTP.get(host, { auth: key+":"+monitorID });
return response.data;
},
"apitoolsCreateService" : function(name, endpoint, credentials) {
host = "https://" + credentials.key + "@" + credentials.monitorID + ".my.apitools.com/api/services";
console.log(name, endpoint, credentials);
try {
response = HTTP.post(host,
{
auth: credentials.key+":"+credentials.monitorID,
headers: {"X-XSRF-TOKEN": credentials.token, "Content-Type" : "application/json"},
data: {
endpoints:[{
url: endpoint,
code: URLify2(name)
}],
name:name
}
});
if (response.statusCode == 201) {
console.log(response);
return response.data;
}
else {
console.log("false", response)
return false
}
}
catch (e) {
throw new Meteor.Error(e)
}
},
"apitoolsPushMiddleware" : function(serviceID, code, credentials) {
console.log("pushing middleware...");
host = "https://" + credentials.key + "@" + credentials.monitorID + ".my.apitools.com/api/services/"+serviceID+"/pipeline";
mw_uuid = uuid();
console.log("mw_uuid", mw_uuid);
middlewares = {};
middlewares[mw_uuid] = {
active: true,
code: code,
description: "Middleware autogenerated by API Health Bar",
name: "AutoDegeneratedMw",
position: 0,
uuid: mw_uuid
};
console.log(middlewares);
try {
response = HTTP.post(host,
{
auth: credentials.key+":"+credentials.monitorID,
headers: {"X-XSRF-TOKEN": credentials.token, "Content-Type" : "application/json"},
data: {
_id: 1,
middlewares: middlewares,
service_id: serviceID,
}
});
if (response.statusCode == 200) {
console.log(response);
return response.data;
}
else {
console.log("false", response)
return false
}
}
catch (e) {
throw new Meteor.Error(e);
}
}
});
}
function readCookie(name, headers) {
var cookiename = name + "=";
var ca = headers;
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(cookiename) == 0)
return c.substring(cookiename.length,c.length).split(";")[0];
}
return null;
}
function uuid() {
console.log("generating uuid...");
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r, v;
r = Math.random() * 16 | 0;
v = c === 'x' ? r : r & 0x3 | 0x8;
return v.toString(16);
});
}