Merge pull request #18587 from calavera/daemon_configuration_file
Allow to set daemon and server configurations in a file. Upstream-commit: e44364eae90784b423eee8b2969bda9cd2429746 Component: engine
This commit is contained in:
30
components/engine/api/server/router_swapper.go
Normal file
30
components/engine/api/server/router_swapper.go
Normal file
@ -0,0 +1,30 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// routerSwapper is an http.Handler that allow you to swap
|
||||
// mux routers.
|
||||
type routerSwapper struct {
|
||||
mu sync.Mutex
|
||||
router *mux.Router
|
||||
}
|
||||
|
||||
// Swap changes the old router with the new one.
|
||||
func (rs *routerSwapper) Swap(newRouter *mux.Router) {
|
||||
rs.mu.Lock()
|
||||
rs.router = newRouter
|
||||
rs.mu.Unlock()
|
||||
}
|
||||
|
||||
// ServeHTTP makes the routerSwapper to implement the http.Handler interface.
|
||||
func (rs *routerSwapper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
rs.mu.Lock()
|
||||
router := rs.router
|
||||
rs.mu.Unlock()
|
||||
router.ServeHTTP(w, r)
|
||||
}
|
||||
@ -4,7 +4,6 @@ import (
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
@ -42,10 +41,11 @@ type Config struct {
|
||||
|
||||
// Server contains instance details for the server
|
||||
type Server struct {
|
||||
cfg *Config
|
||||
servers []*HTTPServer
|
||||
routers []router.Router
|
||||
authZPlugins []authorization.Plugin
|
||||
cfg *Config
|
||||
servers []*HTTPServer
|
||||
routers []router.Router
|
||||
authZPlugins []authorization.Plugin
|
||||
routerSwapper *routerSwapper
|
||||
}
|
||||
|
||||
// Addr contains string representation of address and its protocol (tcp, unix...).
|
||||
@ -80,12 +80,14 @@ func (s *Server) Close() {
|
||||
}
|
||||
}
|
||||
|
||||
// ServeAPI loops through all initialized servers and spawns goroutine
|
||||
// with Server method for each. It sets CreateMux() as Handler also.
|
||||
func (s *Server) ServeAPI() error {
|
||||
// serveAPI loops through all initialized servers and spawns goroutine
|
||||
// with Server method for each. It sets createMux() as Handler also.
|
||||
func (s *Server) serveAPI() error {
|
||||
s.initRouterSwapper()
|
||||
|
||||
var chErrors = make(chan error, len(s.servers))
|
||||
for _, srv := range s.servers {
|
||||
srv.srv.Handler = s.CreateMux()
|
||||
srv.srv.Handler = s.routerSwapper
|
||||
go func(srv *HTTPServer) {
|
||||
var err error
|
||||
logrus.Infof("API listen on %s", srv.l.Addr())
|
||||
@ -186,11 +188,11 @@ func (s *Server) addRouter(r router.Router) {
|
||||
s.routers = append(s.routers, r)
|
||||
}
|
||||
|
||||
// CreateMux initializes the main router the server uses.
|
||||
// createMux initializes the main router the server uses.
|
||||
// we keep enableCors just for legacy usage, need to be removed in the future
|
||||
func (s *Server) CreateMux() *mux.Router {
|
||||
func (s *Server) createMux() *mux.Router {
|
||||
m := mux.NewRouter()
|
||||
if os.Getenv("DEBUG") != "" {
|
||||
if utils.IsDebugEnabled() {
|
||||
profilerSetup(m, "/debug/")
|
||||
}
|
||||
|
||||
@ -207,3 +209,36 @@ func (s *Server) CreateMux() *mux.Router {
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// Wait blocks the server goroutine until it exits.
|
||||
// It sends an error message if there is any error during
|
||||
// the API execution.
|
||||
func (s *Server) Wait(waitChan chan error) {
|
||||
if err := s.serveAPI(); err != nil {
|
||||
logrus.Errorf("ServeAPI error: %v", err)
|
||||
waitChan <- err
|
||||
return
|
||||
}
|
||||
waitChan <- nil
|
||||
}
|
||||
|
||||
func (s *Server) initRouterSwapper() {
|
||||
s.routerSwapper = &routerSwapper{
|
||||
router: s.createMux(),
|
||||
}
|
||||
}
|
||||
|
||||
// Reload reads configuration changes and modifies the
|
||||
// server according to those changes.
|
||||
// Currently, only the --debug configuration is taken into account.
|
||||
func (s *Server) Reload(config *daemon.Config) {
|
||||
debugEnabled := utils.IsDebugEnabled()
|
||||
switch {
|
||||
case debugEnabled && !config.Debug: // disable debug
|
||||
utils.DisableDebug()
|
||||
s.routerSwapper.Swap(s.createMux())
|
||||
case config.Debug && !debugEnabled: // enable debug
|
||||
utils.EnableDebug()
|
||||
s.routerSwapper.Swap(s.createMux())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user