-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
182 lines (164 loc) · 4.36 KB
/
server.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
package server
import (
"encoding/json"
"fmt"
"io/fs"
"net/http"
"strings"
"text/template"
"github.com/go-chi/chi"
bolt "go.etcd.io/bbolt"
)
type server struct {
r chi.Router
db *bolt.DB
content fs.FS
}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.r.ServeHTTP(w, r)
}
func New(db *bolt.DB, content fs.FS) http.Handler {
srv := &server{
r: chi.NewRouter(),
db: db,
content: content,
}
srv.Routes()
return srv
}
type Entry struct {
Source string `json:"source"`
Destination string `json:"destination"`
}
func (s *server) handleRegistry() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
tpl, err := template.ParseFS(s.content, "templates/registry.tmpl")
if err != nil {
http.Error(w, fmt.Errorf("failed to find template: %w", err).Error(), http.StatusInternalServerError)
return
}
var entries []Entry
err = s.db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte("redirects"))
c := bucket.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
entries = append(entries, Entry{
Source: string(k),
Destination: string(v),
})
}
return nil
})
if err != nil {
http.Error(w, fmt.Errorf("failed to view entries: %w", err).Error(), http.StatusInternalServerError)
return
}
if err := tpl.Execute(w, struct {
Entries []Entry
}{
Entries: entries,
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func (s *server) handleEdit() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/") {
r.URL.Path = r.URL.Path + "/%s"
http.Redirect(w, r, r.URL.String(), http.StatusTemporaryRedirect)
return
}
key := strings.TrimPrefix(r.URL.Path, "/edit/")
destination := ""
s.db.View(func(tx *bolt.Tx) error {
destination = string(tx.Bucket([]byte("redirects")).Get([]byte(key)))
return nil
})
tpl, err := template.ParseFS(s.content, "templates/edit.tmpl")
if err != nil {
http.Error(w, fmt.Errorf("failed to find template: %w", err).Error(), http.StatusInternalServerError)
return
}
if err := tpl.Execute(w, struct {
Path string
Destination string
}{
Path: key,
Destination: destination,
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func (s *server) handleSave() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var entry Entry
if err := json.NewDecoder(r.Body).Decode(&entry); err != nil {
http.Error(w, fmt.Errorf("failed to decode save request: %w", err).Error(), http.StatusBadRequest)
return
}
err := s.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte("redirects"))
if entry.Destination == "" {
return bucket.Delete([]byte(entry.Source))
}
return bucket.Put([]byte(entry.Source), []byte(entry.Destination))
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func (s *server) handleRedirect() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
err := s.db.View(func(tx *bolt.Tx) error {
path := strings.TrimPrefix(r.URL.Path, "/")
bucket := tx.Bucket([]byte("redirects"))
// check for exact match
entry := bucket.Get([]byte(path))
if entry != nil {
http.Redirect(w, r, string(entry), http.StatusTemporaryRedirect)
return nil
}
// check for wildcard
paths := strings.Split(path, "/")
position := -1
for i := 0; i < len(paths); i++ {
elements := []string{}
if len(paths[:i]) > 0 {
elements = append(elements, paths[:i]...)
}
elements = append(elements, "%s")
if len(paths[i+1:]) > 0 {
elements = append(elements, paths[i+1:]...)
}
checkURL := strings.Join(elements, "/")
entry = bucket.Get([]byte(checkURL))
if entry != nil {
position = i
break
}
}
if entry == nil {
r.URL.Path = "/edit/" + path
http.Redirect(w, r, r.URL.String(), http.StatusTemporaryRedirect)
return nil
}
url := string(entry)
if strings.Contains(url, "%s") {
url = strings.Replace(url, "%s", paths[position], -1)
}
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
return nil
})
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
}
}