Maint: package local narrow Logger interfaces

This commit is contained in:
Quentin McGaw (desktop)
2021-09-23 16:58:21 +00:00
parent d8e008606f
commit cf95692b93
57 changed files with 414 additions and 154 deletions

View File

@@ -7,22 +7,21 @@ import (
"strings"
"github.com/qdm12/gluetun/internal/dns"
"github.com/qdm12/golibs/logging"
)
func newDNSHandler(ctx context.Context, looper dns.Looper,
logger logging.Logger) http.Handler {
warner warner) http.Handler {
return &dnsHandler{
ctx: ctx,
looper: looper,
logger: logger,
warner: warner,
}
}
type dnsHandler struct {
ctx context.Context
looper dns.Looper
logger logging.Logger
warner warner
}
func (h *dnsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@@ -47,7 +46,7 @@ func (h *dnsHandler) getStatus(w http.ResponseWriter) {
encoder := json.NewEncoder(w)
data := statusWrapper{Status: string(status)}
if err := encoder.Encode(data); err != nil {
h.logger.Warn(err.Error())
h.warner.Warn(err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
@@ -72,7 +71,7 @@ func (h *dnsHandler) setStatus(w http.ResponseWriter, r *http.Request) {
}
encoder := json.NewEncoder(w)
if err := encoder.Encode(outcomeWrapper{Outcome: outcome}); err != nil {
h.logger.Warn(err.Error())
h.warner.Warn(err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

View File

@@ -11,10 +11,9 @@ import (
"github.com/qdm12/gluetun/internal/publicip"
"github.com/qdm12/gluetun/internal/updater"
"github.com/qdm12/gluetun/internal/vpn"
"github.com/qdm12/golibs/logging"
)
func newHandler(ctx context.Context, logger logging.Logger, logging bool,
func newHandler(ctx context.Context, logger infoWarner, logging bool,
buildInfo models.BuildInformation,
vpnLooper vpn.Looper,
pfGetter portforward.Getter,

View File

@@ -8,10 +8,9 @@ import (
"github.com/qdm12/gluetun/internal/dns"
"github.com/qdm12/gluetun/internal/updater"
"github.com/qdm12/gluetun/internal/vpn"
"github.com/qdm12/golibs/logging"
)
func newHandlerV0(ctx context.Context, logger logging.Logger,
func newHandlerV0(ctx context.Context, logger infoWarner,
vpn vpn.Looper, dns dns.Looper, updater updater.Looper) http.Handler {
return &handlerV0{
ctx: ctx,
@@ -24,7 +23,7 @@ func newHandlerV0(ctx context.Context, logger logging.Logger,
type handlerV0 struct {
ctx context.Context
logger logging.Logger
logger infoWarner
vpn vpn.Looper
dns dns.Looper
updater updater.Looper

View File

@@ -7,13 +7,12 @@ import (
"strings"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/golibs/logging"
)
func newHandlerV1(logger logging.Logger, buildInfo models.BuildInformation,
func newHandlerV1(w warner, buildInfo models.BuildInformation,
openvpn, dns, updater, publicip http.Handler) http.Handler {
return &handlerV1{
logger: logger,
warner: w,
buildInfo: buildInfo,
openvpn: openvpn,
dns: dns,
@@ -23,7 +22,7 @@ func newHandlerV1(logger logging.Logger, buildInfo models.BuildInformation,
}
type handlerV1 struct {
logger logging.Logger
warner warner
buildInfo models.BuildInformation
openvpn http.Handler
dns http.Handler
@@ -52,7 +51,7 @@ func (h *handlerV1) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (h *handlerV1) getVersion(w http.ResponseWriter) {
encoder := json.NewEncoder(w)
if err := encoder.Encode(h.buildInfo); err != nil {
h.logger.Warn(err.Error())
h.warner.Warn(err.Error())
w.WriteHeader(http.StatusInternalServerError)
}
}

View File

@@ -5,11 +5,9 @@ import (
"strconv"
"sync"
"time"
"github.com/qdm12/golibs/logging"
)
func withLogMiddleware(childHandler http.Handler, logger logging.Logger, enabled bool) *logMiddleware {
func withLogMiddleware(childHandler http.Handler, logger infoer, enabled bool) *logMiddleware {
return &logMiddleware{
childHandler: childHandler,
logger: logger,
@@ -20,7 +18,7 @@ func withLogMiddleware(childHandler http.Handler, logger logging.Logger, enabled
type logMiddleware struct {
childHandler http.Handler
logger logging.Logger
logger infoer
timeNow func() time.Time
enabled bool
enabledMu sync.RWMutex

29
internal/server/logger.go Normal file
View File

@@ -0,0 +1,29 @@
package server
type Logger interface {
infoer
warner
errorer
}
type infoErrorer interface {
infoer
errorer
}
type infoWarner interface {
infoer
warner
}
type infoer interface {
Info(s string)
}
type warner interface {
Warn(s string)
}
type errorer interface {
Error(s string)
}

View File

@@ -8,16 +8,15 @@ import (
"github.com/qdm12/gluetun/internal/portforward"
"github.com/qdm12/gluetun/internal/vpn"
"github.com/qdm12/golibs/logging"
)
func newOpenvpnHandler(ctx context.Context, looper vpn.Looper,
pfGetter portforward.Getter, logger logging.Logger) http.Handler {
pfGetter portforward.Getter, w warner) http.Handler {
return &openvpnHandler{
ctx: ctx,
looper: looper,
pf: pfGetter,
logger: logger,
warner: w,
}
}
@@ -25,7 +24,7 @@ type openvpnHandler struct {
ctx context.Context
looper vpn.Looper
pf portforward.Getter
logger logging.Logger
warner warner
}
func (h *openvpnHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@@ -64,7 +63,7 @@ func (h *openvpnHandler) getStatus(w http.ResponseWriter) {
encoder := json.NewEncoder(w)
data := statusWrapper{Status: string(status)}
if err := encoder.Encode(data); err != nil {
h.logger.Warn(err.Error())
h.warner.Warn(err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
@@ -89,7 +88,7 @@ func (h *openvpnHandler) setStatus(w http.ResponseWriter, r *http.Request) {
}
encoder := json.NewEncoder(w)
if err := encoder.Encode(outcomeWrapper{Outcome: outcome}); err != nil {
h.logger.Warn(err.Error())
h.warner.Warn(err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
@@ -102,7 +101,7 @@ func (h *openvpnHandler) getSettings(w http.ResponseWriter) {
settings.Password = "redacted"
encoder := json.NewEncoder(w)
if err := encoder.Encode(settings); err != nil {
h.logger.Warn(err.Error())
h.warner.Warn(err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
@@ -113,7 +112,7 @@ func (h *openvpnHandler) getPortForwarded(w http.ResponseWriter) {
encoder := json.NewEncoder(w)
data := portWrapper{Port: port}
if err := encoder.Encode(data); err != nil {
h.logger.Warn(err.Error())
h.warner.Warn(err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}

View File

@@ -6,21 +6,18 @@ import (
"strings"
"github.com/qdm12/gluetun/internal/publicip"
"github.com/qdm12/golibs/logging"
)
func newPublicIPHandler(
looper publicip.Looper,
logger logging.Logger) http.Handler {
func newPublicIPHandler(looper publicip.Looper, w warner) http.Handler {
return &publicIPHandler{
looper: looper,
logger: logger,
warner: w,
}
}
type publicIPHandler struct {
looper publicip.Looper
logger logging.Logger
warner warner
}
func (h *publicIPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@@ -42,7 +39,7 @@ func (h *publicIPHandler) getPublicIP(w http.ResponseWriter) {
data := h.looper.GetData()
encoder := json.NewEncoder(w)
if err := encoder.Encode(data); err != nil {
h.logger.Warn(err.Error())
h.warner.Warn(err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}

View File

@@ -13,7 +13,6 @@ import (
"github.com/qdm12/gluetun/internal/publicip"
"github.com/qdm12/gluetun/internal/updater"
"github.com/qdm12/gluetun/internal/vpn"
"github.com/qdm12/golibs/logging"
)
type Server interface {
@@ -22,11 +21,11 @@ type Server interface {
type server struct {
address string
logger logging.Logger
logger infoErrorer
handler http.Handler
}
func New(ctx context.Context, address string, logEnabled bool, logger logging.Logger,
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 {

View File

@@ -7,24 +7,23 @@ import (
"strings"
"github.com/qdm12/gluetun/internal/updater"
"github.com/qdm12/golibs/logging"
)
func newUpdaterHandler(
ctx context.Context,
looper updater.Looper,
logger logging.Logger) http.Handler {
warner warner) http.Handler {
return &updaterHandler{
ctx: ctx,
looper: looper,
logger: logger,
warner: warner,
}
}
type updaterHandler struct {
ctx context.Context
looper updater.Looper
logger logging.Logger
warner warner
}
func (h *updaterHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@@ -49,7 +48,7 @@ func (h *updaterHandler) getStatus(w http.ResponseWriter) {
encoder := json.NewEncoder(w)
data := statusWrapper{Status: string(status)}
if err := encoder.Encode(data); err != nil {
h.logger.Warn(err.Error())
h.warner.Warn(err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
@@ -74,7 +73,7 @@ func (h *updaterHandler) setStatus(w http.ResponseWriter, r *http.Request) {
}
encoder := json.NewEncoder(w)
if err := encoder.Encode(outcomeWrapper{Outcome: outcome}); err != nil {
h.logger.Warn(err.Error())
h.warner.Warn(err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}