-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis.go
121 lines (111 loc) · 3.57 KB
/
redis.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
package redis
import (
"fmt"
"net/http"
"regexp"
"strings"
"github.com/bitly/go-simplejson"
"github.com/clbanning/mxj"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
)
var (
LogTag = "caddy-redis"
apiPath = "/redis/"
apiPathRegex = regexp.MustCompile(apiPath + "(.+[^/])/?")
)
type Redis struct {
Next httpserver.Handler
}
func (redis Redis) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
// Get key from url
match := apiPathRegex.FindStringSubmatch(r.URL.Path)
// See if we got a match
if len(match) > 1 {
key := match[1]
// Check HTTP method
if r.Method == http.MethodPost {
// POST
// We need to write to the database
// Get json
// Here we are using simplejson to convert to a json object. This is just to check if we were given valid json with the request
json, err := simplejson.NewFromReader(r.Body)
if err != nil {
errorMessage := "Error while decoding json from POST: " + err.Error()
fmt.Println(LogTag, errorMessage)
serveJSONError(w, errorMessage)
return 0, nil
}
fmt.Println(LogTag, "Got json from POST:", json)
jsonEncoded, err := json.Encode()
if err != nil {
errorMessage := "Error while encoding json from POST: " + err.Error()
fmt.Println(LogTag, errorMessage)
serveJSONError(w, errorMessage)
return 0, nil
}
// Write the json string to the databse under the key
set(key, string(jsonEncoded))
} else if r.Method == http.MethodGet {
// GET
// We need to get the key from the database
value, err := get(key)
if err != nil {
errorMessage := "Error while getting value from Redis: " + err.Error()
fmt.Println(LogTag, errorMessage)
serveJSONError(w, errorMessage)
return 0, nil
}
// Here we are using simplejson to convert to a json object. This is just to check if we were given valid json by Redis
json, err := simplejson.NewJson(value)
if err != nil {
errorMessage := "Error while unmarshalling json from Redis: " + err.Error()
fmt.Println(LogTag, errorMessage)
serveJSONError(w, errorMessage)
return 0, nil
}
// Check what type of encoding we need to serve
if strings.Contains(r.Header.Get("Accept-Encoding"), "xml") {
// Create XML
jsonMap, err := json.Map()
if err != nil {
errorMessage := "Error while converting json to Golang map: " + err.Error()
fmt.Println(LogTag, errorMessage)
serveXMLError(w, errorMessage)
return 0, nil
}
xmlMap := mxj.Map(jsonMap)
xmlValue, err := xmlMap.Xml()
if err != nil {
errorMessage := "Error while unmarshalling xml: " + err.Error()
fmt.Println(LogTag, errorMessage)
serveXMLError(w, errorMessage)
return 0, nil
}
serveXML(w, xmlValue)
} else {
serveJSON(w, []byte(value))
}
}
return http.StatusOK, nil
} else {
return redis.Next.ServeHTTP(w, r)
}
}
func serveJSONError(w http.ResponseWriter, errorMessage string) {
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{\"error\":\"" + errorMessage + "\"}"))
}
func serveJSON(w http.ResponseWriter, jsonPayload []byte) {
w.Header().Set("Content-Type", "application/json")
w.Write(jsonPayload)
}
func serveXMLError(w http.ResponseWriter, errorMessage string) {
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "application/xml")
w.Write([]byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?><error>" + errorMessage + "</error>"))
}
func serveXML(w http.ResponseWriter, xmlPayload []byte) {
w.Header().Set("Content-Type", "application/xml")
w.Write(xmlPayload)
}