-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUrlHandlers.go
141 lines (123 loc) · 3.64 KB
/
UrlHandlers.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
package main
import (
"github.com/PuerkitoBio/purell"
"github.com/gorilla/mux"
"github.com/mediocregopher/radix/v3"
"github.com/spf13/viper"
"goshort/utils"
"net/http"
)
func urlsPostHandler(w http.ResponseWriter, r *http.Request) {
url_, err := CreateUrlFromHttpRequest(w, r)
if err != nil {
utils.ErrorToResponse(err, w)
return
}
if url_.Code == 0 {
url_.Code = 301
}
url_.Url, err = purell.NormalizeURLString(url_.Url,
purell.FlagLowercaseScheme|purell.FlagLowercaseHost|purell.FlagUppercaseEscapes)
if err != nil {
utils.ErrorToResponse(&utils.SimpleResponse{Status: 500, Msg: "incorrect URL"}, w)
return
}
if url_.Key == "" {
url_.Autogenerated = true
added := false
for !added {
var potentialKey string
potentialKey, err = GetAutogenUrlFromRedis(AppObject.Pool, url_.Url)
if err != nil {
utils.ErrorToResponse(&utils.SimpleResponse{Status: http.StatusInternalServerError, Msg: "Redis error"}, w)
return
}
if potentialKey != "" {
var potentialUrl Url
if potentialUrl, err = CreateUrlFromRedis(AppObject.Pool, potentialKey); err != nil {
utils.ErrorToResponse(err, w)
return
}
if potentialUrl.Url == url_.Url {
url_ = potentialUrl
added = true
}
} else {
url_.Key = utils.GetNewUrlString(AppObject.Pool)
if added, err = url_.CreateInRedis(AppObject.Pool); err != nil {
utils.ErrorToResponse(err, w)
return
}
}
}
} else {
result, err := url_.CreateInRedis(AppObject.Pool)
if err != nil {
utils.ErrorToResponse(err, w)
return
} else if !result {
utils.ErrorToResponse(&utils.SimpleResponse{Status: http.StatusConflict, Msg: "Value already exists"}, w)
return
}
}
url_.ToHttpResponse(w)
}
func urlsGetHandler(w http.ResponseWriter, _ *http.Request, url Url) {
url.ToHttpResponse(w)
}
func urlsPatchRequest(w http.ResponseWriter, r *http.Request, url Url) {
newUrl, err := CreateUrlFromHttpRequest(w, r)
if err != nil {
utils.ErrorToResponse(err, w)
return
}
url.Url = newUrl.Url
err = url.UpdateInRedis(AppObject.Pool)
if err != nil {
utils.ErrorToResponse(err, w)
return
}
url.ToHttpResponse(w)
}
func urlsDeleteRequest(w http.ResponseWriter, _ *http.Request, key string) {
err := AppObject.Pool.Do(radix.Cmd(nil, "DEL", key))
if err != nil {
utils.ErrorToResponse(err, w)
return
}
}
func checkTokenMiddleware(next func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
correctToken := viper.GetString("token")
if correctToken != "" {
token := r.Header.Get("Authorization")
if token == "" {
utils.ErrorToResponse(&utils.SimpleResponse{Status: http.StatusUnauthorized, Msg: "Need auth credentials"}, w)
return
} else if "Bearer "+correctToken != token {
utils.ErrorToResponse(&utils.SimpleResponse{Status: http.StatusUnauthorized, Msg: "Bad auth credentials"}, w)
return
}
}
next(w, r)
}
}
func urlDetailsHandler(w http.ResponseWriter, r *http.Request) {
url, err := CreateUrlFromRedis(AppObject.Pool, mux.Vars(r)["id"])
if err != nil {
utils.ErrorToResponse(err, w)
return
}
if r.Method == http.MethodGet {
urlsGetHandler(w, r, url)
} else if r.Method == http.MethodPatch || r.Method == http.MethodPut {
urlsPatchRequest(w, r, url)
} else { // DELETE
urlsDeleteRequest(w, r, url.Key)
}
}
func RegisterUrlsHandlers(router *mux.Router) {
router.HandleFunc("/urls/", checkTokenMiddleware(urlsPostHandler)).Methods(http.MethodPost)
router.HandleFunc("/urls/{id}/", checkTokenMiddleware(urlDetailsHandler)).Methods(http.MethodGet,
http.MethodPatch, http.MethodPut, http.MethodDelete)
}