-
Notifications
You must be signed in to change notification settings - Fork 4
/
gudgeon.go
193 lines (160 loc) · 4.34 KB
/
gudgeon.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
package main
import (
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"syscall"
log "github.com/sirupsen/logrus"
"github.com/chrisruffalo/gudgeon/config"
"github.com/chrisruffalo/gudgeon/engine"
"github.com/chrisruffalo/gudgeon/events"
"github.com/chrisruffalo/gudgeon/provider"
"github.com/chrisruffalo/gudgeon/util"
"github.com/chrisruffalo/gudgeon/version"
"github.com/chrisruffalo/gudgeon/web"
)
// default divider
const divider = "==============================="
// Gudgeon Core Gudgeon object for executing a Gudgeon process
type Gudgeon struct {
confPath string
config *config.GudgeonConfig
engine engine.Engine
provider provider.Provider
web web.Web
}
// NewGudgeon Create a new Gudgeon instance from a given Gudgeon Config
func NewGudgeon(confPath *string, config *config.GudgeonConfig) *Gudgeon {
return &Gudgeon{
confPath: *confPath,
config: config,
}
}
func (gudgeon *Gudgeon) Start() error {
// error
var err error
// start the event bus
events.Start()
// start the file watcher
events.StartFileWatch()
// create engine which handles resolution, logging, etc
gudgeon.engine, err = engine.NewReloadingEngine(gudgeon.confPath, gudgeon.config)
if err != nil {
return err
}
if gudgeon.engine == nil {
return fmt.Errorf("Could not create required engine component")
}
// create a new provider and start hosting
gudgeon.provider = provider.NewProvider(gudgeon.engine)
err = gudgeon.provider.Host(gudgeon.config, gudgeon.engine)
if err != nil {
return fmt.Errorf("Could not create provider: %s", err)
}
// open web ui if web enabled
if gudgeon.config.Web.Enabled {
gudgeon.web = web.New()
err = gudgeon.web.Serve(gudgeon.config, gudgeon.engine)
if err != nil {
return fmt.Errorf("Could not host web: %s", err)
}
}
// try and print out error if we caught one during startup
if recovery := recover(); recovery != nil {
return fmt.Errorf("unrecoverable error: %s", recovery)
}
return nil
}
func (gudgeon *Gudgeon) Shutdown() {
// stop the file watcher
events.StopFileWatch()
// stop the event bus
events.Stop()
// stop providers
if gudgeon.provider != nil {
log.Infof("Shutting down DNS endpoints...")
err := gudgeon.provider.Shutdown()
if err != nil {
log.Errorf("Could not shutdown endpoints: %s", err)
}
}
// stop web
if gudgeon.web != nil {
log.Infof("Shutting down Web service...")
gudgeon.web.Stop()
}
// stop/shutdown engine
log.Infof("Shutting down Engine...")
gudgeon.engine.Shutdown()
}
func main() {
// set initial log instance configuration
log.SetOutput(os.Stdout)
log.SetLevel(log.InfoLevel)
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
})
// load command options
opts, err := config.Options(version.GetLongVersion())
if err != nil {
log.Errorf("%s", err)
os.Exit(1)
}
// debug print config
log.Info(divider)
log.Infof("Gudgeon %s", version.GetLongVersion())
log.Info(divider)
// start profiling if enabled
if opts.DebugOptions.Profile {
// start profile http endpoint on given port
profPort := "9900"
go func() {
err := http.ListenAndServe("127.0.0.1:"+profPort, nil)
if err != nil {
log.Errorf("Could not expose HTTP profile endpoint on %s: %s", profPort, err)
}
}()
}
// load config
filename := string(opts.AppOptions.ConfigPath)
conf, warnings, err := config.Load(filename)
if err != nil {
log.Errorf("%s", err)
os.Exit(1)
}
// configure log file from configuration if additional configuration is available
// print log warnings and continue
if len(warnings) > 0 {
for _, warn := range warnings {
log.Warn(warn)
}
}
// print log file information
if "" != filename {
log.Infof("Configuration file: %s", filename)
}
// create new Gudgeon instance
instance := NewGudgeon(&filename, conf)
// start new instance
err = instance.Start()
if err != nil {
log.Errorf("Error starting Gudgeon: %s", err)
os.Exit(1)
}
// wait for signal
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
s := <-sig
// clean out session directory
if "" != conf.SessionRoot() {
util.ClearDirectory(conf.SessionRoot())
}
log.Infof("Signal (%s) received, stopping", s)
// stop gudgeon, hopefully gracefully
instance.Shutdown()
// debugging: print any still-running goroutines
//pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
}