-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
276 lines (243 loc) · 5.54 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// ThingsDB module for communication with ThingsDB.
//
// For example:
//
// // Create the module (@thingsdb scope)
// new_module('thingsdb', 'github.com/thingsdb/module-go-thingsdb');
//
// // Configure the module
// set_module_conf('thingsdb', {
// username: 'admin',
// password: 'pass',
// host: 'localhost',
// default_scope: '//stuff',
// });
//
// // Use the module
// thingsdb.query(".id()").then(|res| {
// res; // just return the response.
// });
package main
import (
"crypto/tls"
"fmt"
"log"
"sync"
"time"
thingsdb "github.com/thingsdb/go-thingsdb"
timod "github.com/thingsdb/go-timod"
"github.com/vmihailenco/msgpack"
)
var conn *thingsdb.Conn = nil
var mux sync.RWMutex
type nodes struct {
Host string `msgpack:"host"`
Port *uint16 `msgpack:"port"`
}
type config struct {
Username *string `msgpack:"username"`
Password *string `msgpack:"password"`
Token *string `msgpack:"token"`
Host string `msgpack:"host"`
Port *uint16 `msgpack:"port"`
UseSSL *bool `msgpack:"use_ssl"`
SkipVerify *bool `msgpack:"skip_verify"`
Timeout *uint16 `msgpack:"timeout"`
Nodes []nodes `msgpack:"nodes"`
}
type request struct {
Scope string `msgpack:"scope"`
Code *string `msgpack:"code"`
Name *string `msgpack:"name"`
Vars map[string]interface{} `msgpack:"vars"`
Args interface{} `msgpack:"args"`
}
func handleConf(cfg *config) {
mux.Lock()
defer mux.Unlock()
var port uint16 = 9200
var tlsConfig *tls.Config = nil
if cfg.Port != nil {
port = *cfg.Port
}
if cfg.UseSSL != nil && *cfg.UseSSL {
skipVerify := false
if cfg.SkipVerify != nil && *cfg.SkipVerify {
skipVerify = true
}
tlsConfig = &tls.Config{
InsecureSkipVerify: skipVerify,
}
}
if cfg.Username == nil && cfg.Token == nil {
log.Println("Missing username or token")
timod.WriteConfErr()
return
}
if cfg.Username != nil && cfg.Token != nil {
log.Println("Use a username or token, not both")
timod.WriteConfErr()
return
}
if cfg.Username != nil && cfg.Password == nil {
log.Println("Missing password for the username")
timod.WriteConfErr()
return
}
if conn != nil {
conn.Close()
}
conn = thingsdb.NewConn(cfg.Host, port, tlsConfig)
conn.DefaultTimeout = 10 * time.Second
if cfg.Timeout != nil {
conn.DefaultTimeout = time.Duration(*cfg.Timeout) * time.Second
}
if cfg.Nodes != nil && len(cfg.Nodes) > 0 {
for i := 0; i < len(cfg.Nodes); i++ {
var port uint16 = 9200
node := cfg.Nodes[i]
if node.Port == nil {
port = *node.Port
}
conn.AddNode(node.Host, port)
}
}
if err := conn.Connect(); err != nil {
conn = nil
log.Println(err)
timod.WriteConfErr()
return
}
if cfg.Token != nil {
if err := conn.AuthToken(*cfg.Token); err != nil {
conn.Close()
conn = nil
log.Println(err)
timod.WriteConfErr()
return
}
} else {
if err := conn.AuthPassword(*cfg.Username, *cfg.Password); err != nil {
conn.Close()
conn = nil
log.Println(err)
timod.WriteConfErr()
return
}
}
timod.WriteConfOk()
}
func onResponse(pkg *timod.Pkg, res []byte, err error) {
if err != nil {
if terr, ok := err.(*thingsdb.TiError); ok {
timod.WriteEx(
pkg.Pid,
timod.Ex(terr.Code()),
terr.Error())
return
}
timod.WriteEx(
pkg.Pid,
timod.ExBadData,
fmt.Sprintf("Unexpected error: %s", err))
return
}
timod.WriteResponseRaw(pkg.Pid, res)
}
func onModuleReq(pkg *timod.Pkg) {
mux.RLock()
defer mux.RUnlock()
if conn == nil {
timod.WriteEx(
pkg.Pid,
timod.ExCancelled,
"No connection, make sure you used set_module_conf() to initialze the module")
return
}
var req request
err := msgpack.Unmarshal(pkg.Data, &req)
if err != nil {
timod.WriteEx(
pkg.Pid,
timod.ExBadData,
fmt.Sprintf("Invalid request: %s", err))
return
}
if req.Code == nil && req.Name == nil {
timod.WriteEx(
pkg.Pid,
timod.ExBadData,
"Missing code (for QUERY) or name (for RUN)")
return
}
if req.Code != nil && req.Name != nil {
timod.WriteEx(
pkg.Pid,
timod.ExBadData,
"Both code (for QUERY) and name (for RUN) are set")
return
}
if req.Code != nil {
if req.Args != nil {
timod.WriteEx(
pkg.Pid,
timod.ExBadData,
"Found `args`, use `vars` for QUERY")
return
}
res, err := conn.QueryRaw(req.Scope, *req.Code, req.Vars)
onResponse(pkg, res, err)
return
}
if req.Name != nil {
if req.Vars != nil {
timod.WriteEx(
pkg.Pid,
timod.ExBadData,
"Found `vars`, use `args` for RUN")
return
}
res, err := conn.RunRaw(req.Scope, *req.Name, req.Args)
onResponse(pkg, res, err)
return
}
timod.WriteEx(
pkg.Pid,
timod.ExCancelled,
"Code should not be reached")
}
func handler(buf *timod.Buffer, quit chan bool) {
for {
select {
case pkg := <-buf.PkgCh:
switch timod.Proto(pkg.Tp) {
case timod.ProtoModuleConf:
var cfg config
err := msgpack.Unmarshal(pkg.Data, &cfg)
if err == nil {
handleConf(&cfg)
} else {
log.Println("Error: Missing or invalid ThingsDB configuration")
timod.WriteConfErr()
}
case timod.ProtoModuleReq:
onModuleReq(pkg)
default:
log.Printf("Error: Unexpected package type: %d", pkg.Tp)
}
case err := <-buf.ErrCh:
// In case of an error you probably want to quit the module.
// ThingsDB will try to restart the module a few times if this
// happens.
log.Printf("Error: %s", err)
quit <- true
}
}
}
func main() {
// Starts the module
timod.StartModule("thingsdb", handler)
if conn != nil {
conn.Close()
}
}