This commit is contained in:
Melody 2024-08-28 17:20:38 +02:00
parent 94197780e1
commit 2977f09245
32 changed files with 763 additions and 936 deletions

View file

@ -1,36 +1,38 @@
package server
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/sirupsen/logrus"
"gitlab.com/mstarongitlab/linstrom/server/middlewares"
"github.com/rs/zerolog"
"gitlab.com/mstarongitlab/linstrom/storage"
"gorm.io/gorm"
)
// Mount under /.well-known/webfinger
func webfingerHandler(w http.ResponseWriter, r *http.Request) {
logEntry, ok := r.Context().Value(middlewares.CONTEXT_KEY_LOGRUS).(*logrus.Entry)
if !ok {
http.Error(w, "couldn't get logging entry from context", http.StatusInternalServerError)
return
}
logger := zerolog.Ctx(r.Context())
store := storage.Storage{}
requestedResource := r.FormValue("resource")
if requestedResource == "" {
http.Error(w, "bad request. Include \"resource\" parameter", http.StatusBadRequest)
logEntry.Infoln("No resource parameter. Cancelling")
logger.Debug().Msg("Resource parameter missing. Cancelling")
return
}
accName := strings.TrimPrefix(requestedResource, "acc:")
acc, err := store.FindLocalAccount(accName)
acc, err := store.FindAccountByFullHandle(accName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
logEntry.WithError(err).Warningln("couldn't find account")
return
if errors.Is(err, gorm.ErrRecordNotFound) {
http.Error(w, "account not found", http.StatusNotFound)
logger.Debug().Str("account-name", accName).Msg("Account not found")
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
logger.Error().Err(err).Msg("Error while searching for account")
return
}
}
fmt.Fprint(w, acc)
}

View file

@ -1,10 +1,9 @@
package middlewares
import (
"context"
"net/http"
"github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
)
const CONTEXT_KEY_LOGRUS = ContextKey("logrus")
@ -12,9 +11,7 @@ const CONTEXT_KEY_LOGRUS = ContextKey("logrus")
// Inject a logrus entry into the context that has the url path already set
func InjectLogrusMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reqContext := r.Context()
entry := logrus.WithField("url-path", r.URL.Path)
newContext := context.WithValue(reqContext, CONTEXT_KEY_LOGRUS, entry)
newContext := log.With().Str("url-path", r.URL.RawPath).Logger().WithContext(r.Context())
next.ServeHTTP(w, r.WithContext(newContext))
})
}