Skip to content

Commit

Permalink
fix(tabby): fix swagger's local server use local port (TabbyML#458)
Browse files Browse the repository at this point in the history
* fixed: swagger's local server use local port

* fix: extract fn add_localhost_server

* fix: add_localhost_server return doc
  • Loading branch information
hufeng authored Sep 19, 2023
1 parent ede704c commit de3a227
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions crates/tabby/src/serve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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
}

0 comments on commit de3a227

Please sign in to comment.