-
Notifications
You must be signed in to change notification settings - Fork 5
/
templates.go
209 lines (181 loc) · 7.02 KB
/
templates.go
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package rest
const(
documentation = `
{{ define "path-param" }}
{{ $arr := Split ( js . ) "/" }}
{{ js . }}
<div class="row">
{{ range $part := $arr }}
{{ if eq $part "" }}
{{ else if ( Contains $part ":" ) }}
<div class="col-xs-2">
<input id="{{ $part }}" class="form-control" type="text" placeholder="{{ $part }}">
</div>
{{ else }}
<div class="col-xs-2">{{ $part }}</div>
{{ end }}
{{ end }}
</div>
{{ end }}
{{ define "body-param" }}
{{ if .HasBodyParam }}
<div class="row">
<div class="col-xs-6">
<div class="form-group">
<label for="body">Body</label>
<textarea id="body" class="form-control" rows="10">{{ .JsonSchema }}
</textarea>
</div>
</div>
</div>
{{ end }}
{{ end }}
{{$page := .}}
<div class="text-center">
<h1>REST API on {{$page.Host}}</h1>
</div>
<div class="container">
{{range $route := .Routes}}
<div>
{{ if eq $route.Method "PUT" }}
<div class="bg-warning">
<form id="{{ printf "%s-%s" $route.Method $route.Path }}">
<input type="button"
class="btn btn-warning"
value="{{$route.Method}}"
onClick="doRequest('{{ $route.Method }}',
'{{ js (printf "%s-%s" $route.Method $route.Path) }}',
'{{ printf "http://%s%s" $page.Host $route.Path}}'
)">
{{ template "path-param" $route.Path }}
<br>
{{ template "body-param" $route }}
</form>
</div>
<div id="{{$route.Method}}-{{$route.Path}}"></div>
{{ else if eq $route.Method "POST" }}
<div class="bg-success">
<form id="{{ printf "%s-%s" $route.Method $route.Path }}">
<input type="button"
class="btn btn-success active"
value="{{$route.Method}}"
onClick="doRequest('{{ $route.Method }}',
'{{ js (printf "%s-%s" $route.Method $route.Path) }}',
'{{ printf "http://%s%s" $page.Host $route.Path}}'
)">
{{ template "path-param" $route.Path }}
<br>
{{ template "body-param" $route }}
</form>
</div>
<div id="{{$route.Method}}-{{$route.Path}}"></div>
{{ else if eq $route.Method "DELETE" }}
<div class="bg-danger">
<form id="{{ printf "%s-%s" $route.Method $route.Path }}">
<input type="button"
class="btn btn-danger active"
value="{{$route.Method}}"
onClick="doRequest('{{ $route.Method }}',
'{{ js (printf "%s-%s" $route.Method $route.Path) }}',
'{{ printf "http://%s%s" $page.Host $route.Path}}'
)">
{{ template "path-param" $route.Path }}
<br>
{{ template "body-param" $route }}
</form>
</div>
<div id="{{$route.Method}}-{{$route.Path}}"></div>
{{ else if eq $route.Method "GET"}}
<div class="bg-info">
<form id="{{ printf "%s-%s" $route.Method $route.Path }}">
<input type="button"
class="btn btn-info active"
value="{{$route.Method}}"
onClick="doRequest('{{ $route.Method }}',
'{{ js (printf "%s-%s" $route.Method $route.Path) }}',
'{{ printf "http://%s%s" $page.Host $route.Path}}'
)">
{{ template "path-param" $route.Path }}
<br>
{{ template "body-param" $route }}
</form>
</div>
<div id="{{ printf "%s-%s" $route.Method $route.Path}}"></div>
{{ else }}
<p class="bg-primary">
Unsupported HTTP Verb {{ $route.Method }}
</p>
{{ end }}
</div>
{{end}}
</div>
<script type="text/javascript">
function showError (result, resultDiv) {
console.log(result);
resultDiv.html('<pre class="box">Status: ' + result.status
+ "<br>Status Text: " + result.statusText
+ "<br>Response: " + result.responseText + "</pre>");
}
function getJsDivID (divID) {
divID = divID.replace("-", "\\-");
divID = divID.replace(new RegExp(":", 'g'), "\\:");
divID = divID.replace(new RegExp("/", 'g'), "\\/");
return divID
}
function replaceInPath (divID, path, resultDiv) {
resultDiv.html(""); // TODO: add a spinning wheel for in progress.
formDiv = $("form#"+divID+" :input:text");
for (var i = 0; i < formDiv.length; ++i) {
path = path.replace(formDiv[i].id, formDiv[i].value);
if (formDiv[i].value == "") {
resultDiv.html('<pre class="box">text box for variable "' + formDiv[i].id + '" is empty</pre>');
return;
}
}
return path;
}
function getBody(divID, resultDiv) {
textArea = $("form#"+divID+" textarea#body");
console.log(textArea);
if (textArea.length > 0) {
return textArea[0].value
} else {
return
}
}
function doRequest(verb, divID, path) {
divID = getJsDivID(divID);
resultDiv = $("div#" + divID);
path = replaceInPath(divID, path, resultDiv);
if (!path) { return; }
var request = {
url: path,
type: verb,
success: function(result, textStatus, jqXHR) {
console.log(jqXHR);
var js = "Status: " + jqXHR.status + "\n";
js += "Status Text: " + jqXHR.statusText;
if (result) {
js += "\n\n" + JSON.stringify(result, null, 2);
}
resultDiv.html("<pre class=box>" + js + "</pre>");
},
error: function(jqXHR) {
showError(jqXHR, resultDiv);
}
}
if (verb == 'POST' || verb == 'PUT') {
var body = getBody(divID, resultDiv);
request.headers = { "Content-Type": "application/json" };
request.data = body;
} else if (verb == 'DELETE') {
request.headers = { "Content-Type": "application/json" };
}
$.ajax(request);
}
</script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
`
)