use futures::future::TryFutureExt;
use std::net::Ipv4Addr;
use tokio::sync::oneshot;
use warp;

mod database;
mod handler;
mod model;
mod route;

const LISTENER_ADDRESS: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);
const LISTENER_PORT: u16 = 3100;
const PAYLOAD_LIMIT: u64 = 1024 * 64;

#[tokio::main]
async fn main() {
    let database = database::init_database();
    let routes = route::guild_setting_routes(database);

    // TODO: Fix me for handling SIGINT
    //  If using before this is fixed, just comment it out and replace with
    //  warp::serve(routes).run(([127, 0, 0, 1], 3100)).await;
    let (tx, rx) = oneshot::channel();

    let (_, server) =
        warp::serve(routes).bind_with_graceful_shutdown((LISTENER_ADDRESS, LISTENER_PORT), async {
            rx.await.ok();
        });

    tokio::task::spawn(server);

    let _ = tx.send(());
}
