mirror of
https://github.com/dcarrillo/whatismyip.git
synced 2025-07-01 06:59:27 +00:00
Server handling refactor (#27)
This commit is contained in:
@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
@ -9,20 +10,20 @@ import (
|
||||
"github.com/quic-go/quic-go/http3"
|
||||
)
|
||||
|
||||
type QuicServer struct {
|
||||
type Quic struct {
|
||||
server *http3.Server
|
||||
tlsServer *TLSServer
|
||||
tlsServer *TLS
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewQuicServer(ctx context.Context, tlsServer *TLSServer) *QuicServer {
|
||||
return &QuicServer{
|
||||
func NewQuicServer(ctx context.Context, tlsServer *TLS) *Quic {
|
||||
return &Quic{
|
||||
tlsServer: tlsServer,
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func (q *QuicServer) Start() {
|
||||
func (q *Quic) Start() {
|
||||
q.server = &http3.Server{
|
||||
Addr: setting.App.TLSAddress,
|
||||
Handler: q.tlsServer.server.Handler,
|
||||
@ -40,13 +41,13 @@ func (q *QuicServer) Start() {
|
||||
log.Printf("Starting QUIC server listening on %s (udp)", setting.App.TLSAddress)
|
||||
go func() {
|
||||
if err := q.server.ListenAndServeTLS(setting.App.TLSCrtPath, setting.App.TLSKeyPath); err != nil &&
|
||||
err.Error() != "quic: Server closed" {
|
||||
!errors.Is(err, http.ErrServerClosed) {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (q *QuicServer) Stop() {
|
||||
func (q *Quic) Stop() {
|
||||
log.Printf("Stopping QUIC server...")
|
||||
if err := q.server.Close(); err != nil {
|
||||
log.Printf("QUIC server forced to shutdown")
|
@ -2,14 +2,12 @@ package server
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/dcarrillo/whatismyip/internal/setting"
|
||||
"github.com/dcarrillo/whatismyip/models"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type Server interface {
|
||||
@ -17,80 +15,47 @@ type Server interface {
|
||||
Stop()
|
||||
}
|
||||
|
||||
type Factory struct {
|
||||
tcpServer *TCPServer
|
||||
tlsServer *TLSServer
|
||||
quicServer *QuicServer
|
||||
type Manager struct {
|
||||
servers []Server
|
||||
}
|
||||
|
||||
func Setup(ctx context.Context, handler http.Handler) *Factory {
|
||||
var tcpServer *TCPServer
|
||||
var tlsServer *TLSServer
|
||||
var quicServer *QuicServer
|
||||
|
||||
if setting.App.BindAddress != "" {
|
||||
tcpServer = NewTCPServer(ctx, &handler)
|
||||
}
|
||||
|
||||
if setting.App.TLSAddress != "" {
|
||||
tlsServer = NewTLSServer(ctx, &handler)
|
||||
if setting.App.EnableHTTP3 {
|
||||
quicServer = NewQuicServer(ctx, tlsServer)
|
||||
}
|
||||
}
|
||||
|
||||
return &Factory{
|
||||
tcpServer: tcpServer,
|
||||
tlsServer: tlsServer,
|
||||
quicServer: quicServer,
|
||||
func Setup(servers []Server) *Manager {
|
||||
return &Manager{
|
||||
servers: servers,
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Factory) Run() {
|
||||
f.start()
|
||||
func (m *Manager) Run() {
|
||||
m.start()
|
||||
|
||||
signalChan := make(chan os.Signal, 3)
|
||||
signalChan := make(chan os.Signal, len(m.servers))
|
||||
signal.Notify(signalChan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
|
||||
var s os.Signal
|
||||
for {
|
||||
s = <-signalChan
|
||||
|
||||
if s == syscall.SIGHUP {
|
||||
f.stop()
|
||||
m.stop()
|
||||
models.CloseDBs()
|
||||
models.Setup(setting.App.GeodbPath.City, setting.App.GeodbPath.ASN)
|
||||
f.start()
|
||||
m.start()
|
||||
} else {
|
||||
log.Printf("Shutting down...")
|
||||
f.stop()
|
||||
m.stop()
|
||||
models.CloseDBs()
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Factory) start() {
|
||||
if f.tcpServer != nil {
|
||||
f.tcpServer.Start()
|
||||
}
|
||||
|
||||
if f.tlsServer != nil {
|
||||
f.tlsServer.Start()
|
||||
if f.quicServer != nil {
|
||||
f.quicServer.Start()
|
||||
}
|
||||
func (m *Manager) start() {
|
||||
for _, s := range m.servers {
|
||||
s.Start()
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Factory) stop() {
|
||||
if f.tcpServer != nil {
|
||||
f.tcpServer.Stop()
|
||||
}
|
||||
|
||||
if f.tlsServer != nil {
|
||||
if f.quicServer != nil {
|
||||
f.quicServer.Stop()
|
||||
}
|
||||
f.tlsServer.Stop()
|
||||
func (m *Manager) stop() {
|
||||
for _, s := range m.servers {
|
||||
s.Stop()
|
||||
}
|
||||
}
|
||||
|
@ -9,20 +9,20 @@ import (
|
||||
"github.com/dcarrillo/whatismyip/internal/setting"
|
||||
)
|
||||
|
||||
type TCPServer struct {
|
||||
type TCP struct {
|
||||
server *http.Server
|
||||
handler *http.Handler
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewTCPServer(ctx context.Context, handler *http.Handler) *TCPServer {
|
||||
return &TCPServer{
|
||||
func NewTCPServer(ctx context.Context, handler *http.Handler) *TCP {
|
||||
return &TCP{
|
||||
handler: handler,
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TCPServer) Start() {
|
||||
func (t *TCP) Start() {
|
||||
t.server = &http.Server{
|
||||
Addr: setting.App.BindAddress,
|
||||
Handler: *t.handler,
|
||||
@ -38,7 +38,7 @@ func (t *TCPServer) Start() {
|
||||
}()
|
||||
}
|
||||
|
||||
func (t *TCPServer) Stop() {
|
||||
func (t *TCP) Stop() {
|
||||
log.Printf("Stopping TCP server...")
|
||||
if err := t.server.Shutdown(t.ctx); err != nil {
|
||||
log.Printf("TCP server forced to shutdown: %s", err)
|
@ -9,20 +9,20 @@ import (
|
||||
"github.com/dcarrillo/whatismyip/internal/setting"
|
||||
)
|
||||
|
||||
type TLSServer struct {
|
||||
type TLS struct {
|
||||
server *http.Server
|
||||
handler *http.Handler
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewTLSServer(ctx context.Context, handler *http.Handler) *TLSServer {
|
||||
return &TLSServer{
|
||||
func NewTLSServer(ctx context.Context, handler *http.Handler) *TLS {
|
||||
return &TLS{
|
||||
handler: handler,
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TLSServer) Start() {
|
||||
func (t *TLS) Start() {
|
||||
t.server = &http.Server{
|
||||
Addr: setting.App.TLSAddress,
|
||||
Handler: *t.handler,
|
||||
@ -39,7 +39,7 @@ func (t *TLSServer) Start() {
|
||||
}()
|
||||
}
|
||||
|
||||
func (t *TLSServer) Stop() {
|
||||
func (t *TLS) Stop() {
|
||||
log.Printf("Stopping TLS server...")
|
||||
if err := t.server.Shutdown(t.ctx); err != nil {
|
||||
log.Printf("TLS server forced to shutdown: %s", err)
|
Reference in New Issue
Block a user