-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
187 lines (136 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
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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"os/user"
"path/filepath"
"strings"
"time"
"github.com/kernelschmelze/inbox/handler"
"github.com/kernelschmelze/inbox/handler/pushover"
"github.com/kernelschmelze/inbox/store"
"github.com/kernelschmelze/inbox/store/json"
log "github.com/kernelschmelze/pkg/logger"
"github.com/kernelschmelze/pkg/srv"
"github.com/pelletier/go-toml"
)
const (
maxFileSize = 1024 * 1024 * 5 // 5MB
)
type config struct {
Listen string
Crt string
Key string
Path string
Pushover pushover.Config
JsonStore jsonstore.Config
}
func main() {
path := flag.String("f", "./inbox.toml", "config file")
flag.Parse()
// read config from file
config, err := readConfig(*path)
if err != nil {
log.Errorf("read config failed, err=%s", err)
}
var store store.Store
// currently only a simple json store is supported
if store == nil {
if len(config.JsonStore.Path) == 0 {
config.JsonStore.Path = "./data"
}
if config.JsonStore.Path, err = expandPath(config.JsonStore.Path); err != nil {
log.Errorf("get data path failed, err=%s", err)
} else {
log.Infof("use data path '%s'", config.JsonStore.Path)
store = jsonstore.New(config.JsonStore)
}
}
// http handler
handler := handler.New(maxFileSize, store)
// pushover plugin
if len(config.Pushover.User) != 0 && len(config.Pushover.App) != 0 {
pushover := pushover.New(config.Pushover)
handler.AddPlugin(pushover)
}
// run the server
if len(config.Listen) == 0 {
config.Listen = ":25478"
}
server := srv.New(onListen, onShutdown)
err = server.Add(srv.Config{
config.Listen,
handler,
config.Crt,
config.Key,
})
if err != nil {
log.Errorf("server '%s' failed, err=%s", config.Listen, err)
}
defer func() {
server.Close()
handler.Close()
if store != nil {
store.Close()
}
}()
signalHandler()
}
func signalHandler() {
var gracefulStop = make(chan os.Signal)
signal.Notify(gracefulStop, os.Interrupt)
select {
case <-gracefulStop:
fmt.Println("")
}
go func() {
select {
case <-time.After(10 * time.Second):
fmt.Println("kill app")
os.Exit(1)
}
}()
}
func onListen(addr string, crtFile string, keyFile string) {
if len(crtFile) == 0 || len(keyFile) == 0 {
log.Infof("listen on '%s'", addr)
} else {
log.Infof("listen on '%s', crt '%s', key '%s'", addr, crtFile, keyFile)
}
}
func onShutdown(addr string, err error) {
if err == nil {
log.Infof("shutdown '%s'", addr)
return
}
if err != http.ErrServerClosed {
log.Errorf("shutdown '%s', err=%s", addr, err)
} else {
log.Infof("shutdown '%s', err=%s", addr, err)
}
}
func readConfig(path string) (config, error) {
var err error
var tml *toml.Tree
c := config{}
if tml, err = toml.LoadFile(path); err == nil {
err = tml.Unmarshal(&c)
}
return c, err
}
func expandPath(path string) (string, error) {
if path == "" {
return "", nil
}
if strings.HasPrefix(path, "~") {
usr, err := user.Current()
if err != nil {
return "", err
}
path = strings.Replace(path, "~", usr.HomeDir, 1)
}
return filepath.Abs(path)
}