/
main.go
109 lines (89 loc) · 2.95 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
package main
import (
"fmt"
"golang.org/x/net/websocket"
"log"
"net/http"
// "net/url"
"github.com/larspensjo/config"
"strconv"
)
var applications ApplicationGroup
var applications_config ApplicationGroupConfig
type ServiceError struct {
Msg string
}
func (e *ServiceError) Error() string {
return fmt.Sprintf("%s",e.Msg)
}
func Error(msg string) error {
return &ServiceError{msg}
}
func StaticServer(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, "demo/demo.html")
// staticHandler := http.FileServer(http.Dir("./"))
// staticHandler.ServeHTTP(w, req)
return
}
func initServer() error {
configMap := make(map[string]ApplicationConfig)
cfg, err := config.ReadDefault("config.ini")
if err != nil {
return Error("unable to open config file or wrong fomart")
}
sections := cfg.Sections()
if len(sections) == 0 {
return Error("no app config")
}
for _, section := range sections {
if section != "DEFAULT" {
sectionData, _ := cfg.SectionOptions(section)
tmp := make(map[string]string)
for _, key := range sectionData {
value, err := cfg.String(section, key)
if err == nil {
tmp[key] = value
}
}
maxClientConn, _ := strconv.Atoi(tmp["MaxClientConn"])
configMap[section] = ApplicationConfig{tmp["AppId"], tmp["AppSecret"], maxClientConn, tmp["GetConnectApi"], tmp["LoseConnectApi"], tmp["MessageTransferApi"]}
}
}
fmt.Println(configMap)
valid_config := make(map[string]ApplicationConfig)
for appid, appconfig := range configMap {
// if appconfig.TokenMethod != TOKEN_METHOD_GET && appconfig.TokenMethod != TOKEN_METHOD_COOKIE{
// return Error("invalid TokenMethod appid: " + appid )
// }
if appconfig.MaxClientConn < 1 || appconfig.MaxClientConn > MAX_CLIENT_CONN {
return Error("invalid MaxClientConn appid: " + appid)
}
channelGroup := make(map[string]ChannelService)
app := Application{channelGroup, appconfig}
applications[appid] = app
valid_config[appid] = appconfig
}
applications_config = valid_config
return nil
}
func main() {
var err error
http.Handle("/", websocket.Handler(WsServer))
http.Handle("/api/create-channel", &ApiServer{ApiName: "create-channel"}) //create a ChannelService
http.Handle("/api/push", &ApiServer{ApiName: "push"})
http.Handle("/api/broadcast", &ApiServer{ApiName: "broadcast"})
http.Handle("/api/get-channel", &ApiServer{ApiName: "get-channel"})
http.Handle("/api/close-channel", &ApiServer{ApiName: "close-channel"}) //close a specific ChannelService
http.Handle("/api/app-status", &ApiServer{ApiName: "app-status"}) //online num and live connection num
http.HandleFunc("/demo", StaticServer)
fmt.Println("listen on port 8002")
//TODO offer a init commad to reload application info file
applications = make(ApplicationGroup)
applications_config = make(ApplicationGroupConfig)
if err = initServer(); err != nil {
panic(err.Error())
}
if err = http.ListenAndServe(":8002", nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}