use std::env;
use envconfig::Envconfig;
use sataddress::{api, db, handlers, with_clone, Config};
use warp::Filter;
use log::*;
use std::collections::HashMap;
#[tokio::main]
async fn main() {
if env::var_os("RUST_LOG").is_none() {
env::set_var("RUST_LOG", "debug");
}
pretty_env_logger::init();
let config = Config::init_from_env().unwrap();
debug!("Running with the following config {:?}", config);
let db = db::Db::init().unwrap();
let base_dir = format!("{}/", env!("CARGO_MANIFEST_DIR"));
let index = warp::path::end()
.and(with_clone(config.clone()))
.and_then(handlers::index);
let statics = warp::path("static").and(warp::fs::dir(format!("{}assets/", base_dir)));
let base = warp::any()
.and(with_clone(db.clone()))
.and(with_clone(config.clone()));
let ln_url = base
.clone()
.and(warp::path!(".well-known" / "lnurlp" / String))
.and(warp::host::optional())
.and_then(api::check_domain)
.untuple_one()
.and(warp::query::<HashMap<String, String>>())
.and_then(handlers::lnurl);
let grab = base
.clone()
.and(warp::path("grab"))
.and(warp::body::aggregate())
.and_then(handlers::grab);
let api = warp::path!("api" / "v1" / ..)
.and(warp::header::<String>("X-PIN"))
.and(with_clone(config.clone()))
.and_then(api::authenticate)
.untuple_one()
.and(api::handlers(db.clone(), config.clone()));
let routes = warp::any().and(
index
.or(statics)
.or(ln_url)
.or(grab)
.or(api)
.recover(handlers::handle_rejection),
);
info!("Starting server...");
warp::serve(routes).run((config.host, config.port)).await;
}