-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
225 lines (185 loc) · 4.66 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
package main
import (
"flag"
"fmt"
"log"
"log/syslog"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"github.com/jonathantorres/ftr/internal/conf"
"github.com/jonathantorres/ftr/internal/logger"
"github.com/jonathantorres/ftr/internal/server"
)
const version = "0.1.0"
func main() {
// make sure the prefix ends in "/"
if !strings.HasSuffix(server.Prefix, "/") {
server.Prefix += "/"
}
confF := parseFlags()
log.SetPrefix(logger.Prefix)
// connect to system log
sys, err := syslog.New(syslog.LOG_ERR|syslog.LOG_DAEMON, strings.TrimRight(logger.Prefix, ":"))
if err != nil {
log.Fatalf("error connecting to syslog: %s\n", err)
}
defer sys.Close()
err = openPidFile()
if err != nil {
msg := fmt.Sprintf("error opening pid file: %s\n", err)
sys.Err(msg)
log.Fatal(msg)
}
for {
config, err := conf.Load(confF, server.Prefix)
if err != nil {
msg := fmt.Sprintf("server configuration error: %s\n", err)
sys.Err(msg)
log.Fatal(msg)
}
errLog, accessLog, err := logger.Load(config)
if err != nil {
msg := fmt.Sprintf("server logging error: %s\n", err)
sys.Err(msg)
log.Fatal(msg)
}
s := &server.Server{
Conf: config,
AccessLog: accessLog,
ErrorLog: errLog,
}
go handleSignals(s)
err = s.Start()
if err != nil {
log.Fatalf("server error: %s", err)
}
if s.IsReloading {
continue
}
break
}
}
func parseFlags() string {
var (
versionF bool
helpF bool
testF bool
daemonF bool
prefixF string
confF string
)
flag.BoolVar(&versionF, "v", false, "Show the server version and exit")
flag.BoolVar(&helpF, "h", false, "Show the help contents and exit")
flag.BoolVar(&testF, "t", false, "Test the configuration file and exit")
flag.BoolVar(&daemonF, "d", false, "Run the server in the background (as a daemon)")
flag.StringVar(&prefixF, "p", server.Prefix, "Set the path of the prefix")
flag.StringVar(&confF, "c", server.Prefix+server.DefaultConf, "Use the specified configuration file")
flag.Usage = usage
flag.Parse()
// just print the version and exit
if versionF {
fmt.Fprintf(os.Stderr, "ftr version v%s\n", version)
os.Exit(0)
}
// just print the help and exit
if helpF {
usage()
os.Exit(0)
}
var prefixUsed bool
flag.Visit(func(f *flag.Flag) {
if f.Name == "p" {
prefixUsed = true
}
})
// if prefix was set on the command line,
// then the location of the configuration
// will be based on this prefix
if prefixUsed {
server.Prefix = prefixF
confF = server.Prefix + server.DefaultConf
}
// test the configuration file and exit
if testF {
fmt.Fprintf(os.Stderr, "testing configuration file...")
_, err := conf.Load(confF, server.Prefix)
if err != nil {
fmt.Fprintf(os.Stderr, "failed: %s\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "OK.\n")
os.Exit(0)
}
// run as a daemon
if daemonF {
err := runDaemon()
if err != nil {
fmt.Fprintf(os.Stderr, "there was a problem starting the daemon: %s\n", err)
os.Exit(1)
}
os.Exit(0)
}
return confF
}
func runDaemon() error {
args := os.Args
prog := args[0]
// remove the -d option
for i, arg := range args {
if arg == "-d" {
args = append(args[:i], args[i+1:]...)
break
}
}
cmd := exec.Command(prog)
err := cmd.Start()
if err != nil {
return err
}
return nil
}
func openPidFile() error {
filename := server.Prefix + "/ftr.pid"
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(fmt.Sprintf("%d\n", os.Getpid()))
if err != nil {
return err
}
return nil
}
func handleSignals(serv *server.Server) {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP)
s := <-sig
switch s {
case syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT:
serv.Shutdown()
case syscall.SIGHUP:
err := serv.Reload()
if err != nil {
// there was a problem reloading the configuration file
// the server will keep running with the old configuration
// fire up this goroutine again so we can keep handling signals
go handleSignals(serv)
}
}
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage: ftr -[dhtv] [-p prefix] [-c conf]\n\n")
fmt.Fprintf(os.Stderr, "Options:\n")
fmt.Fprintf(os.Stderr, " -h\t\t: This help menu\n")
fmt.Fprintf(os.Stderr, " -v\t\t: Show server version and exit\n")
fmt.Fprintf(os.Stderr, " -t\t\t: Test the configuration file and exit\n")
fmt.Fprintf(os.Stderr, " -d\t\t: Run the server in the background (as a daemon)\n")
fmt.Fprintf(os.Stderr, " -p prefix\t: Set the path of the prefix\n")
fmt.Fprintf(os.Stderr, " -c filename\t: Use the specified configuration file\n")
}