forked from fabiolb/fabio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manual.go
154 lines (132 loc) · 4.01 KB
/
manual.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
package ui
import (
"html/template"
"net/http"
)
type ManualHandler struct {
BasePath string
Color string
Title string
Version string
Commands string
}
type paths struct {
Path string
Name string
}
func (h *ManualHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.RequestURI[len(h.BasePath):]
data := struct {
*ManualHandler
Path string
APIPath string
}{
ManualHandler: h,
Path: path,
APIPath: "/api/manual" + path,
}
tmplManual.ExecuteTemplate(w, "manual", data)
}
var funcs = template.FuncMap{
"noescape": func(str string) template.HTML {
return template.HTML(str)
},
}
var tmplManual = template.Must(template.New("manual").Funcs(funcs).Parse(`
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>fabio{{if .Title}} - {{.Title}}{{end}}</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style type="text/css">
.footer { padding-top: 10px; }
.logo { height: 32px; margin: 0 auto; display: block; }
</style>
</head>
<body>
<ul id="overrides" class="dropdown-content"></ul>
<nav class="top-nav {{.Color}}">
<div class="container">
<div class="nav-wrapper">
<a href="/" class="brand-logo"><img style="margin: 15px 0" class="logo" src="/logo.svg?format=bw"> {{if .Title}} - {{.Title}}{{end}}</a>
<ul id="nav-mobile" class="right hide-on-med-and-down">
<li><a href="/routes">Routes</a></li>
<li><a class="dropdown-button" href="#!" data-activates="overrides">Overrides<i class="material-icons right">arrow_drop_down</i></a></li>
<li><a href="https://github.com/fabiolb/fabio/blob/master/CHANGELOG.md">{{.Version}}</a></li>
<li><a href="https://github.com/fabiolb/fabio">Github</a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="section">
<h5>Manual Routes{{if .Path}} for "{{.Path}}"{{end}}</h5>
<div class="row">
<form class="col s12">
<input type="hidden" name="version">
<div class="row">
<div class="input-field col s12">
<textarea id="textarea1" class="materialize-textarea"></textarea>
<label for="textarea1"></label>
</div>
</div>
</form>
<button class="btn waves-effect waves-light" name="save">Save</button>
<button class="btn waves-effect waves-light" name="help">Help</button>
</div>
<div class="row">
<pre class="help hide">{{.Commands}}</pre>
</div>
</div>
</div>
<script>
$(function(){
var params={};window.location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(str,key,value){params[key] = value;});
$.get({{.APIPath}}, function(data) {
$("input[name=version]").val(data.version);
$("textarea>label").val("Version " + data.version);
$("#textarea1").val(data.value);
$("#textarea1").trigger('autoresize');
});
$.get('/api/paths', function(data) {
var d = $("#overrides");
$.each(data, function(idx, val) {
var path = val;
if (val == "") {
val = "default"
}
d.append('<li><a href="/manual'+path+'">'+val+'</a></li>');
});
});
$("button[name=help]").click(function() {
$("pre.help").toggleClass("hide");
});
$("button[name=save]").click(function() {
var data = {
value : $("#textarea1").val(),
version : $("input[name=version]").val()
}
$.ajax({{.APIPath}}, {
type: 'PUT',
data: JSON.stringify(data),
contentType: 'application/json',
statusCode: {
400: function(jqXHR, textStatus, err) { alert(err); },
409: function(jqXHR, textStatus, err) { alert(err); },
500: function(jqXHR, textStatus, err) { alert(err); }
},
success: function() {
window.location.reload();
}
});
});
})
</script>
</body>
</html>
`))