-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
74 lines (64 loc) · 1.52 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"time"
"github.com/gorilla/mux"
"github.com/pion/webrtc/v2"
"net/http"
)
// Prepare the configuration
var peerConnectionConfig = webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{"stun:stun.l.google.com:19302"},
},
},
}
func main() {
rooms := NewRooms()
router := mux.NewRouter()
router.HandleFunc("/api/stats", func(w http.ResponseWriter, r *http.Request) {
bytes, err := json.Marshal(rooms.GetStats())
if err != nil {
http.Error(w, fmt.Sprint(err), 500)
}
w.Write(bytes)
}).Methods("GET")
router.HandleFunc("/api/rooms/{id}", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Headers", "*")
w.Header().Add("Access-Control-Allow-Origin", "*")
vars := mux.Vars(r)
roomID := vars["id"]
room, err := rooms.Get(roomID)
if err == errNotFound {
http.NotFound(w, r)
return
}
bytes, err := json.Marshal(room.Wrap(nil))
if err != nil {
http.Error(w, fmt.Sprint(err), 500)
}
w.Write(bytes)
}).Methods("GET")
router.HandleFunc("/{id}", func(w http.ResponseWriter, r *http.Request) {
serveWs(rooms, w, r)
})
// go rooms.Watch()
port := os.Getenv("PORT")
if port == "" {
port = "80"
log.Printf("Defaulting to port %s", port)
}
addr := fmt.Sprintf(":%s", port)
fmt.Printf("listening on %s\n", addr)
srv := &http.Server{
Handler: router,
Addr: addr,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}