-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.go
51 lines (40 loc) · 1.49 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
package chat
import (
"encoding/json"
"net/http"
"github.com/julienschmidt/httprouter"
)
// getServeMux returns a serve mux to be used in an `http.Server`
func getServeMux(h *hub) http.Handler {
r := httprouter.New()
r.GET("/", homeHandler)
r.POST("/messages", handle(h, newMessageHandler))
r.POST("/ws", handle(h, createWSUserHandler))
return r
}
// handler is the type that any HTTP handler needing to communicate with the
// server must use
type handler func(h *hub, w http.ResponseWriter, r *http.Request, ps httprouter.Params)
func handle(hub *hub, h handler) httprouter.Handle {
return httprouter.Handle(func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
h(hub, w, r, ps)
})
}
func homeHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Write([]byte("Hello! Try sending a message via a POST request to /messages, or connecting via websockets by sending a POST request with your desired username to /ws.\n"))
}
func createWSUserHandler(h *hub, w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
u := createWSUser(h, w, r, nil)
h.userCh <- u
}
func newMessageHandler(h *hub, w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
msg := &message{}
err := json.NewDecoder(r.Body).Decode(&msg)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer r.Body.Close()
h.messageCh <- msg
w.Write([]byte("Sent message " + msg.Text + " as user " + msg.Username + " to channel " + msg.Channel + "\n"))
}