-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
65 lines (53 loc) · 1.52 KB
/
Copy pathmain.rs
File metadata and controls
65 lines (53 loc) · 1.52 KB
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
55
56
57
58
59
60
61
62
63
64
65
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_migrations;
extern crate dotenv;
use std::io;
use std::time::Duration;
use actix_web::{App, HttpServer, web};
use actix_web::middleware::Logger;
use env_logger;
use log::error;
use crate::api::addresses::addresses;
use crate::data::state::refresh_state;
use crate::data::state::state_refresher::StateRefresher;
use crate::db::init_connection_pool;
mod api;
mod data;
mod db;
mod api_tests;
mod utils;
const DATA_REFRESH_INTERVAL_SECS: u64 = 3600 * 24;
embed_migrations!("./migrations");
#[actix_rt::main]
async fn main() -> io::Result<()> {
std::env::set_var("RUST_LOG", "info");
env_logger::init();
let pool = init_connection_pool();
let conn = pool.get().unwrap();
web::block(move || { embedded_migrations::run(&conn) })
.await
.expect("Error while running migrations");
if let Err(err) = refresh_state(&pool).await {
error!("Error while refreshing state: {}", err);
};
// Start background periodic state refresh
let refresher_pool = pool.clone();
actix_rt::spawn(async move {
let state_refresher = StateRefresher::new(
Duration::from_secs(DATA_REFRESH_INTERVAL_SECS),
false
);
state_refresher.start(&refresher_pool).await;
});
HttpServer::new(move || {
App::new()
.data(pool.clone())
.wrap(Logger::default())
.route("/addresses", web::get().to(addresses))
})
.bind("0.0.0.0:3000")?
.run()
.await
}