-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.rs
69 lines (58 loc) · 2.07 KB
/
mod.rs
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
66
67
68
69
use crate::module::Modules;
use crate::routes::health::{hc, hc_mongodb};
use crate::routes::information::info;
use crate::routes::mountain::{find_mountains, find_mountains_by_box, get_mountain};
use crate::routes::surrounding_mountain::find_surroundings;
use axum::http::Method;
use axum::{routing::get, Extension, Router};
use dotenv::dotenv;
use std::env;
use std::net::{IpAddr, SocketAddr};
use std::sync::Arc;
use tower_http::cors::{Any, CorsLayer};
pub async fn startup(modules: Arc<Modules>) {
let cors = CorsLayer::new()
.allow_methods([Method::GET, Method::OPTIONS, Method::HEAD])
.allow_origin(Any);
let hc_router = Router::new()
.route("/", get(hc))
.route("/mongo", get(hc_mongodb));
let mountain_router = Router::new()
.route("/", get(find_mountains))
.route("/:id", get(get_mountain))
.route("/:id/surroundings", get(find_surroundings))
.route("/geosearch", get(find_mountains_by_box));
let info_router = Router::new().route("/", get(info));
let app = Router::new()
.nest("/api/v1/", info_router)
.nest("/api/v1/hc", hc_router)
.nest("/api/v1/mountains", mountain_router)
.layer(cors)
.layer(Extension(modules));
let addr = SocketAddr::from(init_addr());
tracing::info!("Server listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap_or_else(|_| panic!("Server cannot launch."));
}
pub fn init_app() {
dotenv().ok();
tracing_subscriber::fmt::init();
}
fn init_addr() -> (IpAddr, u16) {
let env_host = env::var_os("HOST").expect("HOST is undefined.");
let ip_addr = env_host
.into_string()
.expect("HOST is invalid.")
.parse::<IpAddr>()
.expect("HOST is invalid.");
let env_port = env::var_os("PORT").expect("PORT is undefined.");
let port = env_port
.into_string()
.expect("PORT is invalid.")
.parse::<u16>()
.expect("PORT is invalid.");
tracing::debug!("Init ip address.");
(ip_addr, port)
}