mod git; mod events; mod xrpc; mod ingest; use axum::{ Router, http::{Method, StatusCode, header}, response::{IntoResponse as _, Response}, routing::{get, post}, }; use tower_http::cors::{Any, CorsLayer}; use crate::config; pub async fn daemon(config: config::Config) -> anyhow::Result<()> { tokio::select! { res = start_server(&config) => res, res = start_js_ingestor(&config) => res, } } async fn start_server(config: &config::Config) -> anyhow::Result<()> { let app = init_router(config); let listener = tokio::net::TcpListener::bind(config.listen_addr.clone()).await?; axum::serve(listener, app).await?; Ok(()) } async fn start_js_ingestor(config: &config::Config) -> anyhow::Result<()> { ingest::start(config.jetstream_endpoint.clone()).await } fn init_router(_config: &config::Config) -> Router { let cors = CorsLayer::new() .allow_methods([Method::GET, Method::POST]) .allow_headers([header::AUTHORIZATION, header::ACCEPT, header::CONTENT_TYPE]) .allow_origin(Any); Router::new() .route("/", get(index)) .nest("/{did}/{name}", { // git op routes Router::new() .route("/info/refs", get(git::info_refs)) .route("/git-upload-pack", post(git::git_upload_pack)) .route("/git-receive-pack", post(git::git_receive_pack)) .route("/archive", get(git::archive)) // .layer(redirect_handle) }) .nest("/xrpc", { Router::new() .route("/", get(index)) }) .route("/events", get(events::events)) .layer(cors) } #[axum::debug_handler] async fn index() -> Result { Ok("hello world. this is knot server".into_response()) }