-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
117 lines (93 loc) · 2.66 KB
/
handlers.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/harrydrippin/go-quick-link/link"
)
// HandlerGo handles the `/go` route.
func HandlerGo(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path[4:] // `/go/a/b/c/` => `a/b/c`
splitted := strings.Split(path, "/")
if splitted[len(splitted)-1] == "" {
splitted = splitted[0 : len(splitted)-1]
}
command := splitted[0]
params := splitted[1:]
log.Printf("Command: %s", command)
log.Printf("Params: %v", params)
query := link.Query{
Command: command,
ParamCount: len(params),
}
quickLink, err := link.GetDatabase().QueryQuickLink(query)
if err != nil {
w.WriteHeader(404)
w.Write([]byte(fmt.Sprintf("Command %s with %d parameters is not found", command, len(params))))
return
}
link := quickLink.Path
for i := 0; i < quickLink.ParamCount; i++ {
link = strings.Replace(link, fmt.Sprintf("{%d}", i), params[i], 1)
}
http.Redirect(w, r, link, http.StatusPermanentRedirect)
}
// HandlerGetQuickLinks handles the GET `/manage` route
func HandlerGetQuickLinks(w http.ResponseWriter, r *http.Request) {
result := link.GetDatabase().GetQuickLinks()
if err := json.NewEncoder(w).Encode(result); err != nil {
log.Println(err)
return
}
}
// HandlerSetQuickLinks handles the POST `/manage` route
func HandlerSetQuickLinks(w http.ResponseWriter, r *http.Request) {
var quickLinks []link.QuickLink
if err := json.NewDecoder(r.Body).Decode(&quickLinks); err != nil {
log.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
if err := link.GetDatabase().AddQuickLink(quickLinks); err != nil {
log.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
}
// HandlerDeleteQuickLinks handles the DELETE `/manage` route
func HandlerDeleteQuickLinks(w http.ResponseWriter, r *http.Request) {
var query link.Query
if err := json.NewDecoder(r.Body).Decode(&query); err != nil {
log.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
if err := link.GetDatabase().RemoveQuickLink(query); err != nil {
log.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
return
}
// HandlerHealthCheck handles the `/health` route.
func HandlerHealthCheck(w http.ResponseWriter, r *http.Request) {
type HealthCheckReturn struct {
Result bool `json:"result"`
Timestamp int64 `json:"timestamp"`
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
result := &HealthCheckReturn{
Result: true,
Timestamp: time.Now().Unix() * 1000,
}
if err := json.NewEncoder(w).Encode(result); err != nil {
log.Println(err)
return
}
}