Skip to content

Commit

Permalink
Added redirects in the warp servers from /swagger-ui to `/swagger-u…
Browse files Browse the repository at this point in the history
…i/` (#171)

The ui only works if you go to `/swagger-ui/` with a tailing `/`.
If you browse to `/swagger-ui`, you get an unhelpful blank page,
due to the relative links for JS, CSS, et al files.
My investigation indicates that the best way to fix this in `warp`
is to manually create a redirect.
So I updated the `warp` examples to have this
(I am unsure if the other servers have the same problem)

I considered using a regex on the full path
instead of passing in both the path and tail,
but I decided passing along two strings was more efficient than
running a full regex every time

Co-authored-by: Ken Swanson <ken.swanson@redpoll.ai>
  • Loading branch information
Swandog and Swandog authored Jun 14, 2022
1 parent 13c9149 commit 39c1a50
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 8 additions & 1 deletion examples/todo-warp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use utoipa::{
};
use utoipa_swagger_ui::Config;
use warp::{
http::Uri,
hyper::{Response, StatusCode},
path::Tail,
path::{FullPath, Tail},
Filter, Rejection, Reply,
};

Expand Down Expand Up @@ -46,6 +47,7 @@ async fn main() {

let swagger_ui = warp::path("swagger-ui")
.and(warp::get())
.and(warp::path::full())
.and(warp::path::tail())
.and(warp::any().map(move || config.clone()))
.and_then(serve_swagger);
Expand All @@ -56,9 +58,14 @@ async fn main() {
}

async fn serve_swagger(
full_path: FullPath,
tail: Tail,
config: Arc<Config<'static>>,
) -> Result<Box<dyn Reply + 'static>, Rejection> {
if full_path.as_str() == "/swagger-ui" {
return Ok(Box::new(warp::redirect::found(Uri::from_static("/swagger-ui/"))));
}

let path = tail.as_str();
match utoipa_swagger_ui::serve(path, config) {
Ok(file) => {
Expand Down
9 changes: 8 additions & 1 deletion examples/warp-multiple-api-docs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use std::{net::Ipv4Addr, sync::Arc};
use utoipa::OpenApi;
use utoipa_swagger_ui::Config;
use warp::{
http::Uri,
hyper::{Response, StatusCode},
path::Tail,
path::{FullPath, Tail},
Filter, Rejection, Reply,
};

Expand Down Expand Up @@ -32,6 +33,7 @@ async fn main() {

let swagger_ui = warp::path("swagger-ui")
.and(warp::get())
.and(warp::path::full())
.and(warp::path::tail())
.and(warp::any().map(move || config.clone()))
.and_then(serve_swagger);
Expand All @@ -52,9 +54,14 @@ async fn main() {
}

async fn serve_swagger(
full_path: FullPath,
tail: Tail,
config: Arc<Config<'static>>,
) -> Result<Box<dyn Reply + 'static>, Rejection> {
if full_path.as_str() == "/swagger-ui" {
return Ok(Box::new(warp::redirect::found(Uri::from_static("/swagger-ui/"))));
}

let path = tail.as_str();
match utoipa_swagger_ui::serve(path, config) {
Ok(file) => {
Expand Down

0 comments on commit 39c1a50

Please sign in to comment.