/
ui.go
163 lines (150 loc) · 3.75 KB
/
ui.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
package redirect
import (
"embed"
"encoding/json"
"net/http"
"strings"
)
const (
formFieldTemplate = "template"
formFieldService = "service"
headerRedirPort = "X-Redir-Port"
)
//go:embed ui/*
var defaultStaticUI embed.FS // nolint:gochecknoglobals
// Get default UI static files (prefixed by ui/).
func DefaultUIStatic() embed.FS {
return defaultStaticUI
}
// description of rule for API request.
type UIEntry struct {
Template string `json:"template"`
Hits int64 `json:"hits"`
URL string `json:"url"`
}
type basicUI struct {
storage Storage
stats StatReader
engine Engine
redirPort string
}
func DefaultUI(storage Storage, stats StatReader, engine Engine, redirPort string) http.Handler {
if storage == nil {
panic("ui storage is nil")
}
if stats == nil {
panic("ui stats reader is nil")
}
if engine == nil {
panic("ui engine ref is nil")
}
return &basicUI{
stats: stats,
storage: storage,
engine: engine,
redirPort: redirPort,
}
}
func (ui *basicUI) ServeHTTP(wr http.ResponseWriter, rq *http.Request) {
defer rq.Body.Close()
service := strings.Trim(rq.URL.Path, "/")
switch rq.Method {
case http.MethodGet:
if service == "" {
ui.list(wr, rq)
} else {
ui.get(service, wr, rq)
}
case http.MethodPost, http.MethodPut, http.MethodPatch:
ui.set(wr, rq)
case http.MethodDelete:
ui.remove(service, wr, rq)
default:
ui.list(wr, rq)
}
}
func (ui *basicUI) list(wr http.ResponseWriter, _ *http.Request) {
var ans = make(map[string]*UIEntry)
entries, err := ui.storage.All()
if err != nil {
http.Error(wr, err.Error(), http.StatusInternalServerError)
return
}
for _, elem := range entries {
ans[elem.URL] = &UIEntry{
URL: elem.URL,
Template: elem.LocationTemplate,
Hits: ui.stats.Visits(elem.URL),
}
}
wr.Header().Set(headerRedirPort, ui.redirPort)
sendJSON(ans, wr)
}
func (ui *basicUI) get(service string, wr http.ResponseWriter, rq *http.Request) {
template, exists := ui.storage.Get(service)
if !exists {
http.NotFound(wr, rq)
return
}
wr.Header().Set(headerRedirPort, ui.redirPort)
sendJSON(&UIEntry{
URL: service,
Hits: ui.stats.Visits(service),
Template: template,
}, wr)
}
func (ui *basicUI) remove(service string, wr http.ResponseWriter, _ *http.Request) {
err := ui.storage.Remove(service)
if err != nil {
http.Error(wr, err.Error(), http.StatusInternalServerError)
return
}
err = ui.engine.Reload()
if err != nil {
http.Error(wr, err.Error(), http.StatusInternalServerError)
return
}
wr.WriteHeader(http.StatusNoContent)
}
func (ui *basicUI) set(wr http.ResponseWriter, rq *http.Request) {
var entry UIEntry
if strings.Contains(rq.Header.Get("Content-Type"), "application/json") {
// parse entry as-is except hits
err := json.NewDecoder(rq.Body).Decode(&entry)
if err != nil {
http.Error(wr, err.Error(), http.StatusBadRequest)
return
}
} else {
// use form
err := rq.ParseForm()
if err != nil {
http.Error(wr, err.Error(), http.StatusBadRequest)
return
}
entry.URL = rq.FormValue(formFieldService)
entry.Template = rq.FormValue(formFieldTemplate)
}
err := ui.storage.Set(entry.URL, entry.Template)
if err != nil {
http.Error(wr, err.Error(), http.StatusInternalServerError)
return
}
err = ui.engine.Reload()
if err != nil {
http.Error(wr, err.Error(), http.StatusInternalServerError)
return
}
wr.WriteHeader(http.StatusNoContent)
}
// correctly send JSON with required headers.
func sendJSON(data interface{}, w http.ResponseWriter) {
content, err := json.MarshalIndent(data, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(content)
}