feat!: improved CLI: separated actions from backend
This commit is contained in:
parent
5c5e878cea
commit
28b32c8135
1 changed files with 70 additions and 41 deletions
51
src/main.rs
51
src/main.rs
|
@ -2,12 +2,14 @@ mod proto;
|
|||
mod entities;
|
||||
mod routes;
|
||||
mod persistence;
|
||||
mod maintenance;
|
||||
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use chrono::{DateTime, Utc, Duration};
|
||||
use clap::Parser;
|
||||
use clap::{Parser, Subcommand};
|
||||
use axum::{Router, routing::{get, post}, response::IntoResponse, Json, http::StatusCode};
|
||||
use routes::register::fill_missing_skins;
|
||||
use sea_orm::{DatabaseConnection, Database};
|
||||
use tokio::sync::Mutex;
|
||||
use tracing_subscriber::filter::filter_fn;
|
||||
|
@ -23,10 +25,19 @@ use crate::{routes::{
|
|||
/// Reimplementation of legacy auth server for minecraft
|
||||
#[derive(Parser, Debug, Clone)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
struct ConfigArgs {
|
||||
struct CliArgs {
|
||||
/// Connection string for database
|
||||
database: String,
|
||||
|
||||
/// Action to run
|
||||
#[clap(subcommand)]
|
||||
action: CliAction,
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Clone, Debug)]
|
||||
enum CliAction {
|
||||
/// run yggdrasil backend service
|
||||
Serve {
|
||||
/// Address to bind web application onto
|
||||
#[arg(short, long, default_value = "127.0.0.1:26656")]
|
||||
bind_addr: String,
|
||||
|
@ -35,10 +46,6 @@ struct ConfigArgs {
|
|||
#[arg(long, default_value_t = 3600)]
|
||||
token_duration: u32,
|
||||
|
||||
/// How long an access token is refreshable, in hours
|
||||
#[arg(long, default_value_t = 168)]
|
||||
token_lifetime: u32,
|
||||
|
||||
/// Valid time for join requests, in seconds
|
||||
#[arg(short, long, default_value_t = 10)]
|
||||
time_window: u32,
|
||||
|
@ -46,6 +53,20 @@ struct ConfigArgs {
|
|||
/// Enable join request fallback to Microsoft
|
||||
#[arg(long)]
|
||||
fallback: bool,
|
||||
},
|
||||
|
||||
/// remove expired tokens
|
||||
Clean {
|
||||
/// How long an access token is refreshable, in hours
|
||||
#[arg(long, default_value_t = 168)]
|
||||
token_lifetime: u32,
|
||||
},
|
||||
|
||||
/// fetch missing skins
|
||||
Skins {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
@ -64,7 +85,7 @@ impl JoinAttempt {
|
|||
pub struct AppState {
|
||||
store: Arc<Mutex<HashMap<Uuid, JoinAttempt>>>,
|
||||
db: DatabaseConnection,
|
||||
cfg: ConfigArgs,
|
||||
cfg: CliArgs,
|
||||
secret: String,
|
||||
}
|
||||
|
||||
|
@ -76,17 +97,23 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
|
||||
let cfg = ConfigArgs::parse();
|
||||
let cfg = CliArgs::parse();
|
||||
|
||||
let db = Database::connect(cfg.database.clone()).await?;
|
||||
|
||||
purge_expired_tokens(&db, Duration::hours(cfg.token_lifetime.into())).await?;
|
||||
|
||||
match cfg.action {
|
||||
CliAction::Clean { token_lifetime } => {
|
||||
purge_expired_tokens(&db, Duration::hours(token_lifetime.into())).await?;
|
||||
},
|
||||
CliAction::Skins { } => {
|
||||
fill_missing_skins(&db).await?;
|
||||
},
|
||||
CliAction::Serve { bind_addr, token_duration, time_window, fallback } => {
|
||||
let secret = load_secret(&db).await?;
|
||||
|
||||
let store = Arc::new(Mutex::new(HashMap::new())); // TODO do this as an Actor
|
||||
|
||||
let addr = cfg.bind_addr.parse()?;
|
||||
let addr = bind_addr.parse()?;
|
||||
|
||||
let app = Router::new()
|
||||
// AUTH
|
||||
|
@ -107,6 +134,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
axum::Server::bind(&addr)
|
||||
.serve(app.into_make_service())
|
||||
.await?;
|
||||
},
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue