From de3a2271d613a0e11748c89ff115e7d9c3ca8762 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E9=94=8B?= Date: Tue, 19 Sep 2023 12:36:08 +0800 Subject: [PATCH] fix(tabby): fix swagger's local server use local port (#458) * fixed: swagger's local server use local port * fix: extract fn add_localhost_server * fix: add_localhost_server return doc --- crates/tabby/src/serve/mod.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/crates/tabby/src/serve/mod.rs b/crates/tabby/src/serve/mod.rs index 3dc47d90a735..3ee40d505c17 100644 --- a/crates/tabby/src/serve/mod.rs +++ b/crates/tabby/src/serve/mod.rs @@ -15,7 +15,7 @@ use tabby_common::{config::Config, usage}; use tokio::time::sleep; use tower_http::cors::CorsLayer; use tracing::{info, warn}; -use utoipa::OpenApi; +use utoipa::{openapi::ServerBuilder, OpenApi}; use utoipa_swagger_ui::SwaggerUi; use self::health::HealthState; @@ -37,7 +37,6 @@ Install following IDE / Editor extensions to get started with [Tabby](https://gi ), servers( (url = "https://playground.app.tabbyml.com", description = "Playground server"), - (url = "http://localhost:8080", description = "Local server"), ), paths(events::log_event, completions::completion, health::health), components(schemas( @@ -161,8 +160,10 @@ pub async fn main(config: &Config, args: &ServeArgs) { } info!("Starting server, this might takes a few minutes..."); + + let doc = add_localhost_server(ApiDoc::openapi(), args.port); let app = Router::new() - .merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi())) + .merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", doc)) .nest("/v1", api_router(args, config)) .fallback(fallback()); @@ -223,3 +224,17 @@ fn start_heartbeat(args: &ServeArgs) { } }); } + +fn add_localhost_server(doc: utoipa::openapi::OpenApi, port: u16) -> utoipa::openapi::OpenApi { + let mut doc = doc; + if let Some(servers) = doc.servers.as_mut() { + servers.push( + ServerBuilder::new() + .url(format!("http://localhost:{}", port)) + .description(Some("Local server")) + .build(), + ); + } + + doc +}