-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (62 loc) · 1.66 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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
"os/signal"
"seaspy/aisstream"
)
type Config struct {
Dock Dock `json:"dock"`
Portal Portal `json:"portal"`
Swabby Swabby `json:"swabby"`
Aisstream aisstream.Config `json:"aisstream"`
Google Google `json:"google"`
}
func main() {
configFile := flag.String("c", "", "config file")
flag.Parse()
config, err := loadConfig(*configFile)
if err != nil {
log.Fatalf("could not load config file: %s\n", err.Error())
}
ais := aisstream.NewAIS(config.Aisstream.Url, config.Aisstream.Api)
ais.Sub.AddBox(config.Aisstream.DefaultSub.Boxes)
ais.Sub.AddMMSI(config.Aisstream.DefaultSub.FilterMMSI)
ais.Sub.AddMsgType(config.Aisstream.DefaultSub.FilterMsgType)
go ais.ConnectAndStream()
dock := NewDock(config.Dock)
go dock.Run(ais.Msg)
swabby := NewSwabby(config.Swabby)
go swabby.Cleanup(dock)
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
ListenAndServe(ctx, dock, config.Portal, config.Google)
ais.Quit <- struct{}{}
<-ais.Done
dock.Quit <- struct{}{}
<-dock.Done
swabby.Quit <- struct{}{}
<-swabby.Done
}
func loadConfig(f string) (Config, error) {
var config Config
configFile, err := os.Open(f)
if err != nil {
return config, fmt.Errorf("could not open file: %w", err)
}
defer configFile.Close()
configBytes, err := io.ReadAll(configFile)
if err != nil {
return config, fmt.Errorf("could not read file: %w", err)
}
err = json.Unmarshal(configBytes, &config)
if err != nil {
return config, fmt.Errorf("could not unmarshal file: %w", err)
}
return config, nil
}