summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: f55a17c1919c191f18a28d8d1ed10c5c63216008 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
mod database;
mod routes;
mod structs;

use actix_web::{web, App, HttpServer};
use dotenv::dotenv;
use std::sync::Arc;

#[derive(Clone)]
struct AppState {
    database: Arc<database::DatabaseWrapper>,
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    // Errors can get very tough,
    // the rust log saved my ass
    std::env::set_var("RUST_LOG", "debug");
    env_logger::init();
    dotenv().ok();

    let db_raw = database::DatabaseWrapper::new()
        .await
        .expect("Something went wrong while creating DatabaseWrapper");
    let db = Arc::new(db_raw);
    let app_state = AppState { database: db };

    HttpServer::new(move || {
        App::new()
            .app_data(web::Data::new(app_state.clone()))
            .route("/", web::get().to(root))
            .service(routes::song::get_song)
            .service(routes::album::album)
            .service(routes::artist::get_artist)
            .service(routes::artist::post_artist)
            .service(routes::search_results::search_results)
    })
    .bind(("127.0.0.1", 8000))?
    .run()
    .await
}

async fn root() -> String {
    String::from("Server is up and running")
}