ba6fe0f
pub mod auth;
pub mod cleanup;
pub mod openapi;
use std::sync::Arc;
use actix_cors::Cors;
use actix_web::http::Method;
use actix_web::{Scope, web};
use export::AppState;
use export::http::{Guard, Router};
pub const MAX_REQUEST_BODY_BYTES: usize = 2 * 1024 * 1024;
pub fn build_cors(web: &config::web::WebConfig) -> Cors {
let mut cors = Cors::default();
for origin in &web.cors_allow_origins {
cors = cors.allowed_origin(origin);
}
let mut cors = cors
.allowed_methods([
Method::GET,
Method::POST,
Method::PUT,
Method::PATCH,
Method::DELETE,
Method::OPTIONS,
])
.allowed_headers(["content-type", "authorization", "x-request-id"]);
if web.cors_allow_credentials {
cors = cors.supports_credentials();
}
cors
}
pub fn scope(state: AppState) -> Scope {
let auth = crate::auth::services::AuthService::new(state.clone());
let chat = chat::services::service::ChatService::new(state.clone());
let issues = issues::services::service::IssueService::new(state.clone());
let space = space::services::service::SpaceService::new(state.clone());
let user = user::services::service::UserService::new(state.clone());
let git = git::service::GitService::new(state.clone());
let pulls = pulls::services::service::PullService::new(state.clone());
let internal_service_key = state.config.internal.service_key.clone();
let cleanup_router = cleanup::router(space.clone(), issues.clone(), git.clone());
let public = Router::<()>::new()
.merge(crate::auth::routes::router(auth.clone()).with_state(auth.clone()))
.merge(chat::routes::router(chat.clone(), space.clone()).with_state(chat.clone()))
.merge(issues::routes::router(issues.clone()).with_state(issues))
.merge(space::routes::router(space.clone()).with_state(space))
.merge(user::routes::router(user.clone()).with_state(user))
.merge(git::routes::router(git.clone()).with_state(git.clone()))
.merge(pulls::routes::router().with_state(pulls))
.merge(cleanup_router);
let internal = Router::<()>::new()
.merge(git::routes::internal_router(git.clone()).with_state(git))
.merge(chat::routes::internal_router(chat.clone()).with_state(chat))
.with_state(internal_service_key)
.wrap(Guard::ServiceKey);
Router::<()>::new()
.nest("/api/v1/internal", internal)
.nest("/api/v1", public)
.nest("/pages", crate::auth::routes::pages_router(auth.clone()))
.merge(export::health::router(state))
.into_scope(web::scope(""))
}
pub fn verifier(state: &AppState) -> Option<Arc<token::JwtVerifier>> {
if !state.config.oauth.enabled {
return None;
}
match token::JwtVerifier::with_identity(
&state.config.oauth,
state
.kenauth
.as_ref()
.map(|client| client.service().signing().jwks().clone()),
) {
Ok(verifier) => Some(verifier),
Err(error) => {
tracing::warn!(%error, "oauth is enabled but the JWT verifier could not be built");
None
}
}
}