2023-09-20 15:13:10 -03:00
|
|
|
package handler
|
2023-09-20 01:26:38 -03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2023-09-20 15:13:10 -03:00
|
|
|
"git.maronato.dev/maronato/finger/webfingers"
|
2023-09-20 01:26:38 -03:00
|
|
|
)
|
|
|
|
|
2023-09-20 15:13:10 -03:00
|
|
|
func WebfingerHandler(fingers webfingers.WebFingers) http.Handler {
|
2023-09-20 01:26:38 -03:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Only handle GET requests
|
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the query params
|
|
|
|
q := r.URL.Query()
|
|
|
|
|
|
|
|
// Get the resource
|
|
|
|
resource := q.Get("resource")
|
|
|
|
if resource == "" {
|
|
|
|
http.Error(w, "No resource provided", http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get and validate resource
|
2023-09-20 15:13:10 -03:00
|
|
|
finger, ok := fingers[resource]
|
2023-09-20 01:26:38 -03:00
|
|
|
if !ok {
|
|
|
|
http.Error(w, "Resource not found", http.StatusNotFound)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the content type
|
|
|
|
w.Header().Set("Content-Type", "application/jrd+json")
|
|
|
|
|
|
|
|
// Write the response
|
|
|
|
if err := json.NewEncoder(w).Encode(finger); err != nil {
|
|
|
|
http.Error(w, "Error encoding json", http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|