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
release-0.2
胡锋 2023-09-19 12:36:08 +08:00 committed by GitHub
parent ede704ca05
commit de3a2271d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 3 deletions

View File

@ -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
}