Skip to content

Commit

Permalink
chore(server): move log middleware to internal/server/middlewares/log
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Aug 23, 2024
1 parent fbe8dce commit 4e72236
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
20 changes: 12 additions & 8 deletions internal/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/server/middlewares/log"
)

func newHandler(ctx context.Context, logger infoWarner, logging bool,
Expand All @@ -17,7 +18,7 @@ func newHandler(ctx context.Context, logger infoWarner, logging bool,
publicIPLooper PublicIPLoop,
storage Storage,
ipv6Supported bool,
) http.Handler {
) (httpHandler http.Handler) {
handler := &handler{}

vpn := newVPNHandler(ctx, vpnLooper, storage, ipv6Supported, logger)
Expand All @@ -29,16 +30,19 @@ func newHandler(ctx context.Context, logger infoWarner, logging bool,
handler.v0 = newHandlerV0(ctx, logger, vpnLooper, dnsLooper, updaterLooper)
handler.v1 = newHandlerV1(logger, buildInfo, vpn, openvpn, dns, updater, publicip)

handlerWithLog := withLogMiddleware(handler, logger, logging)
handler.setLogEnabled = handlerWithLog.setEnabled

return handlerWithLog
middlewares := []func(http.Handler) http.Handler{
log.New(logger, logging),
}
httpHandler = handler
for _, middleware := range middlewares {
httpHandler = middleware(httpHandler)
}
return httpHandler
}

type handler struct {
v0 http.Handler
v1 http.Handler
setLogEnabled func(enabled bool)
v0 http.Handler
v1 http.Handler
}

func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand Down
5 changes: 5 additions & 0 deletions internal/server/middlewares/log/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package log

type Logger interface {
Info(message string)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server
package log

import (
"net/http"
Expand All @@ -7,18 +7,21 @@ import (
"time"
)

func withLogMiddleware(childHandler http.Handler, logger infoer, enabled bool) *logMiddleware {
return &logMiddleware{
childHandler: childHandler,
logger: logger,
timeNow: time.Now,
enabled: enabled,
func New(logger Logger, enabled bool) (
middleware func(http.Handler) http.Handler) {
return func(handler http.Handler) http.Handler {
return &logMiddleware{
childHandler: handler,
logger: logger,
timeNow: time.Now,
enabled: enabled,
}
}
}

type logMiddleware struct {
childHandler http.Handler
logger infoer
logger Logger
timeNow func() time.Time
enabled bool
enabledMu sync.RWMutex
Expand All @@ -39,7 +42,7 @@ func (m *logMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.RemoteAddr + " in " + duration.String())
}

func (m *logMiddleware) setEnabled(enabled bool) {
func (m *logMiddleware) SetEnabled(enabled bool) {
m.enabledMu.Lock()
defer m.enabledMu.Unlock()
m.enabled = enabled
Expand Down

0 comments on commit 4e72236

Please sign in to comment.