-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
102 lines (84 loc) · 2.12 KB
/
web.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
package main
import (
"fmt"
"html/template"
"net/http"
"strings"
"github.com/iancoleman/strcase"
)
type webEntry struct {
Entry entry
URLS map[string][]string
}
type appHandler struct {
config config
em entriesManager
}
func (h appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.New("index.html").Funcs(template.FuncMap{
"upper": strings.ToUpper,
"spacify": spacify,
}).ParseFiles("index.html")
entries, err := h.getPreparedEntries()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
err = tmpl.Execute(w, struct {
Config config
WebEntries map[string][]webEntry
}{
h.config,
entries,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// Separate entries by categories and replace urls/port if needed
func (h appHandler) getPreparedEntries() (map[string][]webEntry, error) {
categories := make(map[string][]webEntry)
entries, err := h.em.get()
if err != nil {
return categories, err
}
for _, entry := range entries {
if !entry.NoWeb {
webEntry := webEntry{entry, make(map[string][]string)}
for name, segment := range entry.Segments {
stop := false
for _, host := range segment.Hosts {
if stop {
break
}
for _, path := range segment.Paths {
if stop {
break
}
port := false
if entry.WebDirect ||
((!h.config.ProxyMode || entry.Direct) && (h.config.NoHosts || entry.NoHosts)) {
host = entry.IP
port = true
stop = true
} else if !h.config.ProxyMode || entry.Direct {
port = true
}
url := fmt.Sprintf("%s://%s", segment.Proto, host)
if port && segment.Port != "80" && segment.Port != "443" {
url = fmt.Sprintf("%s:%s", url, segment.Port)
}
url = fmt.Sprint(url, path)
webEntry.URLS[name] = append(webEntry.URLS[name], url)
}
}
}
for _, cat := range entry.Category {
categories[cat] = append(categories[cat], webEntry)
}
}
}
return categories, nil
}
func spacify(s string) string {
return strings.Title(strcase.ToDelimited(s, ' '))
}