use actix_web::{middleware::Logger, web, App, HttpServer, Responder}; use bb8_postgres::tokio_postgres; mod actor; mod apub; mod args; mod config; mod db; mod error; mod inbox; mod nodeinfo; mod notify; mod requests; mod responses; mod state; mod verifier; mod webfinger; use self::{args::Args, config::Config, db::Db, state::State, webfinger::RelayResolver}; async fn index() -> impl Responder { "hewwo, mr obama" } #[actix_rt::main] async fn main() -> Result<(), anyhow::Error> { dotenv::dotenv().ok(); let config = Config::build()?; if config.debug() { std::env::set_var("RUST_LOG", "debug") } else { std::env::set_var("RUST_LOG", "info") } pretty_env_logger::init(); let pg_config: tokio_postgres::Config = config.database_url().parse()?; let db = Db::build(pg_config.clone()).await?; let args = Args::new(); if !args.blocks().is_empty() || !args.whitelists().is_empty() { if args.undo() { db.remove_blocks(args.blocks()).await?; db.remove_whitelists(args.whitelists()).await?; } else { db.add_blocks(args.blocks()).await?; db.add_whitelists(args.whitelists()).await?; } return Ok(()); } let state = State::hydrate(config.clone(), &db).await?; let _ = notify::NotifyHandler::start_handler(state.clone(), pg_config.clone()); let bind_address = config.bind_address(); HttpServer::new(move || { App::new() .wrap(Logger::default()) .data(db.clone()) .data(state.clone()) .data(state.requests()) .data(config.clone()) .service(web::resource("/").route(web::get().to(index))) .service( web::resource("/inbox") .wrap(config.digest_middleware()) .wrap(config.signature_middleware(state.requests())) .route(web::post().to(inbox::inbox)), ) .service(web::resource("/actor").route(web::get().to(actor::route))) .service(web::resource("/nodeinfo/2.0").route(web::get().to(nodeinfo::route))) .service( web::scope("/.well-known") .service(actix_webfinger::scoped::<_, RelayResolver>()) .service(web::resource("/nodeinfo").route(web::get().to(nodeinfo::well_known))), ) }) .bind(bind_address)? .run() .await?; Ok(()) }