whatismyip/server/server.go

63 lines
1002 B
Go
Raw Normal View History

2023-03-20 15:33:45 +00:00
package server
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/dcarrillo/whatismyip/internal/setting"
"github.com/dcarrillo/whatismyip/models"
)
type Server interface {
Start()
Stop()
}
2024-03-23 16:41:34 +00:00
type Manager struct {
servers []Server
2023-03-20 15:33:45 +00:00
}
2024-03-23 16:41:34 +00:00
func Setup(servers []Server) *Manager {
return &Manager{
servers: servers,
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
models.Setup(setting.App.GeodbPath.City, setting.App.GeodbPath.ASN)
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()
2023-03-20 15:33:45 +00:00
models.CloseDBs()
models.Setup(setting.App.GeodbPath.City, setting.App.GeodbPath.ASN)
2024-03-23 16:41:34 +00:00
m.start()
2023-03-20 15:33:45 +00:00
} else {
log.Printf("Shutting down...")
2024-03-23 16:41:34 +00:00
m.stop()
2023-03-20 15:33:45 +00:00
models.CloseDBs()
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
}
}