-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
executable file
·226 lines (196 loc) · 4.68 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
"github.com/sevlyar/go-daemon"
"github.com/heetch/confita"
"github.com/heetch/confita/backend"
"github.com/heetch/confita/backend/env"
"github.com/heetch/confita/backend/file"
"github.com/heetch/confita/backend/flags"
)
type config struct {
DB string `config:"db"`
Port int `config:"port"`
Socket string `config:"socket"`
NoReg bool `config:"noreg"`
Debug bool `config:"debug"`
Foreground bool `config:"foreground"`
UseCORS bool `config:"cors" json:"cors" yaml:"cors" toml:"cors"`
}
var cfg = config{
DB: "sf.db",
Port: 8888,
Debug: false,
NoReg: false,
Foreground: false,
UseCORS: false,
}
var (
signal = flag.Bool("stop", false, `shutdown server`)
migrate = flag.Bool("migrate", false, `perform DB migrations`)
ver = flag.Bool("v", false, `show version`)
cfgPath = flag.String("c", ".", `config file location`)
run = make(chan bool)
)
var loadedConfig = "using flags"
// Version string will be set by linker
var Version = "dev"
// BuildTime string will be set by linker
var BuildTime = "N/A"
func init() {
confBackends := []backend.Backend{}
cfgPath = getConfigFlag()
if *cfgPath == "env" {
confBackends = append(confBackends, env.NewBackend())
loadedConfig = "using environment"
} else {
configs := []string{"standardfile.json", "standardfile.toml", "standardfile.yaml"}
path := strings.TrimRight(*cfgPath, string(os.PathSeparator))
fileName := ""
if info, err := os.Stat(path); err == nil {
if !info.IsDir() {
fileName = filepath.Base(path)
}
}
if len(fileName) > 1 {
path = filepath.Dir(path)
configs = append([]string{fileName}, configs...)
}
for _, c := range configs {
if _, err := os.Stat(path + "/" + c); err == nil {
loadedConfig = path + "/" + c
confBackends = append(confBackends, file.NewBackend(loadedConfig))
break
}
}
}
confBackends = append(confBackends, flags.NewBackend())
loader := confita.NewLoader(confBackends...)
err := loader.Load(context.Background(), &cfg)
if err != nil {
fmt.Println(err)
}
}
func main() {
flag.Parse()
if *ver {
socket := "no"
if len(cfg.Socket) > 0 {
socket = cfg.Socket
}
fmt.Println(` Version: ` + Version + `
Built: ` + BuildTime + `
Go Version: ` + runtime.Version() + `
OS/Arch: ` + runtime.GOOS + "/" + runtime.GOARCH + `
Loaded Config: ` + loadedConfig + `
No Registrations: ` + strconv.FormatBool(cfg.NoReg) + `
CORS Enabled: ` + strconv.FormatBool(cfg.UseCORS) + `
Run in Foreground: ` + strconv.FormatBool(cfg.Foreground) + `
Webserver Port: ` + strconv.Itoa(cfg.Port) + `
Socket: ` + socket + `
DB Path: ` + cfg.DB + `
Debug: ` + strconv.FormatBool(cfg.Debug))
return
}
if *migrate {
Migrate()
return
}
if cfg.Port == 0 {
cfg.Port = 8888
}
if cfg.Foreground {
worker()
return
}
daemon.AddCommand(daemon.BoolFlag(signal), syscall.SIGTERM, termHandler)
cntxt := &daemon.Context{
PidFileName: "pid",
PidFilePerm: 0644,
LogFileName: "log",
LogFilePerm: 0640,
WorkDir: "./",
Umask: 027,
Args: nil,
}
if len(daemon.ActiveFlags()) > 0 {
d, err := cntxt.Search()
if err != nil {
log.Fatalln("Unable send signal to the daemon:", err)
}
log.Println("Stopping server")
daemon.SendCommands(d)
return
}
d, err := cntxt.Reborn()
if err != nil {
log.Fatalln(err)
}
if d != nil {
return
}
defer cntxt.Release()
go worker()
if err := daemon.ServeSignals(); err != nil {
log.Println("Error:", err)
}
}
func termHandler(sig os.Signal) error {
close(run)
return daemon.ErrStop
}
func getConfigFlag() *string {
cfg := "."
args := os.Args[1:]
if len(args) == 0 {
return &cfg
}
s := args[0]
if len(s) < 2 || s[0] != '-' {
return &cfg
}
numMinuses := 1
if s[1] == '-' {
numMinuses++
if len(s) == 2 { // "--" terminates the flags
return &cfg
}
}
name := s[numMinuses:]
if len(name) == 0 || name[0] == '-' || name[0] == '=' {
log.Fatalf("bad flag syntax: %s", s)
return &cfg
}
args = args[1:]
hasValue := false
value := ""
// possible to set it as -config=value or -c=value
for i := 1; i < len(name); i++ {
if name[i] == '=' {
value = name[i+1:]
hasValue = true
name = name[0:i]
break
}
}
//check if flag name is config
if name != "c" && name != "config" {
return &cfg
}
if !hasValue && len(args) > 0 {
// value is the next arg
hasValue = true
value = args[0]
}
cfg = value
return &cfg
}