/
handlers.go
132 lines (120 loc) · 4.08 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
131
132
package internal
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/vincent-petithory/dataurl"
"net/http"
"strings"
)
// Handlers struct has methods to handle http requests to be served by
// the webserver.
type Handlers struct {
storage *StorageInterface
// Address of the server hosting the application. This will be used
// to generate short urls. E.g - https://bm.suram.in/r/foo
serverAddress string
// Location of the sqlite file.
dbFile string
}
// ActionView handles http request to view bookmarks list.
func (h *Handlers) ActionView(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
secret := r.FormValue("secret")
aliasInfos := h.storage.GetAllAliases(secret)
resp := CreateViewResponse(aliasInfos, h.serverAddress)
if err := json.NewEncoder(w).Encode(resp); err != nil {
Log.Fatal(err)
}
}
// ActionLookup handles http request to convert short url to the full url.
func (h *Handlers) ActionLookup(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
short := r.FormValue("short")
fullURL := h.storage.URLFromAlias(short)
if fullURL == nil {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "Not Found")
return
}
fmt.Fprint(w, *fullURL)
}
// ActionRevLookup handles http request to convert full url to the short url.
func (h *Handlers) ActionRevLookup(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
secret := r.FormValue("secret")
long := r.FormValue("long")
shortUrls := h.storage.GetShortUrls(secret, long)
resp := RevLookupResponse{shortUrls}
if err := json.NewEncoder(w).Encode(resp); err != nil {
Log.Fatal(err)
}
}
// ActionAdd handles http request to add a new bookmark.
func (h *Handlers) ActionAdd(w http.ResponseWriter, r *http.Request) {
short := r.FormValue("short")
long := r.FormValue("url")
secret := r.FormValue("secret")
Log.Println(short, long, secret)
respStr := h.storage.AddAlias(long, short, secret)
fmt.Fprint(w, respStr)
}
// ActionDel handles http request to add a delete bookmark.
func (h *Handlers) ActionDel(w http.ResponseWriter, r *http.Request) {
short := r.FormValue("id")
secret := r.FormValue("secret")
Log.Println("formparams", short, secret)
respStr := h.storage.DelAlias(short, secret)
fmt.Fprint(w, respStr)
}
// ActionUpdate handles http request to add a update bookmark.
func (h *Handlers) ActionUpdate(w http.ResponseWriter, r *http.Request) {
presAlias := r.FormValue("id")
newVal := r.FormValue("newvalue")
oldVal := r.FormValue("oldvalue")
colName := r.FormValue("colname")
secret := r.FormValue("secret")
Log.Println("formparams:(presAlias, new, old, field)", presAlias, newVal, oldVal, colName)
respStr := h.storage.UpdateAlias(presAlias, oldVal, newVal, colName, secret)
fmt.Fprint(w, respStr)
}
func (h *Handlers) writeError(w http.ResponseWriter, status int, text string) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(status)
err := json.NewEncoder(w).Encode(ErrStruct{Code: status, Text: text})
if err != nil {
panic(err)
}
}
func (h *Handlers) handleDataUrl(w http.ResponseWriter, urlStr string) {
parsed, err := dataurl.DecodeString(urlStr)
if err != nil {
h.writeError(w, http.StatusInternalServerError, err.Error())
return
}
w.Header().Add("Content-Type", parsed.MediaType.ContentType())
w.Write(parsed.Data)
}
// Redirect handles redirect for short url to full url.
func (h *Handlers) Redirect(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
redID := vars["redId"]
url := h.storage.URLFromAlias(redID)
if url != nil {
var urlStr = *url
if strings.HasPrefix(urlStr, "data:") {
h.handleDataUrl(w, urlStr)
return
}
if !strings.HasPrefix(urlStr, "http://") && !strings.HasPrefix(urlStr, "https://") {
urlStr = "http://" + urlStr
}
http.Redirect(w, r, urlStr, http.StatusFound)
return
}
// If we didn't find it, 404
h.writeError(w, http.StatusNotFound, "Not Found")
}