Merge branch 'cli'
This commit is contained in:
commit
a87ffa958f
4 changed files with 83 additions and 15 deletions
40
src/cli.rs
Normal file
40
src/cli.rs
Normal 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,
|
||||
}
|
|
@ -9,4 +9,12 @@
|
|||
// for redis_cmd_key in redis_cmd_keys {
|
||||
// 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!()
|
||||
}
|
||||
|
|
39
src/main.rs
39
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;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -19,14 +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 client = RedisClient::default();
|
||||
client.init().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);
|
||||
|
@ -42,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
|
||||
|
|
Loading…
Add table
Reference in a new issue