/
api.go
37 lines (30 loc) · 933 Bytes
/
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
package main
import (
"fmt"
"github.com/gorilla/mux"
"net/http"
)
func handleApiDefault(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "APIs:")
fmt.Fprintf(w, "/hw")
fmt.Fprintf(w, "/bc")
}
func handleApiHelloWorld(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World")
}
func handleApiBroadcast(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "broadcast")
queryParams := r.URL.Query()
message := queryParams.Get("msg")
h.broadcast <- []byte(message)
}
func setupRoutes() {
router := mux.NewRouter()
subRouter := router.PathPrefix("/api").Subrouter().StrictSlash(false)
subRouter.HandleFunc("/", handleApiDefault)
subRouter.HandleFunc("/hw", handleApiHelloWorld)
subRouter.HandleFunc("/bc", handleApiBroadcast).Methods("POST")
router.PathPrefix("/ws").HandlerFunc(serveWs)
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/")))
http.Handle("/", router)
}