-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
56 lines (53 loc) · 1.45 KB
/
handler.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
package main
import (
"encoding/json"
"io"
"net/http"
"strings"
)
func RedirectHandler(service LinkService) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
slug := r.PathValue("slug")
url, err := service.Get(slug)
if err == ErrDecodeFailure {
msg := http.StatusText(http.StatusBadRequest)
http.Error(w, msg, http.StatusBadRequest)
} else if err == ErrNotFound {
http.NotFound(w, r)
} else {
// `err` must be `nil`...
http.Redirect(w, r, url, http.StatusPermanentRedirect)
}
})
}
func CreateLinkHandler(service LinkService) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Body == nil || r.Body == http.NoBody {
msg := http.StatusText(http.StatusBadRequest)
http.Error(w, msg, http.StatusBadRequest)
return
}
payload := Link{}
body, err := io.ReadAll(r.Body) // DOS attack vector
defer r.Body.Close()
if err == nil {
err = json.Unmarshal(body, &payload)
}
if err != nil {
msg := http.StatusText(http.StatusBadRequest)
http.Error(w, msg, http.StatusBadRequest)
return
}
url := strings.TrimSpace(payload.URL)
link, err := service.Create(url)
if err != nil {
msg := http.StatusText(http.StatusInternalServerError)
http.Error(w, msg, http.StatusInternalServerError)
return
}
res, _ := json.Marshal(link)
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusCreated)
w.Write(res)
})
}