-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.go
121 lines (94 loc) · 2.62 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
118
119
120
121
package gosf
import (
"log"
"net/http"
"reflect"
"strconv"
io "github.com/ambelovsky/gosf-socketio"
transport "github.com/ambelovsky/gosf-socketio/transport"
)
// SocketIO Server
var ioServer *io.Server
var ioTransport *transport.WebsocketTransport
func init() {
// Transport configuration
ioTransport = transport.GetDefaultWebsocketTransport()
// SocketIO Server
ioTransport.UnsecureTLS = true
ioServer = io.NewServer(ioTransport)
}
// Startup activates the framework and starts the server
func Startup(config map[string]interface{}) {
secure := false
port := 9999
path := "/"
/*** CONFIG ***/
if config["secure"] != nil {
secure = config["secure"].(bool)
}
if config["port"] != nil {
if reflect.TypeOf(config["port"]).String() == "float64" {
port = int(config["port"].(float64))
} else {
port = config["port"].(int)
}
}
if config["path"] != nil {
path = config["path"].(string)
}
address := ":" + strconv.Itoa(port)
if config["host"] != nil {
address = config["host"].(string) + address
}
if config["rejectInvalidHostnames"] != nil {
ioTransport.UnsecureTLS = !config["rejectInvalidHostnames"].(bool)
ioServer.UpdateTransport(ioTransport)
}
if config["enableCORS"] != nil {
corsDomain := config["enableCORS"].(string)
ioServer.EnableCORS(corsDomain)
}
/*** END CONFIG ***/
// Activate configured plugins
for _, plugin := range plugins {
plugin.Activate(&App)
}
// Start connection handlers
startConnectionHandlers()
// Setup http server
startHTTPServer(config, secure, address, port, path)
}
// Shutdown cleanly terminates the framework and its plugins
func Shutdown() {
// Deactivate configured plugins
for _, plugin := range plugins {
plugin.Deactivate(&App)
}
}
func startConnectionHandlers() {
// Handle connected
ioServer.On(io.OnConnection, func(channel *io.Channel) {
client := new(Client)
client.channel = channel
request := new(Request)
request.Endpoint = "connect"
emit("connect", client, request)
})
// Handle disconnected
ioServer.On(io.OnDisconnection, func(channel *io.Channel) {
client := new(Client)
client.channel = channel
request := new(Request)
request.Endpoint = "disconnect"
emit("disconnect", client, request)
})
}
func startHTTPServer(config map[string]interface{}, secure bool, address string, port int, path string) {
serveMux := http.NewServeMux()
serveMux.Handle(path, ioServer)
if !secure || config["ssl-cert"] == nil || config["ssl-key"] == nil {
log.Panic(http.ListenAndServe(address, serveMux))
} else {
log.Panic(http.ListenAndServeTLS(address, config["ssl-cert"].(string), config["ssl-key"].(string), serveMux))
}
}