CLI интерфейс #18

Manually merged
DarkCat09 merged 1 commit from cli into master 2025-01-09 17:00:39 +03:00
4 changed files with 83 additions and 21 deletions

40
src/cli.rs Normal file
View file

@ -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,
}

View file

@ -10,3 +10,11 @@
// redis_cmd_key.as_str().unwrap().split("_"); // Продолжи
// }
// }
use fred::prelude::RedisClient;
use crate::cli::{Cli, SocketServerArgs};
pub async fn socketserv_main(args: Cli, specific_args: SocketServerArgs, client: RedisClient) {
todo!()
}

View file

@ -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;
}
};
}

View file

@ -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