diff --git a/Cargo.lock b/Cargo.lock index 27ee903..4b86b5d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -284,6 +284,56 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e231f6134f61b71076a3eab506c379d4f36122f2af15a9ff04415ea4c3339e2" +dependencies = [ + "windows-sys 0.60.2", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e0633414522a32ffaac8ac6cc8f748e090c5717661fddeea04219e2344f5f2a" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.60.2", +] + [[package]] name = "arrayvec" version = "0.7.6" @@ -510,9 +560,55 @@ dependencies = [ "num-traits", "serde", "wasm-bindgen", - "windows-link", + "windows-link 0.1.3", ] +[[package]] +name = "clap" +version = "4.5.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c2cfd7bf8a6017ddaa4e32ffe7403d547790db06bd171c1c53926faab501623" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a4c05b9e80c5ccd3a7ef080ad7b6ba7d6fc00a985b8b157197075677c82c7a0" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.104", +] + +[[package]] +name = "clap_lex" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d" + +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + [[package]] name = "concurrent-queue" version = "2.5.0" @@ -1197,6 +1293,12 @@ dependencies = [ "hashbrown 0.15.4", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + [[package]] name = "itoa" version = "1.0.15" @@ -1234,6 +1336,7 @@ dependencies = [ "built", "bytestring", "chrono", + "clap", "dashmap", "dotenvy", "futures-util", @@ -1480,6 +1583,12 @@ version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + [[package]] name = "overload" version = "0.1.1" @@ -2300,6 +2409,12 @@ dependencies = [ "unicode-properties", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "subtle" version = "2.6.1" @@ -2634,6 +2749,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "uuid" version = "1.17.0" @@ -2801,7 +2922,7 @@ checksum = "c0fdd3ddb90610c7638aa2b3a3ab2904fb9e5cdbecc643ddb3647212781c4ae3" dependencies = [ "windows-implement", "windows-interface", - "windows-link", + "windows-link 0.1.3", "windows-result", "windows-strings", ] @@ -2834,13 +2955,19 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a" +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + [[package]] name = "windows-result" version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56f42bd332cc6c8eac5af113fc0c1fd6a8fd2aa08a0119358686e5160d0586c6" dependencies = [ - "windows-link", + "windows-link 0.1.3", ] [[package]] @@ -2849,7 +2976,7 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56e6c93f3a0c3b36176cb1327a4958a0353d5d166c2a35cb268ace15e91d3b57" dependencies = [ - "windows-link", + "windows-link 0.1.3", ] [[package]] @@ -2879,6 +3006,15 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows-sys" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" +dependencies = [ + "windows-targets 0.53.5", +] + [[package]] name = "windows-targets" version = "0.48.5" @@ -2903,13 +3039,30 @@ dependencies = [ "windows_aarch64_gnullvm 0.52.6", "windows_aarch64_msvc 0.52.6", "windows_i686_gnu 0.52.6", - "windows_i686_gnullvm", + "windows_i686_gnullvm 0.52.6", "windows_i686_msvc 0.52.6", "windows_x86_64_gnu 0.52.6", "windows_x86_64_gnullvm 0.52.6", "windows_x86_64_msvc 0.52.6", ] +[[package]] +name = "windows-targets" +version = "0.53.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" +dependencies = [ + "windows-link 0.2.1", + "windows_aarch64_gnullvm 0.53.1", + "windows_aarch64_msvc 0.53.1", + "windows_i686_gnu 0.53.1", + "windows_i686_gnullvm 0.53.1", + "windows_i686_msvc 0.53.1", + "windows_x86_64_gnu 0.53.1", + "windows_x86_64_gnullvm 0.53.1", + "windows_x86_64_msvc 0.53.1", +] + [[package]] name = "windows_aarch64_gnullvm" version = "0.48.5" @@ -2922,6 +3075,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" + [[package]] name = "windows_aarch64_msvc" version = "0.48.5" @@ -2934,6 +3093,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" +[[package]] +name = "windows_aarch64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" + [[package]] name = "windows_i686_gnu" version = "0.48.5" @@ -2946,12 +3111,24 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" +[[package]] +name = "windows_i686_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" + [[package]] name = "windows_i686_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" +[[package]] +name = "windows_i686_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" + [[package]] name = "windows_i686_msvc" version = "0.48.5" @@ -2964,6 +3141,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" +[[package]] +name = "windows_i686_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" + [[package]] name = "windows_x86_64_gnu" version = "0.48.5" @@ -2976,6 +3159,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" +[[package]] +name = "windows_x86_64_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" + [[package]] name = "windows_x86_64_gnullvm" version = "0.48.5" @@ -2988,6 +3177,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" + [[package]] name = "windows_x86_64_msvc" version = "0.48.5" @@ -3000,6 +3195,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "windows_x86_64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" + [[package]] name = "winnow" version = "0.7.11" diff --git a/Cargo.toml b/Cargo.toml index 0c41fa6..3fd4d5c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ actix-ws = { version = "0.3.0", default-features = false } async-trait = "0.1.88" bytestring = "1.4.0" chrono = { version = "0.4.41", features = ["serde"] } +clap = { version = "4.5.50", features = ["derive"] } dashmap = { version = "6.1.0", features = ["serde"] } dotenvy = "0.15.7" futures-util = "0.3.31" diff --git a/src/guards.rs b/src/guards.rs index b39fffb..60fbaf1 100644 --- a/src/guards.rs +++ b/src/guards.rs @@ -1,9 +1,13 @@ +use crate::get_args; use actix_web::guard::GuardContext; - pub const INTERNAL_KEY_HEADER: &str = "Kromer-Key"; pub fn internal_key_guard(ctx: &GuardContext) -> bool { - let kromer_key = std::env::var("INTERNAL_KEY").expect("No INTERNAL_KEY set in .env file"); + let args = get_args(); + let kromer_key = args.key.clone().unwrap_or_else(|| { + std::env::var("INTERNAL_KEY") + .expect("No INTERNAL_KEY set in .env file or command line argument (--key)") + }); ctx.head() .headers() .get(INTERNAL_KEY_HEADER) diff --git a/src/lib.rs b/src/lib.rs index 1fd8634..4052459 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,6 @@ +use clap::Parser; use sqlx::{Pool, Postgres}; +use tokio::sync::OnceCell; pub mod database; pub mod errors; @@ -7,6 +9,38 @@ pub mod models; pub mod routes; pub mod utils; pub mod websockets; +static ARGS: OnceCell = OnceCell::const_new(); + +/// A mostly Krist-Compatible currency server for ComputerCraft, made by ReconnectedCC. Args override environment variables. +#[derive(Parser, Debug)] +#[command(about, long_about = None)] + +pub struct Args { + /// Enable debug mode, prints debug messages to the console + + #[arg(short, long)] + pub debug: bool, + #[arg(long)] + /// Sets the Server URL + pub url: Option, + /// Sets the Database URL + #[arg(long)] + pub database_url: Option, + /// Sets the Internal Key + #[arg(long)] + pub key: Option, + /// Force Websocket to use the insecure "ws://" protocol + #[arg(short, long)] + pub insecure: bool, +} + +pub fn init_args(args: Args) { + ARGS.set(args).unwrap(); +} + +pub fn get_args() -> &'static Args { + ARGS.get().unwrap() +} pub mod build_info { // The file has been placed there by the build script. diff --git a/src/main.rs b/src/main.rs index 3e1acb4..1c541b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,34 @@ use actix_cors::Cors; use actix_web::{App, HttpServer, middleware, web}; +use clap::Parser; +use kromer::{AppState, Args, get_args, init_args, routes, websockets::WebSocketServer}; use sqlx::postgres::PgPool; use std::env; -use kromer::{AppState, routes, websockets::WebSocketServer}; - #[actix_web::main] async fn main() -> Result<(), Box> { - tracing_subscriber::fmt::init(); - + let parsed_args = Args::parse(); + init_args(parsed_args); + let args = get_args(); + if !args.debug { + tracing_subscriber::fmt::init(); + } else { + tracing_subscriber::fmt() + .with_max_level(tracing::Level::DEBUG) + .init(); + tracing::info!("Debug mode enabled"); + } dotenvy::dotenv().ok(); - let server_url = env::var("SERVER_URL").expect("SERVER_URL is not set in .env file"); - let database_url = env::var("DATABASE_URL").expect("DATABASE_URL is not set in .env file"); + let server_url = args.url.clone().unwrap_or_else(|| { + env::var("SERVER_URL") + .expect("SERVER_URL is not set in .env file or as command line argument (--url)") + }); + let database_url = args.database_url.clone().unwrap_or_else(|| { + env::var("DATABASE_URL").expect( + "DATABASE_URL is not set in .env file or as command line argument (--database_url)", + ) + }); let pool = PgPool::connect(&database_url).await?; diff --git a/src/models/mod.rs b/src/models/mod.rs index f89204f..55d0fa5 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,2 +1,2 @@ pub mod krist; -pub mod kromer; \ No newline at end of file +pub mod kromer; diff --git a/src/routes/krist/misc.rs b/src/routes/krist/misc.rs index 984c418..54c009e 100644 --- a/src/routes/krist/misc.rs +++ b/src/routes/krist/misc.rs @@ -103,9 +103,11 @@ async fn get_v2_address(query: web::Json) -> Result) -> Result { let pool = &state.pool; - let money_supply: Decimal = sqlx::query_scalar("SELECT COALESCE(SUM(balance), 0) FROM wallets WHERE address != 'serverwelf'") - .fetch_one(pool) - .await?; + let money_supply: Decimal = sqlx::query_scalar( + "SELECT COALESCE(SUM(balance), 0) FROM wallets WHERE address != 'serverwelf'", + ) + .fetch_one(pool) + .await?; Ok(HttpResponse::Ok().json(MoneySupplyResponse { ok: true, diff --git a/src/websockets/utils/make_url.rs b/src/websockets/utils/make_url.rs index 74d57dc..0d1dbd8 100644 --- a/src/websockets/utils/make_url.rs +++ b/src/websockets/utils/make_url.rs @@ -1,19 +1,22 @@ +use crate::get_args; use std::env; - use uuid::Uuid; -use crate::errors::{KromerError, websocket::WebSocketError}; +use crate::errors::KromerError; pub fn make_url(uuid: Uuid) -> Result { + let args = get_args(); let force_insecure = env::var("FORCE_WS_INSECURE").unwrap_or("true".to_owned()); - let schema = if force_insecure == "true" { + let schema = if force_insecure == "true" || args.insecure { "ws" } else { "wss" }; - let server_url = env::var("PUBLIC_URL") - .map_err(|_| KromerError::WebSocket(WebSocketError::ServerConfigError))?; + let server_url = args.url.clone().unwrap_or_else(|| { + env::var("PUBLIC_URL") + .unwrap_or_else(|_| env::var("SERVER_URL").unwrap_or("localhost:8080".to_owned())) + }); Ok(format!( "{schema}://{server_url}/api/krist/ws/gateway/{uuid}"