/
app.go
143 lines (121 loc) · 3.22 KB
/
app.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
package app
import (
"sync"
"time"
"github.com/rez-go/stev"
"github.com/timemore/foundation/errors"
)
const EnvPrefixDefault = "APP_"
const (
NameDefault = "Timemore"
URLDefault = "https://github.com/timemore"
EmailDefault = "nop@e-mail.mailer.com"
NotificationEmailSenderDefault = "no-reply@e-mail.mailer.com"
TeamNameDefault = "Time More Team's"
EnvDefault = "dev"
DefaultTZLocation = "UTC"
)
type Info struct {
// Name of the app
Name string
// URL Canonical URL of the app
URL string
TermsOfServiceURL string
PrivacyPolicyURL string
Email string
NotificationEmailSender string
TeamName string
Env string
TZLocation string
location *time.Location
}
func DefaultInfo() Info {
return Info{
Name: NameDefault,
URL: URLDefault,
Email: EmailDefault,
NotificationEmailSender: NotificationEmailSenderDefault,
TeamName: TeamNameDefault,
Env: EnvDefault,
TZLocation: DefaultTZLocation,
}
}
type App interface {
AppInfo() Info
InstanceID() string
AddServer(ServiceServer)
Run()
IsAllServersAcceptingClients() bool
}
type Base struct {
appInfo Info
instanceID string
servers []ServiceServer
serversMu sync.Mutex
}
func (appBase Base) AppInfo() Info { return appBase.appInfo }
func (appBase Base) InstanceID() string { return appBase.instanceID }
// AddServer adds a server to be run simultaneously. Do NOT call this
// method after the app has been started.
func (appBase *Base) AddServer(srv ServiceServer) {
appBase.serversMu.Lock()
appBase.servers = append(appBase.servers, srv)
appBase.serversMu.Unlock()
}
// Run runs all the servers. Do NOT add any new server after this method was called.
func (appBase *Base) Run() {
RunServers(appBase.servers)
}
// IsAllServersAcceptingClients checks if every server is accepting clients.
func (appBase *Base) IsAllServersAcceptingClients() bool {
servers := appBase.servers
for _, srv := range servers {
if !srv.IsAcceptingClients() {
return false
}
}
return true
}
var (
defApp App
defAppOnce sync.Once
)
func InitByEnvDefault() (App, error) {
info := DefaultInfo()
err := stev.LoadEnv(EnvPrefixDefault, &info)
if err != nil {
return nil, errors.Wrap("info loading from environment variables", err)
}
return Init(&info)
}
func Init(info *Info) (App, error) {
var err error
defAppOnce.Do(func() {
var appInfo Info
if info != nil {
appInfo = *info
} else {
appInfo = DefaultInfo()
}
var unameStr string
unameStr, err = unameString()
if err != nil {
return
}
location, err := time.LoadLocation(appInfo.TZLocation)
if err != nil {
location = time.Local
}
if appInfo.Env == "" {
appInfo.Env = "dev"
}
appInfo.location = location
defApp = &Base{appInfo: appInfo, instanceID: unameStr}
})
if err != nil {
return nil, err
}
return defApp, nil
}
func TZLocation() *time.Location { return defApp.AppInfo().location }
func (appInfo Info) TZ() *time.Location { return appInfo.location }