forked from boson-project/grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
104 lines (98 loc) · 3.15 KB
/
handler.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
package grid
import (
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/gorilla/mux"
)
func newHandler(g *Grid) http.Handler {
h := mux.NewRouter()
h.HandleFunc("/v1/version", g.handleVersion) // see handlers.go
h.HandleFunc("/v1/events", g.handleEvents)
h.HandleFunc("/v1/subscriptions", g.handleSubscriptions)
return h
}
// handleVersion returns the version number provided, if any, JSON encoded.
func (g *Grid) handleVersion(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err := json.NewEncoder(w).Encode(g.version); err != nil {
fmt.Fprintln(os.Stderr, fmt.Errorf("error encoding version: %v", err))
}
}
// handleEvents returns the list of available events
func (g *Grid) handleEvents(w http.ResponseWriter, r *http.Request) {
var err error
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
switch r.Method {
case "GET":
var events []string
if events, err = g.adapter.EventManager().List(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
err = json.NewEncoder(w).Encode(err)
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Errorf("error while encoding error: %v", err))
}
return
}
if err := json.NewEncoder(w).Encode(events); err != nil {
fmt.Fprintln(os.Stderr, fmt.Errorf("error encoding version: %v", err))
}
case "POST":
w.WriteHeader(http.StatusNotImplemented)
err = json.NewEncoder(w).Encode("Creation of events not implemented.")
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
case "DELETE":
w.WriteHeader(http.StatusNotImplemented)
err = json.NewEncoder(w).Encode("Deleteion of events not implemented.")
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
default:
w.WriteHeader(http.StatusMethodNotAllowed)
err = json.NewEncoder(w).Encode("Method not supported.")
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
}
}
// handleSubscriptions returns the list of available subscriptions
func (g *Grid) handleSubscriptions(w http.ResponseWriter, r *http.Request) {
var err error
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
switch r.Method {
case "GET":
var subscriptions []string
if subscriptions, err = g.adapter.SubscriptionManager().List(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
err = json.NewEncoder(w).Encode(err)
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Errorf("error while encoding error: %v", err))
}
return
}
if err := json.NewEncoder(w).Encode(subscriptions); err != nil {
fmt.Fprintln(os.Stderr, fmt.Errorf("error encoding version: %v", err))
}
case "POST":
w.WriteHeader(http.StatusNotImplemented)
err = json.NewEncoder(w).Encode("Creation of subscriptions not implemented.")
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
case "DELETE":
w.WriteHeader(http.StatusNotImplemented)
err = json.NewEncoder(w).Encode("Deleteion of subscriptions not implemented.")
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
default:
w.WriteHeader(http.StatusMethodNotAllowed)
err = json.NewEncoder(w).Encode("Method not supported.")
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
}
}