-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
210 lines (173 loc) · 4.18 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"flag"
"fmt"
"github.com/blent/beagle/pkg/tracking"
"github.com/blent/beagle/server"
"github.com/blent/beagle/server/http"
"github.com/blent/beagle/server/storage"
"github.com/pkg/errors"
"os"
"strings"
"time"
)
var DefaultSettings = server.NewDefaultSettings()
var Version = "undefined"
var (
ErrInvalidName = errors.New("name value must be non-empty string")
ErrRouteCollision = errors.New("routes collision detected")
ErrStaticRoute = errors.New("static route must be non-empty string")
ErrInvalidTtlDuration = errors.New("ttl value must be greater than 0")
ErrInvalidHeartbeatInterval = errors.New("heartbeat value must be greater than 0")
ErrInvalidStorageConnection = errors.New("storage connection value must be non-empty string")
)
var (
help = flag.Bool(
"help",
false,
"show this list",
)
version = flag.Bool(
"version",
false,
"show version",
)
name = flag.String(
"name",
DefaultSettings.Name,
"application name",
)
httpEnable = flag.Bool(
"http",
DefaultSettings.Http.Enabled,
"enables http server",
)
httpPort = flag.Int(
"http-port",
DefaultSettings.Http.Port,
"http server port number",
)
httpApiRoute = flag.String(
"http-api-route",
DefaultSettings.Http.Api.Route,
"http server api route",
)
httpStaticsDir = flag.String("http-static-dir",
DefaultSettings.Http.Static.Directory,
"http server static files directory",
)
httpStaticsRoute = flag.String(
"http-static-route",
DefaultSettings.Http.Static.Route,
"http server static files route",
)
trackingTtl = flag.Int(
"tracking-ttl",
int(DefaultSettings.Tracking.Ttl/time.Second),
"peripheral ttl duration in seconds",
)
trackingHeartbeat = flag.Int(
"tracking-heartbeat",
int(DefaultSettings.Tracking.Heartbeat/time.Second),
"peripheral heartbeat interval in seconds",
)
storageConnection = flag.String(
"storage-connection",
DefaultSettings.Storage.ConnectionString,
"storage connection string",
)
)
func setHttpSettings(settings *http.Settings) error {
settings.Enabled = *httpEnable
if !settings.Enabled {
return nil
}
settings.Api.Route = strings.TrimSpace(*httpApiRoute)
settings.Port = *httpPort
settings.Headless = true
httpStaticsDirVal := strings.TrimSpace(*httpStaticsDir)
if httpStaticsDirVal != "" {
settings.Static.Directory = httpStaticsDirVal
settings.Static.Route = strings.TrimSpace(*httpStaticsRoute)
if settings.Static.Route == settings.Api.Route {
return ErrRouteCollision
}
if settings.Static.Route == "" {
return ErrStaticRoute
}
}
return nil
}
func setTrackingSettings(settings *tracking.Settings) error {
trackingTtlVal := *trackingTtl
if trackingTtlVal < 0 {
return ErrInvalidTtlDuration
}
trackingHeartbeat := *trackingHeartbeat
if trackingHeartbeat < 0 {
return ErrInvalidHeartbeatInterval
}
settings.Ttl = time.Second * time.Duration(trackingTtlVal)
settings.Heartbeat = time.Second * time.Duration(trackingHeartbeat)
return nil
}
func setStorageSettings(settings *storage.Settings) error {
settings.ConnectionString = strings.TrimSpace(*storageConnection)
if settings.ConnectionString == "" {
return ErrInvalidStorageConnection
}
return nil
}
func createSettings() (*server.Settings, error) {
res := server.NewDefaultSettings()
res.Version = Version
res.Name = strings.TrimSpace(*name)
if res.Name == "" {
return nil, ErrInvalidName
}
if err := setHttpSettings(res.Http); err != nil {
return nil, err
}
if err := setTrackingSettings(res.Tracking); err != nil {
return nil, err
}
if err := setStorageSettings(res.Storage); err != nil {
return nil, err
}
return res, nil
}
func main() {
flag.Parse()
if *help {
flag.PrintDefaults()
os.Exit(0)
return
}
if *version {
fmt.Println(Version)
os.Exit(0)
return
}
if os.Geteuid() != 0 {
fmt.Println(os.ErrPermission.Error())
os.Exit(1)
return
}
settings, err := createSettings()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
return
}
app, err := server.New(settings)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
return
}
if err := app.Run(); err != nil {
fmt.Println(err.Error())
os.Exit(1)
return
}
}