linstrom/server/endpoints_ap.go

39 lines
1 KiB
Go
Raw Normal View History

2024-05-31 09:54:39 +00:00
package server
import (
2024-08-28 15:20:38 +00:00
"errors"
2024-05-31 09:54:39 +00:00
"fmt"
"net/http"
"strings"
2024-08-28 15:20:38 +00:00
"github.com/rs/zerolog"
2024-05-31 09:54:39 +00:00
"gitlab.com/mstarongitlab/linstrom/storage"
2024-08-28 15:20:38 +00:00
"gorm.io/gorm"
2024-05-31 09:54:39 +00:00
)
// Mount under /.well-known/webfinger
func webfingerHandler(w http.ResponseWriter, r *http.Request) {
2024-08-28 15:20:38 +00:00
logger := zerolog.Ctx(r.Context())
2024-05-31 09:54:39 +00:00
store := storage.Storage{}
requestedResource := r.FormValue("resource")
if requestedResource == "" {
http.Error(w, "bad request. Include \"resource\" parameter", http.StatusBadRequest)
2024-08-28 15:20:38 +00:00
logger.Debug().Msg("Resource parameter missing. Cancelling")
2024-05-31 09:54:39 +00:00
return
}
accName := strings.TrimPrefix(requestedResource, "acc:")
2024-08-28 15:20:38 +00:00
acc, err := store.FindAccountByFullHandle(accName)
2024-05-31 09:54:39 +00:00
if err != nil {
2024-08-28 15:20:38 +00:00
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
}
2024-05-31 09:54:39 +00:00
}
fmt.Fprint(w, acc)
}