feat(tabby): add auth token support in swagger ui (#988)

r0.7
Meng Zhang 2023-12-08 19:55:54 +08:00 committed by GitHub
parent 711a389197
commit f4224f0417
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 4 deletions

View File

@ -20,6 +20,9 @@ use crate::services::chat::{ChatCompletionRequest, ChatService};
responses( responses(
(status = 200, description = "Success", body = ChatCompletionChunk, content_type = "application/jsonstream"), (status = 200, description = "Success", body = ChatCompletionChunk, content_type = "application/jsonstream"),
(status = 405, description = "When chat model is not specified, the endpoint will returns 405 Method Not Allowed"), (status = 405, description = "When chat model is not specified, the endpoint will returns 405 Method Not Allowed"),
),
security(
("token" = [])
) )
)] )]
#[instrument(skip(state, request))] #[instrument(skip(state, request))]

View File

@ -15,6 +15,9 @@ use crate::services::completion::{CompletionRequest, CompletionResponse, Complet
responses( responses(
(status = 200, description = "Success", body = CompletionResponse, content_type = "application/json"), (status = 200, description = "Success", body = CompletionResponse, content_type = "application/json"),
(status = 400, description = "Bad Request") (status = 400, description = "Bad Request")
),
security(
("token" = [])
) )
)] )]
#[instrument(skip(state, request))] #[instrument(skip(state, request))]

View File

@ -16,6 +16,9 @@ use tabby_common::api::event::{Event, EventLogger, LogEventRequest, SelectKind};
responses( responses(
(status = 200, description = "Success"), (status = 200, description = "Success"),
(status = 400, description = "Bad Request") (status = 400, description = "Bad Request")
),
security(
("token" = [])
) )
)] )]
pub async fn log_event( pub async fn log_event(

View File

@ -10,6 +10,9 @@ use crate::services::health;
tag = "v1", tag = "v1",
responses( responses(
(status = 200, description = "Success", body = HealthState, content_type = "application/json"), (status = 200, description = "Success", body = HealthState, content_type = "application/json"),
),
security(
("token" = [])
) )
)] )]
pub async fn health(State(state): State<Arc<health::HealthState>>) -> Json<health::HealthState> { pub async fn health(State(state): State<Arc<health::HealthState>>) -> Json<health::HealthState> {

View File

@ -32,8 +32,11 @@ pub struct SearchQuery {
responses( responses(
(status = 200, description = "Success" , body = SearchResponse, content_type = "application/json"), (status = 200, description = "Success" , body = SearchResponse, content_type = "application/json"),
(status = 501, description = "When code search is not enabled, the endpoint will returns 501 Not Implemented"), (status = 501, description = "When code search is not enabled, the endpoint will returns 501 Not Implemented"),
) ),
)] security(
("token" = [])
)
)]
#[instrument(skip(state, query))] #[instrument(skip(state, query))]
pub async fn search( pub async fn search(
State(state): State<Arc<dyn CodeSearch>>, State(state): State<Arc<dyn CodeSearch>>,

View File

@ -12,7 +12,10 @@ use tabby_common::{
use tokio::time::sleep; use tokio::time::sleep;
use tower_http::timeout::TimeoutLayer; use tower_http::timeout::TimeoutLayer;
use tracing::info; use tracing::info;
use utoipa::OpenApi; use utoipa::{
openapi::security::{HttpAuthScheme, HttpBuilder, SecurityScheme},
Modify, OpenApi,
};
use utoipa_swagger_ui::SwaggerUi; use utoipa_swagger_ui::SwaggerUi;
use crate::{ use crate::{
@ -63,7 +66,8 @@ Install following IDE / Editor extensions to get started with [Tabby](https://gi
api::code::SearchResponse, api::code::SearchResponse,
api::code::Hit, api::code::Hit,
api::code::HitDocument api::code::HitDocument
)) )),
modifiers(&SecurityAddon),
)] )]
struct ApiDoc; struct ApiDoc;
@ -245,3 +249,21 @@ fn start_heartbeat(args: &ServeArgs) {
} }
}); });
} }
struct SecurityAddon;
impl Modify for SecurityAddon {
fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) {
if let Some(components) = &mut openapi.components {
components.add_security_scheme(
"token",
SecurityScheme::Http(
HttpBuilder::new()
.scheme(HttpAuthScheme::Bearer)
.bearer_format("token")
.build(),
),
)
}
}
}