add cors middleware

This commit is contained in:
Evan Jarrett
2025-10-14 20:56:15 -05:00
parent 764642d271
commit d726e464a6
+26 -9
View File
@@ -32,26 +32,43 @@ func NewXRPCHandler(pds *HoldPDS, publicURL string, blobStore BlobStore) *XRPCHa
}
}
// corsMiddleware wraps a handler with CORS headers
func corsMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
// Handle preflight OPTIONS requests
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}
next(w, r)
}
}
// RegisterHandlers registers all XRPC endpoints
func (h *XRPCHandler) RegisterHandlers(mux *http.ServeMux) {
// Health check endpoint
mux.HandleFunc("/xrpc/_health", h.HandleHealth)
mux.HandleFunc("/xrpc/_health", corsMiddleware(h.HandleHealth))
// Standard PDS endpoints
mux.HandleFunc("/xrpc/com.atproto.server.describeServer", h.HandleDescribeServer)
mux.HandleFunc("/xrpc/com.atproto.repo.describeRepo", h.HandleDescribeRepo)
mux.HandleFunc("/xrpc/com.atproto.repo.getRecord", h.HandleGetRecord)
mux.HandleFunc("/xrpc/com.atproto.repo.listRecords", h.HandleListRecords)
mux.HandleFunc("/xrpc/com.atproto.server.describeServer", corsMiddleware(h.HandleDescribeServer))
mux.HandleFunc("/xrpc/com.atproto.repo.describeRepo", corsMiddleware(h.HandleDescribeRepo))
mux.HandleFunc("/xrpc/com.atproto.repo.getRecord", corsMiddleware(h.HandleGetRecord))
mux.HandleFunc("/xrpc/com.atproto.repo.listRecords", corsMiddleware(h.HandleListRecords))
// Sync endpoints
mux.HandleFunc("/xrpc/com.atproto.sync.listRepos", h.HandleListRepos)
mux.HandleFunc("/xrpc/com.atproto.sync.listRepos", corsMiddleware(h.HandleListRepos))
// Blob endpoints (wrap existing presigned URL logic)
mux.HandleFunc("/xrpc/com.atproto.repo.uploadBlob", h.HandleUploadBlob)
mux.HandleFunc("/xrpc/com.atproto.sync.getBlob", h.HandleGetBlob)
mux.HandleFunc("/xrpc/com.atproto.repo.uploadBlob", corsMiddleware(h.HandleUploadBlob))
mux.HandleFunc("/xrpc/com.atproto.sync.getBlob", corsMiddleware(h.HandleGetBlob))
// DID document
mux.HandleFunc("/.well-known/did.json", h.HandleDIDDocument)
mux.HandleFunc("/.well-known/did.json", corsMiddleware(h.HandleDIDDocument))
}
// HandleHealth returns health check information