-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
212 lines (174 loc) · 6.28 KB
/
app.go
File metadata and controls
212 lines (174 loc) · 6.28 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"fmt"
"log"
"net/http"
socketio "github.com/googollee/go-socket.io"
)
var connFile = ".config/ThingsGUI/thingsgui.connections"
var sessionFile = ".config/ThingsGUI/thingsgui.session"
// app type
type app struct {
clients map[string]*client
server *socketio.Server
}
// authResp type
type authResp struct {
AuthMethod string
AuthOnly bool
}
// SocketRouter socketio
func (app *app) socketRouter() {
app.server.OnConnect("/", func(s socketio.Conn) error {
s.SetContext("")
app.clients[s.ID()] = &client{
connectionsPath: getHomePath(connFile),
logCh: make(chan string),
roomStore: newRoomStore(),
sessionPath: getHomePath(sessionFile),
socketConn: &s,
tmpFiles: newTmpFiles(),
}
lCh := app.clients[s.ID()].logCh
go func() {
for p := range lCh {
s.Emit("onLogging", p)
}
}()
lCh <- fmt.Sprintf("connected: %s", s.ID())
return nil
})
app.server.OnEvent("/", "authKey", func(s socketio.Conn, data map[string]string) (int, interface{}, message) {
return app.clients[s.ID()].authKey(data)
})
app.server.OnEvent("/", "authOnly", func(s socketio.Conn) (int, authResp, message) {
message := successMsg()
var auth authResp
if thingsguiAddress != "" {
auth.AuthOnly = true
auth.AuthMethod = thingsguiAuthMethod
}
return message.Status, auth, message
})
app.server.OnEvent("/", "authToken", func(s socketio.Conn, data map[string]string) (int, interface{}, message) {
return app.clients[s.ID()].authToken(data)
})
app.server.OnEvent("/", "authPass", func(s socketio.Conn, data map[string]string) (int, interface{}, message) {
return app.clients[s.ID()].authPass(data)
})
app.server.OnEvent("/", "cookie", func(s socketio.Conn, cookies string) int {
client := app.clients[s.ID()]
if useCookieSession && client.cookie == nil {
header := s.RemoteHeader()
header.Set("Cookie", cookies)
req := http.Request{Header: header}
cookie, _ := req.Cookie(cookieName)
client.cookie = cookie
}
return http.StatusNoContent
})
app.server.OnEvent("/", "connected", func(s socketio.Conn) (int, connResp, message) {
client := app.clients[s.ID()]
if client.cookie == nil {
req := http.Request{Header: s.RemoteHeader()}
cookie, _ := req.Cookie(cookieName)
client.cookie = cookie
}
return client.connected()
})
app.server.OnEvent("/", "connToNew", func(s socketio.Conn, data loginData) (int, connResp, message) {
return app.clients[s.ID()].connectToNew(data)
})
app.server.OnEvent("/", "connViaCache", func(s socketio.Conn, data loginData) (int, connResp, message) {
return app.clients[s.ID()].handlerConnectViaCache(data)
})
app.server.OnEvent("/", "reconn", func(s socketio.Conn) (int, connResp, message) {
return app.clients[s.ID()].reconnect()
})
app.server.OnEvent("/", "disconn", func(s socketio.Conn) (int, connResp, message) {
return app.clients[s.ID()].disconnect()
})
app.server.OnEvent("/", "getCachedConn", func(s socketio.Conn) (int, interface{}, message) {
return app.clients[s.ID()].getCachedConnections()
})
app.server.OnEvent("/", "newCachedConn", func(s socketio.Conn, data lData) (int, interface{}, message) {
return app.clients[s.ID()].newCachedConnection(data)
})
app.server.OnEvent("/", "editCachedConn", func(s socketio.Conn, data lData) (int, interface{}, message) {
return app.clients[s.ID()].editCachedConnection(data)
})
app.server.OnEvent("/", "renameCachedConn", func(s socketio.Conn, data lData) (int, interface{}, message) {
return app.clients[s.ID()].renameCachedConnection(data)
})
app.server.OnEvent("/", "delCachedConn", func(s socketio.Conn, data loginData) (int, interface{}, message) {
return app.clients[s.ID()].delCachedConnection(data)
})
app.server.OnEvent("/", "query", func(s socketio.Conn, data dataReq) (int, interface{}, message) {
return app.clients[s.ID()].query(data)
})
app.server.OnEvent("/", "cleanupTmp", func(s socketio.Conn) (int, bool, message) {
resp := true
err := app.clients[s.ID()].tmpFiles.cleanupTmp()
message := msg(err)
if err != nil {
resp = false
}
return message.Status, resp, message
})
app.server.OnEvent("/", "join", func(s socketio.Conn, data dataReq) (int, interface{}, message) {
return app.clients[s.ID()].join(s, data)
})
app.server.OnEvent("/", "leave", func(s socketio.Conn, data dataReq) (int, interface{}, message) {
return app.clients[s.ID()].leave(data)
})
app.server.OnEvent("/", "run", func(s socketio.Conn, data dataReq) (int, interface{}, message) {
return app.clients[s.ID()].run(data)
})
app.server.OnError("/", func(s socketio.Conn, e error) {
fmt.Printf("meet error: %s\n", e.Error())
})
app.server.OnDisconnect("/", func(s socketio.Conn, msg string) {
app.clients[s.ID()].logCh <- fmt.Sprintf("closed: %s", msg)
app.clients[s.ID()].tmpFiles.cleanupTmp()
app.clients[s.ID()].closeSingleConn()
delete(app.clients, s.ID())
})
}
// Start app
func (app *app) start() {
go app.server.Serve()
defer app.server.Close()
app.socketRouter()
//HTTP handlers
http.HandleFunc("/", handlerMain) // homepage
http.HandleFunc("/js/main-bundle", handlerMainJsBundle)
http.HandleFunc("/js/vendors-bundle", handlerVendorsJsBundle)
http.HandleFunc("/js/editor.worker.js", handlerEditorWorkerJS)
http.HandleFunc("/fonts/monaco-font.ttf", handlerMonacoFontTTF)
http.HandleFunc("/img/thingsdb.gif", handlerThingsdbGIF)
http.HandleFunc("/img/thingsgui-logo.png", handlerThingsguiLogo)
http.HandleFunc("/img/thingsdb-logo.png", handlerThingsdbLogo)
http.HandleFunc("/img/CesbitLogo.png", handlerCesbitLogo)
http.HandleFunc("/img/view-edit.png", handlerViewEditLogo)
http.HandleFunc("/favicon.ico", handlerFaviconIco)
http.HandleFunc("/download", handlerDownload)
http.HandleFunc("/session", handlerSession)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./react/static/"))))
http.Handle("/socket.io/", app.server)
log.Printf("Serving at http://%s:%d/", serverHost, serverPort)
if !disableOpenBrowser {
go open(fmt.Sprintf("http://%s:%d/", serverHost, serverPort))
}
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", serverHost, serverPort), nil))
}
func (app *app) quit() {
for _, v := range app.clients {
if v != nil {
v.tmpFiles.cleanupTmp()
if v.connection != nil {
v.connection.Close()
}
}
}
app.server.Close()
}