use actix_web::{App, HttpServer, web::Data};
use dotenvy::dotenv;

use sqlx::mysql::{MySqlPool, MySqlPoolOptions};
use std::env;

mod api;
use api::{sample};

#[derive(Clone, Debug)]
pub struct AppState {
    pool: MySqlPool,
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    dotenv().ok();
    let db_connection = env::var("DATABASE_URL").unwrap();
    let pool: MySqlPool = MySqlPoolOptions::new()
        .connect(db_connection.as_str())
        .await
        .unwrap();
    let app_state = AppState { pool };

    HttpServer::new(move || {
        App::new()
            .app_data(Data::new(app_state.pool.clone()))
            .service(sample)
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}
