forked from tquach/zk-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.go
47 lines (41 loc) · 1.14 KB
/
controller.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
package main
import (
"log"
"net/http"
"path"
"github.com/samuel/go-zookeeper/zk"
)
// Controller handles HTTP requests.
type Controller struct {
zkConn *zk.Conn
}
func (c *Controller) ServeHTTP(w http.ResponseWriter, req *http.Request) {
path := cleanPath(req.URL.Path)
switch req.Method {
case "GET":
log.Printf("retrieving node %q", path)
contents, _, err := c.zkConn.Get(path)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(contents)
return
case "OPTIONS":
// TODO: Implement CORs correctly
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type")
w.WriteHeader(http.StatusOK)
return
}
}
func cleanPath(str string) string {
if str == "" || str == "/" {
return str
}
return path.Clean(str)
}