[WIP] tangled knot rust implementation
at main 64 lines 1.8 kB view raw
1mod git; 2mod events; 3mod xrpc; 4mod ingest; 5 6use axum::{ 7 Router, 8 http::{Method, StatusCode, header}, 9 response::{IntoResponse as _, Response}, 10 routing::{get, post}, 11}; 12use tower_http::cors::{Any, CorsLayer}; 13 14use crate::config; 15 16pub async fn daemon(config: config::Config) -> anyhow::Result<()> { 17 tokio::select! { 18 res = start_server(&config) => res, 19 res = start_js_ingestor(&config) => res, 20 } 21} 22 23async fn start_server(config: &config::Config) -> anyhow::Result<()> { 24 let app = init_router(config); 25 26 let listener = tokio::net::TcpListener::bind(config.listen_addr.clone()).await?; 27 axum::serve(listener, app).await?; 28 29 Ok(()) 30} 31 32async fn start_js_ingestor(config: &config::Config) -> anyhow::Result<()> { 33 ingest::start(config.jetstream_endpoint.clone()).await 34} 35 36fn init_router(_config: &config::Config) -> Router { 37 let cors = CorsLayer::new() 38 .allow_methods([Method::GET, Method::POST]) 39 .allow_headers([header::AUTHORIZATION, header::ACCEPT, header::CONTENT_TYPE]) 40 .allow_origin(Any); 41 42 Router::new() 43 .route("/", get(index)) 44 .nest("/{did}/{name}", { 45 // git op routes 46 Router::new() 47 .route("/info/refs", get(git::info_refs)) 48 .route("/git-upload-pack", post(git::git_upload_pack)) 49 .route("/git-receive-pack", post(git::git_receive_pack)) 50 .route("/archive", get(git::archive)) 51 // .layer(redirect_handle) 52 }) 53 .nest("/xrpc", { 54 Router::new() 55 .route("/", get(index)) 56 }) 57 .route("/events", get(events::events)) 58 .layer(cors) 59} 60 61#[axum::debug_handler] 62async fn index() -> Result<Response, StatusCode> { 63 Ok("hello world. this is knot server".into_response()) 64}