-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandlers.go
130 lines (103 loc) · 2.77 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
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
)
func NewRouter(app *App) *mux.Router {
var r = mux.NewRouter()
r.Path("/{hash:[a-z0-9]{6}}").Methods("GET").
HandlerFunc(app.redirectHandler)
r.Path("/add").Methods("POST").
HandlerFunc(app.addHandler)
r.Path("/check").Methods("GET").
HandlerFunc(app.checkHandler)
r.Path("/remove").Methods("DELETE").
HandlerFunc(app.removeHandler)
return r
}
func (a *App) redirectHandler(w http.ResponseWriter, r *http.Request) {
var hash = mux.Vars(r)["hash"]
if cached := a.Cache.Get(hash); cached != "" {
log.Println("using cached url", cached)
http.Redirect(w, r, cached, http.StatusMovedPermanently)
return
}
u, err := a.UrlStore().Get(hash)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else if u.Id == 0 {
http.NotFound(w, r)
return
}
a.Cache.Add(hash, u.Url)
log.Println(hash, "redirecting to", u.Url)
http.Redirect(w, r, u.Url, http.StatusMovedPermanently)
}
func (a *App) addHandler(w http.ResponseWriter, r *http.Request) {
var inputUrl = r.FormValue("url")
destUrl, err := parseUrl(inputUrl)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
u, err := a.UrlStore().GetByUrl(destUrl.String())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else if u.Id != 0 {
writeJson(w, http.StatusOK, u)
return
}
u = Url{
Hash: makeHash(destUrl.Host),
Url: destUrl.String(),
}
if err := a.UrlStore().Update(&u); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Println(destUrl, "added")
writeJson(w, http.StatusCreated, u)
}
func (a *App) checkHandler(w http.ResponseWriter, r *http.Request) {
var hash = r.FormValue("hash")
u, err := a.UrlStore().Get(hash)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else if u.Id == 0 {
http.NotFound(w, r)
return
}
writeJson(w, http.StatusOK, u)
}
func (a *App) removeHandler(w http.ResponseWriter, r *http.Request) {
var hash = r.FormValue("hash")
if u, err := a.UrlStore().Get(hash); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else if u.Id == 0 {
http.NotFound(w, r)
return
}
err := a.UrlStore().Remove(hash)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
a.Cache.Remove(hash)
log.Println(hash, "removed")
writeJson(w, http.StatusNoContent, nil)
}
func writeJson(w http.ResponseWriter, status int, a interface{}) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(status)
if a != nil {
if err := json.NewEncoder(w).Encode(a); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}