Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tabby): fix swagger's local server use local port #458

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading