aboutsummaryrefslogtreecommitdiff
path: root/pkg/ext/middleware.go
diff options
context:
space:
mode:
authorGabriel Arakaki Giovanini <mail@gabrielgio.me>2023-10-24 19:12:52 +0200
committerGabriel Arakaki Giovanini <mail@gabrielgio.me>2023-10-24 19:12:52 +0200
commitc3ea735c0f03a0827a8e753a5b5adf6e31f4c925 (patch)
treeab2d15625f720858c570e66c00279ccc6cb28ec3 /pkg/ext/middleware.go
parenta9a270ba1e1e9add10d75de3e592efee9728c5b4 (diff)
downloadlens-c3ea735c0f03a0827a8e753a5b5adf6e31f4c925.tar.gz
lens-c3ea735c0f03a0827a8e753a5b5adf6e31f4c925.tar.bz2
lens-c3ea735c0f03a0827a8e753a5b5adf6e31f4c925.zip
feat: Migrate from logrus to slog
Diffstat (limited to 'pkg/ext/middleware.go')
-rw-r--r--pkg/ext/middleware.go43
1 files changed, 23 insertions, 20 deletions
diff --git a/pkg/ext/middleware.go b/pkg/ext/middleware.go
index 6a94c4f..38bacca 100644
--- a/pkg/ext/middleware.go
+++ b/pkg/ext/middleware.go
@@ -4,11 +4,10 @@ import (
"context"
"encoding/base64"
"errors"
+ "log/slog"
"net/http"
"time"
- "github.com/sirupsen/logrus"
-
"git.sr.ht/~gabrielgio/img/pkg/database/repository"
"git.sr.ht/~gabrielgio/img/pkg/service"
)
@@ -24,7 +23,7 @@ type (
User string
LogMiddleware struct {
- entry *logrus.Entry
+ logger *slog.Logger
}
)
@@ -32,9 +31,9 @@ const (
UserKey User = "user"
)
-func NewLogMiddleare(log *logrus.Entry) *LogMiddleware {
+func NewLogMiddleare(log *slog.Logger) *LogMiddleware {
return &LogMiddleware{
- entry: log,
+ logger: log,
}
}
@@ -43,27 +42,29 @@ func (l *LogMiddleware) HTTP(next http.HandlerFunc) http.HandlerFunc {
start := time.Now()
next(w, r)
elapsed := time.Since(start)
- l.entry.
- WithField("time", elapsed).
- WithField("path", r.URL.Path).
- Info(r.Method)
+ l.logger.Info(
+ r.Method,
+ slog.Duration("elapsed", elapsed),
+ slog.String("path", r.URL.Path),
+ )
+
}
}
type AuthMiddleware struct {
key []byte
- entry *logrus.Entry
+ logger *slog.Logger
userRepository repository.UserRepository
}
func NewAuthMiddleware(
key []byte,
- log *logrus.Entry,
+ logger *slog.Logger,
userRepository repository.UserRepository,
) *AuthMiddleware {
return &AuthMiddleware{
key: key,
- entry: log.WithField("context", "auth"),
+ logger: logger,
userRepository: userRepository,
}
}
@@ -79,35 +80,37 @@ func (a *AuthMiddleware) LoggedIn(next http.HandlerFunc) http.HandlerFunc {
redirectLogin := "/login?redirect=" + path
authBase64, err := r.Cookie("auth")
if errors.Is(err, http.ErrNoCookie) {
- a.entry.Info("No auth provided")
+ a.logger.Info("No auth provided")
http.Redirect(w, r, redirectLogin, http.StatusTemporaryRedirect)
return
}
auth, err := base64.StdEncoding.DecodeString(authBase64.Value)
if err != nil {
- a.entry.Error(err)
+ a.logger.Error(err.Error())
return
}
token, err := service.ReadToken(auth, a.key)
if err != nil {
- a.entry.Error(err)
+ a.logger.Error(err.Error())
http.Redirect(w, r, redirectLogin, http.StatusTemporaryRedirect)
return
}
user, err := a.userRepository.Get(r.Context(), token.UserID)
if err != nil {
- a.entry.Error(err)
+ a.logger.Error(err.Error())
return
}
r = r.WithContext(context.WithValue(r.Context(), UserKey, user))
- a.entry.
- WithField("userID", token.UserID).
- WithField("username", token.Username).
- Info("user recognized")
+ a.logger.
+ Info(
+ "user recognized",
+ slog.Uint64("userid", uint64(token.UserID)),
+ slog.String("username", token.Username),
+ )
next(w, r)
}
}