whatismyip/server/server.go

66 lines
934 B
Go
Raw Normal View History

2023-03-20 15:33:45 +00:00
package server
import (
"log"
"os"
"os/signal"
"syscall"
2025-01-02 19:13:41 +00:00
"github.com/dcarrillo/whatismyip/service"
2023-03-20 15:33:45 +00:00
)
type Server interface {
Start()
Stop()
}
2024-03-23 16:41:34 +00:00
type Manager struct {
servers []Server
2025-01-02 19:13:41 +00:00
geoSvc *service.Geo
2023-03-20 15:33:45 +00:00
}
2025-01-02 19:13:41 +00:00
func Setup(servers []Server, geoSvc *service.Geo) *Manager {
2024-03-23 16:41:34 +00:00
return &Manager{
servers: servers,
2025-01-02 19:13:41 +00:00
geoSvc: geoSvc,
2023-03-20 15:33:45 +00:00
}
}
2024-03-23 16:41:34 +00:00
func (m *Manager) Run() {
m.start()
2023-03-20 15:33:45 +00:00
2024-03-23 16:41:34 +00:00
signalChan := make(chan os.Signal, len(m.servers))
2023-03-20 15:33:45 +00:00
signal.Notify(signalChan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
var s os.Signal
for {
s = <-signalChan
if s == syscall.SIGHUP {
2024-03-23 16:41:34 +00:00
m.stop()
2025-01-02 19:13:41 +00:00
if m.geoSvc != nil {
m.geoSvc.Reload()
}
2024-03-23 16:41:34 +00:00
m.start()
2023-03-20 15:33:45 +00:00
} else {
2025-01-02 19:13:41 +00:00
log.Print("Shutting down...")
if m.geoSvc != nil {
m.geoSvc.Shutdown()
}
2024-03-23 16:41:34 +00:00
m.stop()
2023-03-20 15:33:45 +00:00
break
}
}
}
2024-03-23 16:41:34 +00:00
func (m *Manager) start() {
for _, s := range m.servers {
s.Start()
2023-03-20 15:33:45 +00:00
}
}
2024-03-23 16:41:34 +00:00
func (m *Manager) stop() {
for _, s := range m.servers {
s.Stop()
2023-03-20 15:33:45 +00:00
}
}