-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
87 lines (75 loc) · 2.44 KB
/
routes.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/natebrennand/shrtnr/shrink"
)
// Used for server responses
type ServerResponse struct {
URL string
}
// Used for server requests
type ServerRequest struct {
LongURL string
RequestedURL string
}
// Serializes and returns the given ServerResponse struct through the resp
func (a apiHandler) returnJson(resp http.ResponseWriter, data interface{}) {
jsonData, err := json.Marshal(data)
if err != nil {
resp.WriteHeader(http.StatusInternalServerError)
log.Printf("Error marshaling data, %s", err.Error())
}
resp.Header().Set("Content-Type", "application/json")
resp.Write(jsonData)
}
// Given a short URL find the full length URL and returns it
func (a apiHandler) forward(req http.Request, shortURL string) {
longURL, err := shrink.RetrieveUrl(a.conn, shortURL)
if err != nil {
http.Error(a.resp, err.Error(), http.StatusBadRequest)
log.Printf("%d: URL for short, %s, not found\n", http.StatusFound, shortURL)
return
}
err = shrink.IncrUrlHitCount(a.conn, a.shortURL)
if err != nil {
log.Printf(err.Error())
}
http.Redirect(a.resp, &req, longURL, 302)
}
// Given a ServerRequest, tries to create a short url before returning
func (a apiHandler) createShortUrl(resp http.ResponseWriter, data ServerRequest) {
shortURL, err := shrink.CreateURL(a.conn, data.LongURL, data.RequestedURL)
if err != nil {
if err == shrink.UrlInUse {
http.Error(resp, err.Error(), http.StatusBadRequest)
log.Printf("Short url, %s, already in use\n", shortURL)
return
}
resp.WriteHeader(http.StatusInternalServerError)
log.Printf("%d: Unclassified error, %s\n", http.StatusInternalServerError, err.Error())
return
}
response := ServerResponse{shortURL}
a.returnJson(resp, response)
}
func (a apiHandler) getUrlStats(resp http.ResponseWriter, req *http.Request) {
stats, err := shrink.RetrieveUrlStats(a.conn, a.shortURL)
if err == shrink.UrlNotFound {
http.Error(resp, fmt.Sprintf("URL: /%s does not exist\n", a.shortURL), http.StatusBadRequest)
return
} else if err != nil {
log.Printf("Error retrieving stats, %s\n", err.Error())
http.Error(resp, fmt.Sprintf("Error retrieving stats for /%s\n", a.shortURL), http.StatusBadRequest)
return
}
statsJson, err := json.Marshal(stats)
if err != nil {
log.Printf("Error encoding stats json, %s\n", err.Error())
http.Error(resp, "Error encoding json", http.StatusInternalServerError)
return
}
resp.Write(statsJson)
}