/
api.go
189 lines (161 loc) · 5.56 KB
/
api.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package bot
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
// HTTPError Converts err into a JSON byte array with key "message",
// then writes the marshalled JSON into the http.ResponseWriter, along with setting
// the error code given
//
// This is similar to http.Error, with 2 key differences:
//
// * HTTPError writes JSON instead of a string. Because of this, it sets the
// Content-Type of the response to application/json
//
// * HTTPError accepts an interface{} err instead of a string. This allows
// error and fmt.Stringer types to be accepted along with strings. If err
// is not a string or either of the types mentioned above, it is output
// as the format specifier `%+v`
func HTTPError(w http.ResponseWriter, err interface{}, code int) error {
var message string
switch err.(type) {
case string:
message = err.(string)
case fmt.Stringer:
message = err.(fmt.Stringer).String()
case error:
message = err.(error).Error()
default:
message = fmt.Sprintf("%+v", err)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(JSONMessage(message))
return nil
}
// JSONMessage Converts err into a JSON byte array with key "message"
func JSONMessage(message string) []byte {
json, _ := json.Marshal(struct {
Message string `json:"message"`
}{Message: message})
return json
}
// APIAddChannel Endpoint handler function to route to AddChannel
func (bot *OziachBot) APIAddChannel(w http.ResponseWriter, r *http.Request) {
pathParams := mux.Vars(r)
w.Header().Set("Content-Type", "application/json")
if name, ok := pathParams["channel"]; ok {
channel, err := bot.ChannelDB.AddChannel(name)
if err != nil {
HTTPError(w, err, http.StatusNotFound)
} else {
json, err := json.Marshal(channel)
if err != nil {
HTTPError(w, err, http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusCreated)
w.Write(json)
}
}
} else {
HTTPError(w, "Bad request format: /channel/{channel} required", http.StatusBadRequest)
}
}
// APIGetChannel Endpoint handler function to route to GetChannel
func (bot *OziachBot) APIGetChannel(w http.ResponseWriter, r *http.Request) {
pathParams := mux.Vars(r)
w.Header().Set("Content-Type", "application/json")
if name, ok := pathParams["channel"]; ok {
channel, err := bot.ChannelDB.GetChannel(name)
if err != nil {
HTTPError(w, err, http.StatusNotFound)
} else {
json, err := json.Marshal(channel)
if err != nil {
HTTPError(w, err, http.StatusInternalServerError)
} else {
w.Write(json)
}
}
} else {
HTTPError(w, "Bad request format: /channel/{channel} required", http.StatusBadRequest)
}
}
// APIConnectToChannel Endpoint handler function to route to ConnectToChannel
func (bot *OziachBot) APIConnectToChannel(w http.ResponseWriter, r *http.Request) {
pathParams := mux.Vars(r)
w.Header().Set("Content-Type", "application/json")
if name, ok := pathParams["channel"]; ok {
err := bot.ConnectToChannel(name)
if err != nil {
code := http.StatusInternalServerError
if _, ok := err.(ChannelNotFoundError); ok {
code = http.StatusNotFound
}
HTTPError(w, err, code)
}
} else {
HTTPError(w, "Bad request format: /channel/{channel} required", http.StatusBadRequest)
}
}
// APIDisconnectFromChannel Endpoint handler function to route to DisconnectFromChannel
func (bot *OziachBot) APIDisconnectFromChannel(w http.ResponseWriter, r *http.Request) {
pathParams := mux.Vars(r)
w.Header().Set("Content-Type", "application/json")
if name, ok := pathParams["channel"]; ok {
err := bot.DisconnectFromChannel(name)
if err != nil {
code := http.StatusInternalServerError
if _, ok := err.(ChannelNotFoundError); ok {
code = http.StatusNotFound
}
HTTPError(w, err, code)
}
} else {
HTTPError(w, "Bad request format: /channel/{channel} required", http.StatusBadRequest)
}
}
// APIChangeRSN Endpoint handler function to route to ChangeRSN
func (bot *OziachBot) APIChangeRSN(w http.ResponseWriter, r *http.Request) {
pathParams := mux.Vars(r)
w.Header().Set("Content-Type", "application/json")
name, ok := pathParams["channel"]
rsn, ok2 := pathParams["rsn"]
if ok && ok2 {
err := bot.ChangeRSN(name, rsn)
if err != nil {
code := http.StatusInternalServerError
if _, ok := err.(ChannelNotFoundError); ok {
code = http.StatusNotFound
}
HTTPError(w, err, code)
}
} else {
HTTPError(w, "Bad request format: /channel/{channel}/rsn/{rsn} required", http.StatusBadRequest)
}
}
// Heartbeat Returns "ok" to validate the health of the application
func Heartbeat(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
}
// ServeAPI Serves OziachBot's API
func (bot *OziachBot) ServeAPI() {
router := mux.NewRouter()
// Health check for load balancer
router.HandleFunc("/", Heartbeat).Methods(http.MethodGet, http.MethodHead)
obRouter := router.PathPrefix("/oziachbot").Subrouter()
channelAPI := obRouter.PathPrefix("/channel").Subrouter()
connectAPI := obRouter.PathPrefix("/connect").Subrouter()
// Configure all endpoints in the channel API
channelAPI.HandleFunc("/{channel}", bot.APIGetChannel).Methods(http.MethodGet)
channelAPI.HandleFunc("/{channel}", bot.APIAddChannel).Methods(http.MethodPost)
channelAPI.HandleFunc("/{channel}/rsn/{rsn}", bot.APIChangeRSN).Methods(http.MethodPut)
connectAPI.HandleFunc("/{channel}", bot.APIConnectToChannel).Methods(http.MethodPost)
connectAPI.HandleFunc("/{channel}", bot.APIDisconnectFromChannel).Methods(http.MethodDelete)
log.Fatal(http.ListenAndServe(":7373", router))
}