-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteonet.go
107 lines (88 loc) · 2.42 KB
/
teonet.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
// Copyright 2022 Kirill Scherba <kirill@scherba.ru>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Teonet module
package main
import (
"errors"
"fmt"
"os"
"time"
"github.com/teonet-go/teomon"
"github.com/teonet-go/teonet"
)
type Teonet struct {
*teonet.Teonet
fortune *teonet.APIClient
}
// newTeonet create new Teonet
func newTeonet() (teo *Teonet, err error) {
teo = new(Teonet)
// Create new teonet connector
teo.Teonet, err = teonet.New(Params.appShort, Params.port,
teonet.Log(), Params.loglevel, teonet.Logfilter(Params.logfilter),
teonet.Stat(Params.stat), teonet.Hotkey(Params.hotkey),
)
if err != nil {
teo.Log().Error.Println("can't init Teonet, error:", err)
return
}
// Show this application private key
if Params.showPrivate {
fmt.Printf("%x\n", teo.GetPrivateKey())
os.Exit(0)
}
// Connect to teonet
for teo.Connect() != nil {
teo.Log().Error.Println("can't connect to Teonet, try again...")
time.Sleep(1 * time.Second)
}
// Print teonet address
teo.Log().Debug.Println("connected to teonet, address:", teo.Address())
// Connet to fortune
if len(fortune) == 0 {
err = errors.New("can't connect to 'fortune', err: fortune address not set")
return
}
if err = teo.ConnectTo(fortune); err != nil {
err = errors.New("can't connect to 'fortune', err: " + err.Error())
return
}
// Connet to fortune api
if teo.fortune, err = teo.NewAPIClient(fortune); err != nil {
err = errors.New("can't connect to 'fortune' api, err: " + err.Error())
return
}
// Connect to monitor
if len(monitor) == 0 {
return
}
teomon.Connect(teo.Teonet, monitor, teomon.Metric{
AppName: appName,
AppShort: Params.appShort,
AppVersion: appVersion,
TeoVersion: teonet.Version,
AppStartTime: appStartTime,
})
teo.Log().Debug.Println("connected to monitor")
return
}
// Fortune get Fortune messsage from teofortune microservice
func (teo *Teonet) Fortune() (msg string, err error) {
// Add teofortune-tg url at the end of return message
defer func() {
msg += "\nSee this Teonet microservice source and description at: " +
"https://github.com/teonet-go/teofortune-tg"
}()
// Get fortune message from teofortune microservice
id, err := teo.fortune.SendTo("fortb", nil)
if err != nil {
return
}
data, err := teo.WaitFrom(fortune, uint32(id))
if err != nil {
return
}
msg = string(data)
return
}