2021-10-31 17:00:07 +00:00
|
|
|
use std::convert::TryFrom;
|
2021-06-01 00:54:55 +00:00
|
|
|
use std::net::SocketAddr;
|
2021-10-03 20:03:32 +00:00
|
|
|
use std::path::PathBuf;
|
2021-06-01 00:54:55 +00:00
|
|
|
use std::str::FromStr;
|
2021-10-31 17:00:07 +00:00
|
|
|
use std::sync::Arc;
|
2021-12-12 18:55:09 +00:00
|
|
|
|
2021-10-03 20:03:32 +00:00
|
|
|
use structopt::StructOpt;
|
|
|
|
|
2021-12-12 18:55:09 +00:00
|
|
|
use geoffrey_models::logging::LogLevel;
|
|
|
|
use geoffrey_models::models::parameters::add_token_params::AddTokenParams;
|
|
|
|
use geoffrey_models::models::response::api_error::GeoffreyAPIError;
|
|
|
|
use geoffrey_models::models::token::Permissions;
|
|
|
|
|
|
|
|
use crate::commands::add_token::AddToken;
|
2021-12-13 02:30:38 +00:00
|
|
|
use crate::commands::{command_filter, Command};
|
2021-12-12 18:55:09 +00:00
|
|
|
use crate::config::GeoffreyAPIConfig;
|
|
|
|
use crate::context::Context;
|
|
|
|
use crate::logging::init_logging;
|
|
|
|
|
|
|
|
mod commands;
|
|
|
|
mod config;
|
|
|
|
mod context;
|
|
|
|
mod helper;
|
|
|
|
mod logging;
|
|
|
|
|
2021-10-03 20:03:32 +00:00
|
|
|
pub type Result<T> = std::result::Result<T, GeoffreyAPIError>;
|
2021-06-01 00:54:55 +00:00
|
|
|
|
|
|
|
#[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"
|
|
|
|
)]
|
2021-10-17 19:09:50 +00:00
|
|
|
log_level: LogLevel,
|
2021-10-31 17:00:07 +00:00
|
|
|
|
|
|
|
#[structopt(subcommand)]
|
|
|
|
command: GeoffreyApiCommand,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, StructOpt, Clone)]
|
|
|
|
enum GeoffreyApiCommand {
|
|
|
|
Run,
|
|
|
|
CreateAdminToken(CreateTokenCommand),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, StructOpt, PartialEq, Clone)]
|
|
|
|
struct CreateTokenCommand {
|
|
|
|
#[structopt(parse(try_from_str = Permissions::try_from))]
|
|
|
|
pub permissions: Vec<Permissions>,
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn run_server(ctx: Arc<Context>) {
|
|
|
|
let socket_addr = match SocketAddr::from_str(ctx.cfg.host.as_str()) {
|
|
|
|
Ok(socket_addr) => socket_addr,
|
|
|
|
Err(e) => {
|
|
|
|
log::warn!("Error parsing {} as address: {}", ctx.cfg.host, e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let api = command_filter(ctx.clone());
|
|
|
|
|
|
|
|
warp::serve(api).run(socket_addr).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_token(ctx: Arc<Context>, perms: Vec<Permissions>) {
|
2021-12-18 03:03:15 +00:00
|
|
|
match AddToken::run_command(ctx, &AddTokenParams { permissions: perms }, None) {
|
2021-10-31 17:00:07 +00:00
|
|
|
Ok(token) => {
|
|
|
|
// Don't log this to keep tokens out of the log
|
|
|
|
println!("Added admin token with secret: {}", token.secret)
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
log::warn!("Unable to create admin token: {}", e)
|
|
|
|
}
|
|
|
|
}
|
2021-06-01 00:54:55 +00:00
|
|
|
}
|
2021-05-08 20:42:47 +00:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2021-06-01 00:54:55 +00:00
|
|
|
let args: Args = Args::from_args();
|
|
|
|
|
2021-10-17 19:09:50 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-01 00:54:55 +00:00
|
|
|
let ctx = Context::new(cfg).unwrap();
|
|
|
|
|
2021-10-31 17:00:07 +00:00
|
|
|
match args.command {
|
|
|
|
GeoffreyApiCommand::Run => run_server(ctx).await,
|
|
|
|
GeoffreyApiCommand::CreateAdminToken(perms) => create_token(ctx, perms.permissions),
|
|
|
|
};
|
2021-10-03 20:03:32 +00:00
|
|
|
}
|