|
@@ -2,8 +2,8 @@ package guerrilla
|
|
|
|
|
|
import (
|
|
|
"errors"
|
|
|
- evbus "github.com/asaskevich/EventBus"
|
|
|
"github.com/flashmob/go-guerrilla/backends"
|
|
|
+ "github.com/flashmob/go-guerrilla/ev"
|
|
|
"github.com/flashmob/go-guerrilla/log"
|
|
|
"sync"
|
|
|
"sync/atomic"
|
|
@@ -36,9 +36,9 @@ func (e Errors) Error() string {
|
|
|
type Guerrilla interface {
|
|
|
Start() error
|
|
|
Shutdown()
|
|
|
- Subscribe(topic Event, fn interface{}) error
|
|
|
- Publish(topic Event, args ...interface{})
|
|
|
- Unsubscribe(topic Event, handler interface{}) error
|
|
|
+ Subscribe(topic ev.Event, fn interface{}) error
|
|
|
+ Publish(topic ev.Event, args ...interface{})
|
|
|
+ Unsubscribe(topic ev.Event, handler interface{}) error
|
|
|
SetLogger(log.Logger)
|
|
|
}
|
|
|
|
|
@@ -49,7 +49,7 @@ type guerrilla struct {
|
|
|
// guard controls access to g.servers
|
|
|
guard sync.Mutex
|
|
|
state int8
|
|
|
- bus *evbus.EventBus
|
|
|
+ ev.EventHandler
|
|
|
logStore
|
|
|
}
|
|
|
|
|
@@ -77,7 +77,6 @@ func New(ac *AppConfig, b backends.Backend, l log.Logger) (Guerrilla, error) {
|
|
|
Config: *ac, // take a local copy
|
|
|
servers: make(map[string]*server, len(ac.Servers)),
|
|
|
backend: b,
|
|
|
- bus: evbus.New(),
|
|
|
}
|
|
|
g.storeMainlog(l)
|
|
|
|
|
@@ -174,12 +173,12 @@ func (g *guerrilla) mapServers(callback func(*server)) map[string]*server {
|
|
|
func (g *guerrilla) subscribeEvents() {
|
|
|
|
|
|
// main config changed
|
|
|
- g.Subscribe(EvConfigNewConfig, func(c *AppConfig) {
|
|
|
+ g.Subscribe(ev.ConfigNewConfig, func(c *AppConfig) {
|
|
|
g.setConfig(c)
|
|
|
})
|
|
|
|
|
|
// allowed_hosts changed, set for all servers
|
|
|
- g.Subscribe(EvConfigAllowedHosts, func(c *AppConfig) {
|
|
|
+ g.Subscribe(ev.ConfigAllowedHosts, func(c *AppConfig) {
|
|
|
g.mapServers(func(server *server) {
|
|
|
server.setAllowedHosts(c.AllowedHosts)
|
|
|
})
|
|
@@ -187,7 +186,7 @@ func (g *guerrilla) subscribeEvents() {
|
|
|
})
|
|
|
|
|
|
// the main log file changed
|
|
|
- g.Subscribe(EvConfigLogFile, func(c *AppConfig) {
|
|
|
+ g.Subscribe(ev.ConfigLogFile, func(c *AppConfig) {
|
|
|
var err error
|
|
|
var l log.Logger
|
|
|
if l, err = log.GetLogger(c.LogFile); err == nil {
|
|
@@ -204,13 +203,13 @@ func (g *guerrilla) subscribeEvents() {
|
|
|
})
|
|
|
|
|
|
// re-open the main log file (file not changed)
|
|
|
- g.Subscribe(EvConfigLogReopen, func(c *AppConfig) {
|
|
|
+ g.Subscribe(ev.ConfigLogReopen, func(c *AppConfig) {
|
|
|
g.mainlog().Reopen()
|
|
|
g.mainlog().Infof("re-opened main log file [%s]", c.LogFile)
|
|
|
})
|
|
|
|
|
|
// when log level changes, apply to mainlog and server logs
|
|
|
- g.Subscribe(EvConfigLogLevel, func(c *AppConfig) {
|
|
|
+ g.Subscribe(ev.ConfigLogLevel, func(c *AppConfig) {
|
|
|
g.mainlog().SetLevel(c.LogLevel)
|
|
|
g.mapServers(func(server *server) {
|
|
|
server.log.SetLevel(c.LogLevel)
|
|
@@ -219,12 +218,12 @@ func (g *guerrilla) subscribeEvents() {
|
|
|
})
|
|
|
|
|
|
// server config was updated
|
|
|
- g.Subscribe(EvConfigServerConfig, func(sc *ServerConfig) {
|
|
|
+ g.Subscribe(ev.ConfigServerConfig, func(sc *ServerConfig) {
|
|
|
g.setServerConfig(sc)
|
|
|
})
|
|
|
|
|
|
// add a new server to the config & start
|
|
|
- g.Subscribe(EvConfigEvServerNew, func(sc *ServerConfig) {
|
|
|
+ g.Subscribe(ev.ConfigEvServerNew, func(sc *ServerConfig) {
|
|
|
if _, err := g.findServer(sc.ListenInterface); err != nil {
|
|
|
// not found, lets add it
|
|
|
if err := g.makeServers(); err != nil {
|
|
@@ -241,7 +240,7 @@ func (g *guerrilla) subscribeEvents() {
|
|
|
}
|
|
|
})
|
|
|
// start a server that already exists in the config and has been enabled
|
|
|
- g.Subscribe(EvConfigServerStart, func(sc *ServerConfig) {
|
|
|
+ g.Subscribe(ev.ConfigServerStart, func(sc *ServerConfig) {
|
|
|
if server, err := g.findServer(sc.ListenInterface); err == nil {
|
|
|
if server.state == ServerStateStopped || server.state == ServerStateNew {
|
|
|
g.mainlog().Infof("Starting server [%s]", server.listenInterface)
|
|
@@ -253,7 +252,7 @@ func (g *guerrilla) subscribeEvents() {
|
|
|
}
|
|
|
})
|
|
|
// stop running a server
|
|
|
- g.Subscribe(EvConfigServerStop, func(sc *ServerConfig) {
|
|
|
+ g.Subscribe(ev.ConfigServerStop, func(sc *ServerConfig) {
|
|
|
if server, err := g.findServer(sc.ListenInterface); err == nil {
|
|
|
if server.state == ServerStateRunning {
|
|
|
server.Shutdown()
|
|
@@ -262,7 +261,7 @@ func (g *guerrilla) subscribeEvents() {
|
|
|
}
|
|
|
})
|
|
|
// server was removed from config
|
|
|
- g.Subscribe(EvConfigServerRemove, func(sc *ServerConfig) {
|
|
|
+ g.Subscribe(ev.ConfigServerRemove, func(sc *ServerConfig) {
|
|
|
if server, err := g.findServer(sc.ListenInterface); err == nil {
|
|
|
server.Shutdown()
|
|
|
g.removeServer(sc.ListenInterface)
|
|
@@ -271,7 +270,7 @@ func (g *guerrilla) subscribeEvents() {
|
|
|
})
|
|
|
|
|
|
// TLS changes
|
|
|
- g.Subscribe(EvConfigServerTLSConfig, func(sc *ServerConfig) {
|
|
|
+ g.Subscribe(ev.ConfigServerTLSConfig, func(sc *ServerConfig) {
|
|
|
if server, err := g.findServer(sc.ListenInterface); err == nil {
|
|
|
if err := server.configureSSL(); err == nil {
|
|
|
g.mainlog().Infof("Server [%s] new TLS configuration loaded", sc.ListenInterface)
|
|
@@ -281,19 +280,19 @@ func (g *guerrilla) subscribeEvents() {
|
|
|
}
|
|
|
})
|
|
|
// when server's timeout change.
|
|
|
- g.Subscribe(EvConfigServerTimeout, func(sc *ServerConfig) {
|
|
|
+ g.Subscribe(ev.ConfigServerTimeout, func(sc *ServerConfig) {
|
|
|
g.mapServers(func(server *server) {
|
|
|
server.setTimeout(sc.Timeout)
|
|
|
})
|
|
|
})
|
|
|
// when server's max clients change.
|
|
|
- g.Subscribe(EvConfigServerMaxClients, func(sc *ServerConfig) {
|
|
|
+ g.Subscribe(ev.ConfigServerMaxClients, func(sc *ServerConfig) {
|
|
|
g.mapServers(func(server *server) {
|
|
|
// TODO resize the pool somehow
|
|
|
})
|
|
|
})
|
|
|
// when a server's log file changes
|
|
|
- g.Subscribe(EvConfigServerLogFile, func(sc *ServerConfig) {
|
|
|
+ g.Subscribe(ev.ConfigServerLogFile, func(sc *ServerConfig) {
|
|
|
if server, err := g.findServer(sc.ListenInterface); err == nil {
|
|
|
var err error
|
|
|
var l log.Logger
|
|
@@ -315,7 +314,7 @@ func (g *guerrilla) subscribeEvents() {
|
|
|
}
|
|
|
})
|
|
|
// when the daemon caught a sighup, event for individual server
|
|
|
- g.Subscribe(EvConfigServerLogReopen, func(sc *ServerConfig) {
|
|
|
+ g.Subscribe(ev.ConfigServerLogReopen, func(sc *ServerConfig) {
|
|
|
if server, err := g.findServer(sc.ListenInterface); err == nil {
|
|
|
server.log.Reopen()
|
|
|
g.mainlog().Infof("Server [%s] re-opened log file [%s]", sc.ListenInterface, sc.LogFile)
|
|
@@ -398,18 +397,6 @@ func (g *guerrilla) Shutdown() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (g *guerrilla) Subscribe(topic Event, fn interface{}) error {
|
|
|
- return g.bus.Subscribe(topic.String(), fn)
|
|
|
-}
|
|
|
-
|
|
|
-func (g *guerrilla) Publish(topic Event, args ...interface{}) {
|
|
|
- g.bus.Publish(topic.String(), args...)
|
|
|
-}
|
|
|
-
|
|
|
-func (g *guerrilla) Unsubscribe(topic Event, handler interface{}) error {
|
|
|
- return g.bus.Unsubscribe(topic.String(), handler)
|
|
|
-}
|
|
|
-
|
|
|
func (g *guerrilla) SetLogger(l log.Logger) {
|
|
|
g.storeMainlog(l)
|
|
|
}
|