-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (126 loc) · 3.08 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
148
149
150
151
package main
import (
"context"
"embed"
"fmt"
"github.com/exelban/JAM/api"
"github.com/exelban/JAM/pkg/html"
"github.com/exelban/JAM/pkg/monitor"
"github.com/exelban/JAM/store"
"github.com/exelban/JAM/types"
"github.com/jessevdk/go-flags"
"github.com/pkgz/logg"
"log"
"os"
"os/signal"
"syscall"
)
type arguments struct {
ConfigPath string `long:"config-path" env:"CONFIG_PATH" default:"./config.yaml" description:"path to the configuration file"`
StorageType string `long:"storage-type" env:"STORAGE_TYPE" default:"bolt" description:"storage type"`
Port int `long:"port" env:"PORT" default:"8822" description:"service rest port"`
Debug bool `long:"debug" env:"DEBUG" description:"debug mode"`
}
type app struct {
srv *api.Server
api *api.Rest
config *types.Cfg
store store.Interface
args arguments
}
//go:embed templates/*
var fs embed.FS
var version string
func main() {
fmt.Println(version)
var args arguments
p := flags.NewParser(&args, flags.Default)
if _, err := p.Parse(); err != nil {
fmt.Printf("error parse args: %v", err)
os.Exit(1)
}
ctx, cancel := context.WithCancel(context.Background())
go func() {
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
<-stop
log.Print("[INFO] interrupt signal")
cancel()
}()
logg.NewGlobal(os.Stdout)
if args.Debug {
logg.DebugMode()
}
app, err := create(ctx, args)
if err != nil {
log.Printf("[ERROR] create app: %v", err)
os.Exit(1)
}
if err := app.run(ctx); err != nil {
log.Printf("[ERROR] run app: %v", err)
os.Exit(1)
}
}
func create(ctx context.Context, args arguments) (*app, error) {
log.Printf("[DEBUG] %+v", args)
cfg, err := types.NewConfig(ctx, args.ConfigPath)
if err != nil {
return nil, fmt.Errorf("new config: %w", err)
}
storage, err := store.New(ctx, args.StorageType, cfg)
if err != nil {
return nil, fmt.Errorf("new store: %w", err)
}
return &app{
srv: &api.Server{
Port: args.Port,
},
api: &api.Rest{
Monitor: &monitor.Monitor{
Store: storage,
},
Templates: &html.Templates{
FS: fs,
Debug: args.Debug,
},
Version: version,
},
config: cfg,
store: storage,
args: args,
}, nil
}
func (a *app) run(ctx context.Context) error {
if err := a.api.Templates.Run(ctx); err != nil {
log.Printf("[ERROR] generate templates: %v", err)
}
go func() {
if err := a.srv.Run(a.api.Router()); err != nil {
log.Printf("[ERROR] run rest server: %v", err)
}
}()
for {
select {
case <-a.config.FW:
if err := a.config.Parse(); err != nil {
log.Printf("[ERROR] parse config: %v", err)
}
if err := a.config.Validate(); err != nil {
log.Printf("[ERROR] validate config: %v", err)
}
if err := a.api.Monitor.Run(a.config); err != nil {
log.Printf("[ERROR] run monitor: %v", err)
}
case <-ctx.Done():
log.Print("[DEBUG] terminating...")
if err := a.srv.Shutdown(); err != nil {
log.Printf("[ERROR] rest shutdown %v", err)
}
if err := a.store.Close(); err != nil {
log.Printf("[ERROR] store close %v", err)
}
log.Print("[INFO] terminated")
return nil
}
}
}