From 2f63bdd24283afa72fd57d8fa7f9067a2e3dcef1 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Thu, 7 Sep 2023 13:57:46 +0200 Subject: [PATCH] fix: Make sure to serve the TD from its inherent base It unbreaks consuming a TD served from `.well-known/wot` when the Thing::base is not set. (See https://www.rfc-editor.org/rfc/rfc3986#section-5.1.3) --- src/servient/builder.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/servient/builder.rs b/src/servient/builder.rs index 693b402..ad63e03 100644 --- a/src/servient/builder.rs +++ b/src/servient/builder.rs @@ -5,7 +5,7 @@ use crate::{ hlist::*, servient::Servient, }; -use axum::{handler::Handler, routing::MethodRouter, Router}; +use axum::{handler::Handler, response::Redirect, routing::MethodRouter, Router}; use tower_http::cors::*; use datta::{Operator, UriTemplate}; @@ -189,9 +189,14 @@ where // TODO: Figure out how to share the thing description and if we want to. let json = serde_json::to_value(&thing)?; + // We serve The thing from the root + router = router.route("/", axum::routing::get(move || async { axum::Json(json) })); + + // We redirect this path to / to support relative Forms with empty base + // See: https://www.rfc-editor.org/rfc/rfc3986#section-5.1.3 router = router.route( "/.well-known/wot", - axum::routing::get(move || async { axum::Json(json) }), + axum::routing::get(move || async { Redirect::to("/") }), ); if thing.other.field_ref().permissive_cors {