poem-web / poem

A full-featured and easy-to-use web framework with the Rust programming language.
Apache License 2.0
3.57k stars 287 forks source link

Is it possible to use custom styles or html template for the swagger ui? #708

Open donkey-donkey opened 10 months ago

donkey-donkey commented 10 months ago
          I will close this as completed as the original problem is now fixed, but my second question still stands:

Is is possible to provide a custom HTML template to set things like the page title and icon for those UIs (Specifically the swagger one in my case)?

Originally posted by @SaculRennorb in https://github.com/poem-web/poem/issues/660#issuecomment-1734736904

SaculRennorb commented 10 months ago

Hey, I just went ahead and implemented my own version using the original source code, which was easy enough.

Here are some of the rough ideas:

fn create_endpoint(spec : &str) -> impl Endpoint {
    let ui_html = String::from(swagger_ui::HTML_INDEX).replace("{spec}", spec);

    poem::Route::new()
        .at("/"           , make_sync(move |_| Html(ui_html.clone())))
        .at("/oauth2.html", make_sync(move |_| Html(swagger_ui::HTML_OAUTH2)))
}

...
let api_service = OpenApiService::new(...);
let api_ep = api_service
        .with(poem::middleware::Cors::new())
        ...
    ;
let ui_ep = create_endpoint(&api_service.spec());
let route = Route::new()
        .nest("/", api_ep)
        .nest("/ui", ui_ep)
    ;

the HTML_... consts are just the desired custom html build into the binary with str_include!(...).

donkey-donkey commented 10 months ago

cool. good one. im looking into that. thanks