This repository has been archived by the owner on Jan 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
86 lines (72 loc) · 1.7 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
// Gnocco is a little cache of goodness
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
log "github.com/karasz/gnocco/cblog"
)
//go:generate go run genroot.go
var (
logger *log.Logger
//Version contains the git hashtag injected by make
Version = "N/A"
//BuildDate contains the build timestamp injected by make
BuildDate = "N/A"
)
func main() {
var confFile string
var vrs bool
flag.StringVar(&confFile, "f", "", "specify the config file, if empty will try gnocco.conf and /etc/gnocco/gnocco.conf.")
flag.BoolVar(&vrs, "v", false, "program version")
flag.Parse()
if vrs {
fmt.Fprintf(os.Stdout, "Gnocco version %s, build date %s\n", Version, BuildDate)
os.Exit(0)
}
cf, err := loadConfig(confFile)
if err != nil {
panic(err)
}
logger = initLogger()
aserver := &server{
host: cf.Listen.Host,
port: cf.Listen.Port,
maxjobs: cf.MaxJobs,
maxqueries: cf.MaxQueries,
}
aserver.run()
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan)
for {
sign := <-signalChan
switch sign {
case syscall.SIGTERM:
aserver.shutDown()
logger.Fatal("Got SIGTERM, stoping as requested")
case syscall.SIGINT:
aserver.shutDown()
logger.Fatal("Got SIGINT, stoping as requested")
case syscall.SIGUSR2:
logger.Info("Got SIGUSR2, dumping cache")
aserver.dumpCache()
case syscall.SIGURG:
default:
logger.Warn("I received %v signal", sign)
}
}
}
func initLogger() *log.Logger {
logger = log.New()
if mainconfig.Log.Stdout {
logger.SetLogger("console", nil)
}
if mainconfig.Log.File != "" {
cfg := map[string]interface{}{"file": mainconfig.Log.File}
logger.SetLogger("file", cfg)
logger.Info("Logger started")
}
return logger
}