From 30aa4707d401c0146af2c134a408e6e2386ba2e1 Mon Sep 17 00:00:00 2001 From: nm17 Date: Wed, 8 Jan 2025 19:19:19 +0400 Subject: [PATCH] feat(cli): first working draft --- src/cli.rs | 40 +++++++++++++++++++++++++++++++++ src/ingest_socket_server/mod.rs | 10 ++++++++- src/main.rs | 39 ++++++++++++++++++++++++-------- src/web_server/mod.rs | 15 ++++--------- 4 files changed, 83 insertions(+), 21 deletions(-) create mode 100644 src/cli.rs diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..e26834b --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,40 @@ +use clap::{Args, Parser, Subcommand}; + +#[derive(Parser, Clone)] +pub struct Cli { + #[command(flatten)] + pub verbose: clap_verbosity_flag::Verbosity, + + #[arg(long, default_value = "localhost")] + pub redis_host: String, + + #[arg(long, default_value_t = 6379)] + pub redis_port: u16, + + #[command(subcommand)] + pub command: MyCommand, +} + +#[derive(Subcommand, Clone)] +pub enum MyCommand { + WebServer(WebServerArgs), + SocketServer(SocketServerArgs), +} + +#[derive(Args, Clone)] +pub struct WebServerArgs { + #[arg(short, long, default_value = "localhost")] + pub addr: String, + + #[arg(short, long, default_value_t = 8080)] + pub port: u16, +} + +#[derive(Args, Clone)] +pub struct SocketServerArgs { + #[arg(short, long, default_value = "localhost")] + pub addr: String, + + #[arg(short = 'p', default_value_t = 8283)] + pub port: u16, +} \ No newline at end of file diff --git a/src/ingest_socket_server/mod.rs b/src/ingest_socket_server/mod.rs index cdfb594..cb607d5 100644 --- a/src/ingest_socket_server/mod.rs +++ b/src/ingest_socket_server/mod.rs @@ -9,4 +9,12 @@ // for redis_cmd_key in redis_cmd_keys { // redis_cmd_key.as_str().unwrap().split("_"); // Продолжи // } -// } \ No newline at end of file +// } + +use fred::prelude::RedisClient; + +use crate::cli::{Cli, SocketServerArgs}; + +pub async fn socketserv_main(args: Cli, specific_args: SocketServerArgs, client: RedisClient) { + todo!() +} diff --git a/src/main.rs b/src/main.rs index 7808067..5b52a4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,42 @@ #![doc = include_str!("../README.md")] -#![feature(try_blocks)] extern crate core; mod ingest_protocol; mod ingest_socket_server; mod utils; mod web_server; -use crate::web_server::server_main; +mod cli; -struct Params {} +use clap::Parser; +use cli::{Cli, MyCommand}; +use fred::{ + clients::RedisClient, prelude::ClientLike, types::{ConnectionConfig, PerformanceConfig, ReconnectPolicy, RedisConfig, Server, ServerConfig} +}; +use ingest_socket_server::socketserv_main; + +use crate::web_server::server_main; #[ntex::main] async fn main() { - //dotenvy::dotenv().unwrap(); - // - // let web_server_hndl = tokio::spawn(server_main()); - // - // web_server_hndl.await.unwrap(); - server_main().await; + let result = Cli::parse(); + + let mut config = RedisConfig::default(); + + config.server = ServerConfig::Centralized { server: Server::new(result.redis_host.clone(), result.redis_port.clone()) }; + + let perf = PerformanceConfig::default(); + let policy = ReconnectPolicy::default(); + let connection_config = ConnectionConfig::default(); + let redis = RedisClient::new(config, Some(perf), Some(connection_config), Some(policy)); + + redis.init().await.unwrap(); + + match &result.command { + MyCommand::WebServer(specific_args) => { + server_main(result.clone(), specific_args.clone(), redis).await; + } + MyCommand::SocketServer(specific_args) => { + socketserv_main(result.clone(), specific_args.clone(), redis).await; + } + }; } diff --git a/src/web_server/mod.rs b/src/web_server/mod.rs index e8c4961..97d99d7 100644 --- a/src/web_server/mod.rs +++ b/src/web_server/mod.rs @@ -19,20 +19,13 @@ pub struct NMAppState { pub redis_client: RedisClient, } +use crate::cli::WebServerArgs; use crate::web_server::old_device_sensor_api::device_handler; +use crate::{Cli}; use ntex::web; -pub async fn server_main() { - let config = RedisConfig::default(); - let perf = PerformanceConfig::default(); - let policy = ReconnectPolicy::default(); - let client = RedisClient::new(config, Some(perf), Some(policy)); - - // connect to the server, returning a handle to the task that drives the connection - client.connect().await.unwrap().unwrap(); - client.wait_for_connect().await.unwrap(); - +pub async fn server_main(args: Cli, specific_args: WebServerArgs, client: RedisClient) { let asd: Str = client.ping().await.unwrap(); println!("Ping result: {}", asd); @@ -48,7 +41,7 @@ pub async fn server_main() { .route("/get", web::route().to(device_handler)) .route("/post", web::route().to(device_handler)) }) - .bind(("127.0.0.1", 8080)) + .bind((specific_args.addr.as_str(), specific_args.port)) .unwrap() .run() .await -- 2.48.1