Geoffrey-rs/geoffrey_api/src/main.rs

65 lines
1.6 KiB
Rust
Raw Normal View History

mod commands;
mod config;
mod context;
mod helper;
mod logging;
use crate::commands::command_filter;
use crate::config::GeoffreyAPIConfig;
use crate::context::Context;
2021-10-23 19:13:28 +00:00
use crate::logging::{init_logging, LogLevel};
use geoffrey_models::models::response::api_error::GeoffreyAPIError;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::str::FromStr;
use structopt::StructOpt;
pub type Result<T> = std::result::Result<T, GeoffreyAPIError>;
#[derive(Debug, StructOpt, Clone)]
#[structopt(name = "GeoffreyAPI", about = "Geoffrey Central API")]
struct Args {
#[structopt(env = "GEOFFREY_CONFIG", parse(from_os_str))]
config: PathBuf,
2021-10-23 19:13:28 +00:00
#[structopt(
short,
long,
env = "GEOFFREY_LOG_LEVEL",
parse(from_str),
default_value = "Info"
)]
log_level: LogLevel,
}
#[tokio::main]
async fn main() {
let args: Args = Args::from_args();
if let Err(e) = init_logging(args.log_level) {
println!("Unable to initialize logger: {}", e);
return;
}
let cfg = match GeoffreyAPIConfig::new(args.config.as_path()) {
Ok(cfg) => cfg,
Err(e) => {
log::warn!("Error opening config: {}", e);
return;
}
};
let socket_addr = match SocketAddr::from_str(cfg.host.as_str()) {
Ok(socket_addr) => socket_addr,
Err(e) => {
log::warn!("Error parsing {} as address: {}", cfg.host, e);
return;
}
};
let ctx = Context::new(cfg).unwrap();
let api = command_filter(ctx.clone());
2021-10-23 19:13:28 +00:00
warp::serve(api).run(socket_addr).await;
}