chore(server): use httpserver package for control server
This commit is contained in:
@@ -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: "})
|
||||||
|
|||||||
@@ -6,11 +6,6 @@ type Logger interface {
|
|||||||
errorer
|
errorer
|
||||||
}
|
}
|
||||||
|
|
||||||
type infoErrorer interface {
|
|
||||||
infoer
|
|
||||||
errorer
|
|
||||||
}
|
|
||||||
|
|
||||||
type infoWarner interface {
|
type infoWarner interface {
|
||||||
infoer
|
infoer
|
||||||
warner
|
warner
|
||||||
|
|||||||
@@ -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,
|
httpServerSettings := httpserver.Settings{
|
||||||
logger: logger,
|
Address: address,
|
||||||
handler: handler,
|
Handler: handler,
|
||||||
}
|
Logger: logger,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) Run(ctx context.Context, done chan<- struct{}) {
|
server, err = httpserver.New(httpServerSettings)
|
||||||
defer close(done)
|
if err != nil {
|
||||||
server := http.Server{Addr: s.address, Handler: s.handler}
|
return nil, fmt.Errorf("cannot create server: %w", err)
|
||||||
go func() {
|
|
||||||
<-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)
|
|
||||||
err := server.ListenAndServe()
|
|
||||||
if err != nil && errors.Is(ctx.Err(), context.Canceled) {
|
|
||||||
s.logger.Error(err.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return server, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user