-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (132 loc) · 3.29 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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/signal"
"path/filepath"
"strings"
"time"
"github.com/samuelventura/laurelview/pkg/lvndb"
"github.com/samuelventura/laurelview/pkg/lvnrt"
"net/http"
_ "net/http/pprof"
)
func main() {
ctrlc := make(chan os.Signal, 1)
signal.Notify(ctrlc, os.Interrupt)
//runtime 0
dl := DefaultLog()
defer CloseLog(dl)
ctx := NewContext(dl)
defer WaitClose(ctx.Close)
log := ctx.PrefixLog("main")
defer TraceRecover(log.Warn)
//runtime 1 /ws/rt
ctx1 := NewContext(dl)
log1 := ctx1.PrefixLog("rt")
defer log1.Info("exited")
defer WaitClose(ctx1.Close)
//gets slow after double connection attempted
//takes >20s to connect on next attempt
ctx1.SetValue("bus.dialtoms", 20000)
ctx1.SetValue("bus.writetoms", 1000)
ctx1.SetValue("bus.readtoms", 1000)
ctx1.SetValue("bus.sleepms", 10)
ctx1.SetValue("bus.retryms", 2000)
ctx1.SetValue("bus.discardms", 100)
ctx1.SetValue("bus.resetms", 400)
ctx1.SetFactory("bus", func(ctx Context) Dispatch { return lvnrt.NewBus(ctx) })
hub1 := AsyncDispatch(log1, lvnrt.NewHub(ctx1))
ctx1.SetDispatch("hub", hub1)
ctx1.SetDispatch("state", AsyncDispatch(log1, lvnrt.NewState(ctx1)))
check1 := lvnrt.NewCheck(ctx1)
defer check1(Mn(":dispose"))
//runtime 2 /ws/db
var db2 = dbpath()
log.Info("db", db2)
var dao2 = NewDao(db2)
defer dao2.Close()
ctx2 := NewContext(dl)
log2 := ctx2.PrefixLog("db")
defer log2.Info("exited")
defer WaitClose(ctx2.Close)
ctx2.SetValue("dao", dao2)
hub2 := AsyncDispatch(log2, lvndb.NewHub(ctx2))
ctx2.SetDispatch("hub", hub2)
ctx2.SetDispatch("state", AsyncDispatch(log2, lvndb.NewState(ctx2)))
check2 := lvndb.NewCheck(ctx2)
defer check2(Mn(":dispose"))
//entry
ep := endpoint()
log.Info("endpoint", ep)
ctx.SetValue("entry.endpoint", ep)
ctx.SetValue("entry.buflen", 256)
ctx.SetValue("entry.wtoms", 4000)
ctx.SetValue("entry.rtoms", 4000)
ctx.SetValue("entry.static", NewEmbedHandler(log))
ctx.SetDispatch("/ws/rt", check1)
ctx.SetDispatch("/ws/db", check2)
entry := lvnrt.NewEntry(ctx)
defer WaitClose(entry.Close)
log.Info("port", entry.Port())
ticker1 := time.NewTicker(1 * time.Second)
defer ticker1.Stop()
go func() {
for range ticker1.C {
hub1(Mns(":ping", "main"))
hub2(Mns(":ping", "main"))
}
}()
http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
output := make(Channel)
entry.Status(output)
for line := range output {
fmt.Fprintln(w, line)
}
})
go func() {
//https://pkg.go.dev/net/http/pprof
//https://golang.org/doc/diagnostics
ep := os.Getenv("LV_NBE_DEBUG")
log.Info("pprof", ep)
log.Debug(http.ListenAndServe(ep, nil))
}()
//wait
exit := make(Channel)
go stdin(exit)
select {
case <-ctrlc:
case <-exit:
}
}
func stdin(exit Channel) {
defer close(exit)
ioutil.ReadAll(os.Stdin)
}
func executable() string {
exe, err := os.Executable()
PanicIfError(err)
return exe
}
func relative(ext string) string {
exe := executable()
dir := filepath.Dir(exe)
base := filepath.Base(exe)
file := base + "." + ext
return filepath.Join(dir, file)
}
func endpoint() string {
ep := os.Getenv("LV_NBE_ENDPOINT")
if len(strings.TrimSpace(ep)) > 0 {
return ep
}
return ":0"
}
func dbpath() string {
db := os.Getenv("LV_NBE_DATABASE")
if len(strings.TrimSpace(db)) > 0 {
return db
}
return relative("db3")
}