refactoring server
This commit is contained in:
parent
ce1d14d779
commit
9edd4874ac
1 changed files with 53 additions and 37 deletions
90
traefik.go
90
traefik.go
|
@ -47,6 +47,7 @@ func main() {
|
||||||
defer close(configurationChan)
|
defer close(configurationChan)
|
||||||
var providers = []Provider{}
|
var providers = []Provider{}
|
||||||
var sigs = make(chan os.Signal, 1)
|
var sigs = make(chan os.Signal, 1)
|
||||||
|
defer close(sigs)
|
||||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
// load global configuration
|
// load global configuration
|
||||||
|
@ -91,7 +92,9 @@ func main() {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
currentConfiguration = configuration
|
currentConfiguration = configuration
|
||||||
configurationRouter = newConfigurationRouter
|
configurationRouter = newConfigurationRouter
|
||||||
srv.Stop(time.Duration(globalConfiguration.GraceTimeOut) * time.Second)
|
oldServer := srv
|
||||||
|
srv = prepareServer(configurationRouter, globalConfiguration, loggerMiddleware, metrics)
|
||||||
|
stopServer(oldServer, globalConfiguration)
|
||||||
time.Sleep(3 * time.Second)
|
time.Sleep(3 * time.Second)
|
||||||
} else {
|
} else {
|
||||||
log.Error("Error loading new configuration, aborted ", err)
|
log.Error("Error loading new configuration, aborted ", err)
|
||||||
|
@ -135,50 +138,20 @@ func main() {
|
||||||
sig := <-sigs
|
sig := <-sigs
|
||||||
log.Infof("I have to go... %+v", sig)
|
log.Infof("I have to go... %+v", sig)
|
||||||
goAway = true
|
goAway = true
|
||||||
srv.Stop(time.Duration(globalConfiguration.GraceTimeOut) * time.Second)
|
stopServer(srv, globalConfiguration)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
//negroni.Use(middlewares.NewCircuitBreaker(oxyLogger))
|
||||||
|
//negroni.Use(middlewares.NewRoutes(configurationRouter))
|
||||||
|
srv = prepareServer(configurationRouter, globalConfiguration, loggerMiddleware, metrics)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if goAway {
|
if goAway {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// middlewares
|
|
||||||
var negroni = negroni.New()
|
|
||||||
negroni.Use(metrics)
|
|
||||||
negroni.Use(loggerMiddleware)
|
|
||||||
//negroni.Use(middlewares.NewCircuitBreaker(oxyLogger))
|
|
||||||
//negroni.Use(middlewares.NewRoutes(configurationRouter))
|
|
||||||
negroni.UseHandler(configurationRouter)
|
|
||||||
|
|
||||||
srv = &graceful.Server{
|
|
||||||
Timeout: time.Duration(globalConfiguration.GraceTimeOut) * time.Second,
|
|
||||||
NoSignalHandling: true,
|
|
||||||
|
|
||||||
Server: &http.Server{
|
|
||||||
Addr: globalConfiguration.Port,
|
|
||||||
Handler: negroni,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
if len(globalConfiguration.CertFile) > 0 && len(globalConfiguration.KeyFile) > 0 {
|
startServer(srv, globalConfiguration)
|
||||||
err := srv.ListenAndServeTLS(globalConfiguration.CertFile, globalConfiguration.KeyFile)
|
|
||||||
if err != nil {
|
|
||||||
netOpError, ok := err.(*net.OpError)
|
|
||||||
if ok && netOpError.Err.Error() != "use of closed network connection" {
|
|
||||||
log.Fatal("Error creating server: ", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
err := srv.ListenAndServe()
|
|
||||||
if err != nil {
|
|
||||||
netOpError, ok := err.(*net.OpError)
|
|
||||||
if ok && netOpError.Err.Error() != "use of closed network connection" {
|
|
||||||
log.Fatal("Error creating server: ", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
log.Info("Started")
|
log.Info("Started")
|
||||||
<-srv.StopChan()
|
<-srv.StopChan()
|
||||||
|
@ -186,6 +159,49 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func startServer(srv *graceful.Server, globalConfiguration *GlobalConfiguration){
|
||||||
|
if len(globalConfiguration.CertFile) > 0 && len(globalConfiguration.KeyFile) > 0 {
|
||||||
|
err := srv.ListenAndServeTLS(globalConfiguration.CertFile, globalConfiguration.KeyFile)
|
||||||
|
if err != nil {
|
||||||
|
netOpError, ok := err.(*net.OpError)
|
||||||
|
if ok && netOpError.Err.Error() != "use of closed network connection" {
|
||||||
|
log.Fatal("Error creating server: ", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err := srv.ListenAndServe()
|
||||||
|
if err != nil {
|
||||||
|
netOpError, ok := err.(*net.OpError)
|
||||||
|
if ok && netOpError.Err.Error() != "use of closed network connection" {
|
||||||
|
log.Fatal("Error creating server: ", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func stopServer(srv *graceful.Server, globalConfiguration *GlobalConfiguration){
|
||||||
|
srv.Stop(time.Duration(globalConfiguration.GraceTimeOut) * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
func prepareServer(router *mux.Router, globalConfiguration *GlobalConfiguration, middlewares...negroni.Handler) (*graceful.Server){
|
||||||
|
// middlewares
|
||||||
|
var negroni = negroni.New()
|
||||||
|
for _, middleware := range middlewares {
|
||||||
|
negroni.Use(middleware)
|
||||||
|
}
|
||||||
|
negroni.UseHandler(router)
|
||||||
|
|
||||||
|
return &graceful.Server{
|
||||||
|
Timeout: time.Duration(globalConfiguration.GraceTimeOut) * time.Second,
|
||||||
|
NoSignalHandling: true,
|
||||||
|
|
||||||
|
Server: &http.Server{
|
||||||
|
Addr: globalConfiguration.Port,
|
||||||
|
Handler: negroni,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func LoadConfig(configuration *Configuration, globalConfiguration *GlobalConfiguration) (*mux.Router, error) {
|
func LoadConfig(configuration *Configuration, globalConfiguration *GlobalConfiguration) (*mux.Router, error) {
|
||||||
router := mux.NewRouter()
|
router := mux.NewRouter()
|
||||||
router.NotFoundHandler = http.HandlerFunc(notFoundHandler)
|
router.NotFoundHandler = http.HandlerFunc(notFoundHandler)
|
||||||
|
|
Loading…
Add table
Reference in a new issue