chore(server): use httpserver package for control server
This commit is contained in:
@@ -3,11 +3,10 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
"fmt"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/dns"
|
||||
"github.com/qdm12/gluetun/internal/httpserver"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/portforward"
|
||||
"github.com/qdm12/gluetun/internal/publicip"
|
||||
@@ -15,44 +14,23 @@ import (
|
||||
"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,
|
||||
buildInfo models.BuildInformation, openvpnLooper vpn.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,
|
||||
openvpnLooper, pfGetter, unboundLooper, updaterLooper, publicIPLooper)
|
||||
return &server{
|
||||
address: address,
|
||||
logger: logger,
|
||||
handler: handler,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *server) Run(ctx context.Context, done chan<- struct{}) {
|
||||
defer close(done)
|
||||
server := http.Server{Addr: s.address, Handler: s.handler}
|
||||
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())
|
||||
httpServerSettings := httpserver.Settings{
|
||||
Address: address,
|
||||
Handler: handler,
|
||||
Logger: logger,
|
||||
}
|
||||
|
||||
server, err = httpserver.New(httpServerSettings)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot create server: %w", err)
|
||||
}
|
||||
|
||||
return server, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user