๐Ÿ“ฆ rparrett / wwd-actix

๐Ÿ“„ main.rs ยท 54 lines
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
46
47
48
49
50
51
52
53
54#[macro_use]
extern crate log;
extern crate env_logger;

use actix_files as fs;
use actix_web::middleware::Logger;
use actix_web::{web, App, HttpServer};
use chrono::{DateTime, Utc};
use std::sync::{Arc, Mutex};

mod config;
mod filters;
mod forecast;
mod handler;

pub struct AppState {
    forecasts: Vec<forecast::BasicWeekendForecast>,
    last_fetch: Option<DateTime<Utc>>,
}

fn main() -> std::io::Result<()> {
    // std::env::set_var("RUST_LOG", "info,actix_web=info");
    env_logger::init();

    let config = config::Config::new("config.toml").expect("Failed to open config file.");
    let addr = config.http.unwrap().addr.unwrap();

    let state = Arc::new(Mutex::new(AppState {
        forecasts: Vec::new(),
        last_fetch: None,
    }));

    let mut grabber = forecast::ForecastGrabber::new(
        state.clone(),
        config.darksky.unwrap(),
        config.locations.unwrap(),
    );

    // start http server
    HttpServer::new(move || {
        App::new()
            .wrap(Logger::default())
            .data(state.clone())
            .service(fs::Files::new("/static", "static"))
            .service(web::resource("/").route(web::get().to(handler::index)))
    })
    .bind(addr)?
    .run();

    grabber.exit_join();

    Ok(())
}