-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
71 lines (61 loc) · 1.52 KB
/
state.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
package app
import (
"context"
"fmt"
"sync"
"time"
"admini.dev/admini/app/lib/auth"
"admini.dev/admini/app/lib/filesystem"
"admini.dev/admini/app/lib/telemetry"
"admini.dev/admini/app/lib/theme"
"admini.dev/admini/app/util"
)
var once sync.Once
type BuildInfo struct {
Version string `json:"version"`
Commit string `json:"commit"`
Date string `json:"date"`
}
func (b *BuildInfo) String() string {
if b.Date == util.KeyUnknown {
return b.Version
}
d, _ := util.TimeFromJS(b.Date)
return fmt.Sprintf("%s (%s)", b.Version, util.TimeToYMD(d))
}
type State struct {
Debug bool
BuildInfo *BuildInfo
Files filesystem.FileLoader
Auth *auth.Service
Themes *theme.Service
Services *Services
Started time.Time
}
func NewState(debug bool, bi *BuildInfo, f filesystem.FileLoader, enableTelemetry bool, port uint16, logger util.Logger) (*State, error) {
var loadLocationError error
once.Do(func() {
loc, err := time.LoadLocation("UTC")
if err != nil {
loadLocationError = err
return
}
time.Local = loc
})
if loadLocationError != nil {
return nil, loadLocationError
}
_ = telemetry.InitializeIfNeeded(enableTelemetry, bi.Version, logger)
return &State{
Debug: debug,
BuildInfo: bi,
Files: f,
Auth: auth.NewService("", port, logger),
Themes: theme.NewService(f),
Started: util.TimeCurrent(),
}, nil
}
func (s State) Close(ctx context.Context, logger util.Logger) error {
defer func() { _ = telemetry.Close(ctx) }()
return s.Services.Close(ctx, logger)
}