pub mod cleanup; pub mod openapi; 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 = ::auth::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 auth_domain = auth.clone(); let public = Router::<()>::new() .setup(move |scope| { scope .app_data(web::Data::new(auth_domain)) .configure(::auth::routes::configure_api) }) .merge(::auth::routes_account::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", ::auth::routes_account::pages_router(auth.clone())) .merge(export::health::router(state)) .into_scope(web::scope("")) .default_service(web::to(::auth::assets::serve)) }