/
main.go
93 lines (84 loc) · 2.5 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
// Demo is a ThingsDB module which may be used as a template to build modules.
//
// This module simply extract a given `message` property from a request and
// returns this message.
//
// For example:
//
// // Create the module (@thingsdb scope)
// new_module('DEMO', 'demo', nil, nil);
//
// // When the module is loaded, use the module in a future
// future({
// module: 'DEMO',
// message: 'Hi ThingsDB module!',
// }).then(|msg| {
// `Got the message back: {msg}`
// });
//
package main
import (
"fmt"
"log"
timod "github.com/thingsdb/go-timod"
"github.com/vmihailenco/msgpack"
)
func handler(buf *timod.Buffer, quit chan bool) {
for {
select {
case pkg := <-buf.PkgCh:
switch timod.Proto(pkg.Tp) {
case timod.ProtoModuleConf:
// Configuration data for this module is received from ThingsDB.
//
// The module should respond with:
//
// - timod.WriteConfOk(): if successful
// - timod.WriteConfErr(): in case the configuration has failed
log.Println("No configuration data is required for this module")
timod.WriteConfOk() // Just write OK
case timod.ProtoModuleReq:
// A request from ThingsDB may be unpacked to a struct or to
// an map[string]interface{}.
//
// The module should respond with:
//
// - timod.WriteResponse(pid, value): if successful
// - timod.WriteEx(pid, err_code, err_msg): in case of an error
type Demo struct {
Message string `msgpack:"message"`
}
var demo Demo
// pkg.Data contains Message Packed data, most likely you want
// to unpack the data into a struct.
err := msgpack.Unmarshal(pkg.Data, &demo)
if err == nil {
// In this demo a `message` property will be unpacked and
// used as a return value.
timod.WriteResponse(pkg.Pid, &demo.Message)
} else {
// In case of an error, make sure to call `WriteEx(..)` so
// ThingsDB can finish the future request with an
// appropriate error.
timod.WriteEx(
pkg.Pid,
timod.ExBadData,
fmt.Sprintf("failed to unpack request (%s)", err))
}
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("demo", handler)
// It is possible to add some cleanup code here
}