chore(server): use httpserver package for control server

This commit is contained in:
Quentin McGaw
2022-03-30 07:45:11 +00:00
parent 8186ef2342
commit 84607e332b
3 changed files with 21 additions and 43 deletions

View File

@@ -424,10 +424,15 @@ func _main(ctx context.Context, buildInfo models.BuildInformation,
controlServerLogging := *allSettings.ControlServer.Log controlServerLogging := *allSettings.ControlServer.Log
httpServerHandler, httpServerCtx, httpServerDone := goshutdown.NewGoRoutineHandler( httpServerHandler, httpServerCtx, httpServerDone := goshutdown.NewGoRoutineHandler(
"http server", goroutine.OptionTimeout(defaultShutdownTimeout)) "http server", goroutine.OptionTimeout(defaultShutdownTimeout))
httpServer := server.New(httpServerCtx, controlServerAddress, controlServerLogging, httpServer, err := server.New(httpServerCtx, controlServerAddress, controlServerLogging,
logger.NewChild(logging.Settings{Prefix: "http server: "}), logger.NewChild(logging.Settings{Prefix: "http server: "}),
buildInfo, vpnLooper, portForwardLooper, unboundLooper, updaterLooper, publicIPLooper) buildInfo, vpnLooper, portForwardLooper, unboundLooper, updaterLooper, publicIPLooper)
go httpServer.Run(httpServerCtx, httpServerDone) if err != nil {
return fmt.Errorf("cannot setup control server: %w", err)
}
httpServerReady := make(chan struct{})
go httpServer.Run(httpServerCtx, httpServerReady, httpServerDone)
<-httpServerReady
controlGroupHandler.Add(httpServerHandler) controlGroupHandler.Add(httpServerHandler)
healthLogger := logger.NewChild(logging.Settings{Prefix: "healthcheck: "}) healthLogger := logger.NewChild(logging.Settings{Prefix: "healthcheck: "})

View File

@@ -6,11 +6,6 @@ type Logger interface {
errorer errorer
} }
type infoErrorer interface {
infoer
errorer
}
type infoWarner interface { type infoWarner interface {
infoer infoer
warner warner

View File

@@ -3,11 +3,10 @@ package server
import ( import (
"context" "context"
"errors" "fmt"
"net/http"
"time"
"github.com/qdm12/gluetun/internal/dns" "github.com/qdm12/gluetun/internal/dns"
"github.com/qdm12/gluetun/internal/httpserver"
"github.com/qdm12/gluetun/internal/models" "github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/portforward" "github.com/qdm12/gluetun/internal/portforward"
"github.com/qdm12/gluetun/internal/publicip" "github.com/qdm12/gluetun/internal/publicip"
@@ -15,44 +14,23 @@ import (
"github.com/qdm12/gluetun/internal/vpn" "github.com/qdm12/gluetun/internal/vpn"
) )
type Server interface {
Run(ctx context.Context, done chan<- struct{})
}
type server struct {
address string
logger infoErrorer
handler http.Handler
}
func New(ctx context.Context, address string, logEnabled bool, logger Logger, func New(ctx context.Context, address string, logEnabled bool, logger Logger,
buildInfo models.BuildInformation, openvpnLooper vpn.Looper, buildInfo models.BuildInformation, openvpnLooper vpn.Looper,
pfGetter portforward.Getter, unboundLooper dns.Looper, pfGetter portforward.Getter, unboundLooper dns.Looper,
updaterLooper updater.Looper, publicIPLooper publicip.Looper) Server { updaterLooper updater.Looper, publicIPLooper publicip.Looper) (server httpserver.Runner, err error) {
handler := newHandler(ctx, logger, logEnabled, buildInfo, handler := newHandler(ctx, logger, logEnabled, buildInfo,
openvpnLooper, pfGetter, unboundLooper, updaterLooper, publicIPLooper) openvpnLooper, pfGetter, unboundLooper, updaterLooper, publicIPLooper)
return &server{
address: address,
logger: logger,
handler: handler,
}
}
func (s *server) Run(ctx context.Context, done chan<- struct{}) { httpServerSettings := httpserver.Settings{
defer close(done) Address: address,
server := http.Server{Addr: s.address, Handler: s.handler} Handler: handler,
go func() { Logger: logger,
<-ctx.Done()
const shutdownGraceDuration = 2 * time.Second
shutdownCtx, cancel := context.WithTimeout(context.Background(), shutdownGraceDuration)
defer cancel()
if err := server.Shutdown(shutdownCtx); err != nil {
s.logger.Error("failed shutting down: " + err.Error())
} }
}()
s.logger.Info("listening on " + s.address) server, err = httpserver.New(httpServerSettings)
err := server.ListenAndServe() if err != nil {
if err != nil && errors.Is(ctx.Err(), context.Canceled) { return nil, fmt.Errorf("cannot create server: %w", err)
s.logger.Error(err.Error())
} }
return server, nil
} }