-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
117 lines (102 loc) · 2.85 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
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
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"net/http"
"os"
"syscall"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
func init() {
viper.SetConfigName("config") // name of config file (without extension)
viper.AddConfigPath(".")
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
log.Fatalf("Fatal error config file: %s \n", err)
}
viper.WatchConfig()
}
var addr = flag.String("addr", ":8080", "http service address")
var roomMap = make(map[string]*Hub)
func serveHome(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL)
if r.URL.Path != "/" {
http.Error(w, "Not found", 404)
return
}
if r.Method != "GET" {
http.Error(w, "Method not allowed", 405)
return
}
http.ServeFile(w, r, "home.html")
}
func wsServe(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Host: %s\n", r.Host)
r.ParseForm()
key := r.Form.Get("key")
room := r.Form.Get("room")
mode := r.Form.Get("mode")
if room == "" {
panic("no room in query string")
return
}
var hub *Hub
if key == "free" {
if roomMap[room] == nil { //该频道不存在
log.Printf("create new room %s", room)
hub = newHub(room, mode)
roomMap[room] = hub
hub.P2pConfig.Live.MaxLayers = viper.GetInt("live.maxLayers")
hub.P2pConfig.Live.MinRPNodes = viper.GetInt("live.minRPNodes")
hub.P2pConfig.Live.MaxRPNodes = viper.GetInt("live.maxRPNodes")
hub.P2pConfig.Live.DefaultUploadBW = viper.GetInt64("live.DefaultUploadBW")
hub.P2pConfig.Live.RPBWThreshold = viper.GetInt64("live.RPBWThreshold")
hub.P2pConfig.Live.Substreams = viper.GetInt("live.substreams")
go hub.run()
} else {
hub = roomMap[room]
}
serveWs(hub, w, r)
} else {
w.WriteHeader(http.StatusForbidden)
}
}
func visServer(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
//log.Printf("Scheme %s", r.URL.Scheme)
room := r.Form.Get("room")
if room == "" {
panic("no room in query string")
return
}
log.Printf("HandleFunc /vis room: %s", room)
hub := roomMap[room]
if hub == nil {
w.WriteHeader(http.StatusNotFound)
return
}
log.Printf("serveVisWs")
serveVisWs(hub, w, r)
}
func main() {
//防止输出出现乱码
handle := syscall.Handle(os.Stdout.Fd())
kernel32DLL := syscall.NewLazyDLL("kernel32.dll")
setConsoleModeProc := kernel32DLL.NewProc("SetConsoleMode")
setConsoleModeProc.Call(uintptr(handle), 0x0001|0x0002|0x0004)
log.Println(viper.GetInt("live.maxLayers"))
flag.Parse()
http.HandleFunc("/", serveHome)
http.HandleFunc("/ws", wsServe)
http.HandleFunc("/vis", visServer)
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func setupHub(hub *Hub) {
}