-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
105 lines (85 loc) · 2.35 KB
/
main.go
File metadata and controls
105 lines (85 loc) · 2.35 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
package main
import (
"database/sql"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/PiotrTopa/js8web/model"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
logger *zap.Logger
)
func parseLogLevel(level string) zapcore.Level {
switch strings.ToLower(level) {
case "debug":
return zapcore.DebugLevel
case "warn":
return zapcore.WarnLevel
case "error":
return zapcore.ErrorLevel
default:
return zapcore.InfoLevel
}
}
func main() {
initConfig()
cfg := zap.NewDevelopmentConfig()
cfg.Level.SetLevel(parseLogLevel(LOG_LEVEL))
logger, _ = cfg.Build()
defer logger.Sync()
logger.Sugar().Infow("js8web starting",
"js8callAddr", JS8CALL_TCP_CONNECTION_STRING,
"port", WEBAPP_PORT,
"db", DB_FILE_PATH,
)
incomingEvents := make(chan model.Js8callEvent, 1)
outgoingEvents := make(chan model.Js8callEvent, 1)
db := initDbConnection()
defer db.Close()
initStationInfoCache(db)
initJs8callConnection(incomingEvents, outgoingEvents)
outgoingWebsocketEvents, newObjects := dispatchStateChangeEvents(incomingEvents)
websocketMessages := make(chan model.WebsocketMessage, 1)
go mainDispatcher(db, websocketMessages, outgoingWebsocketEvents, newObjects)
wsSessionContainer := new(websocketSessionContainer)
wsSessionContainer.init()
go wsSessionContainer.process(websocketMessages)
go startWebappServer(db, wsSessionContainer, outgoingEvents)
logger.Sugar().Infof("js8web ready — http://localhost:%d", WEBAPP_PORT)
// Wait for termination signal
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
sig := <-quit
fmt.Println() // newline after ^C
logger.Sugar().Infow("Shutting down", "signal", sig.String())
}
func mainDispatcher(db *sql.DB, websocketMessages chan<- model.WebsocketMessage, outgoingWebsocketEvents <-chan model.WebsocketEvent, newObjects <-chan model.DbObj) {
go func() {
for object := range newObjects {
err := object.Save(db)
if err != nil {
logger.Sugar().Errorw(
"Error when saving object to DB",
"object", object,
"error", err,
)
}
websocketMessages <- model.WebsocketMessage{
EventType: "object",
WsType: object.WsType(),
Event: object,
}
}
}()
for event := range outgoingWebsocketEvents {
websocketMessages <- model.WebsocketMessage{
EventType: "event",
WsType: event.WsType(),
Event: event,
}
}
}