forked from ostrost/ostent
/
main.bin.go
109 lines (93 loc) · 2.64 KB
/
main.bin.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
// +build bin
package main
import (
"flag"
"net"
"os"
"sync"
"time"
"github.com/ostrost/ostent/commands"
_ "github.com/ostrost/ostent/commands/ostent"
"github.com/ostrost/ostent/ostent"
"github.com/ostrost/ostent/share/templates"
)
var (
// AssetInfoFunc is for wrapping bindata's AssetInfo func.
AssetInfoFunc = BinAssetInfoFunc
// AssetReadFunc is for wrapping bindata's Asset func.
AssetReadFunc = BinAssetReadFunc
)
func init() {
commands.InitStdLog()
}
func main() {
flag.Usage = commands.UsageFunc(flag.CommandLine)
webserver := commands.NewWebserver(8050).AddCommandLine()
upgrade := commands.NewUpgrade().AddCommandLine()
flag.Parse()
errd, atexit := commands.ArgCommands()
defer atexit()
if errd {
return
}
webserver.ShutdownFunc = ostent.Connections.Reload
webserver.ServeFunc = func(listen net.Listener) {
go upgrade.UntilUpgrade()
go func() {
templates.InitTemplates(nil) // preventive
// sequential: Serve must wait for InitTemplates
Serve(listen, true, nil) // true stands for taggedbin
}()
}
upgrade.FirstUpgradeStopper = webserver.GoneAgain // initial upgrade skipped after gone again
upgrade.AfterUpgradeFunc = webserver.GoAgain
upgrade.Run()
webserver.FirstRunFunc = upgrade.HadUpgrade
if !upgrade.HadUpgrade() {
// RunBackground unless just had an upgrade and gonna relaunch anyway
ostent.RunBackground(PeriodFlag)
}
webserver.Run()
}
var (
// BootTime is the boot time.
BootTime = time.Now()
// ReadCache is a cache in AssetReadFunc.
ReadCache struct {
MU sync.Mutex
Byname map[string][]byte
}
)
// BinAssetInfoFunc wraps bindata's AssetInfo func. ModTime is always BootTime.
func BinAssetInfoFunc(infofunc func(string) (os.FileInfo, error)) func(string) (ostent.TimeInfo, error) {
return func(name string) (ostent.TimeInfo, error) {
_, err := infofunc(name)
if err != nil {
return nil, err
}
return BootInfo{}, nil
}
}
// BinAssetReadFunc wraps bindata's Asset func. Result is from cache or cached.
func BinAssetReadFunc(readfunc func(string) ([]byte, error)) func(string) ([]byte, error) {
return func(name string) ([]byte, error) {
return Read(readfunc, name)
}
}
// BootInfo is a ostent.TimeInfo implementation.
type BootInfo struct{}
// ModTime returns BootTime.
func (bi BootInfo) ModTime() time.Time { return BootTime } // bi is unused
// Read returns cached readfunc result.
func Read(readfunc func(string) ([]byte, error), name string) ([]byte, error) {
ReadCache.MU.Lock()
defer ReadCache.MU.Unlock()
if text, ok := ReadCache.Byname[name]; ok {
return text, nil
}
text, err := readfunc(name)
if err != nil {
ReadCache.Byname[name] = text
}
return text, err
}