feat(cli): first working draft
This commit is contained in:
parent
b234914d8b
commit
30aa4707d4
4 changed files with 83 additions and 21 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 {
|
// for redis_cmd_key in redis_cmd_keys {
|
||||||
// redis_cmd_key.as_str().unwrap().split("_"); // Продолжи
|
// 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")]
|
#![doc = include_str!("../README.md")]
|
||||||
#![feature(try_blocks)]
|
|
||||||
extern crate core;
|
extern crate core;
|
||||||
|
|
||||||
mod ingest_protocol;
|
mod ingest_protocol;
|
||||||
mod ingest_socket_server;
|
mod ingest_socket_server;
|
||||||
mod utils;
|
mod utils;
|
||||||
mod web_server;
|
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]
|
#[ntex::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
//dotenvy::dotenv().unwrap();
|
let result = Cli::parse();
|
||||||
//
|
|
||||||
// let web_server_hndl = tokio::spawn(server_main());
|
let mut config = RedisConfig::default();
|
||||||
//
|
|
||||||
// web_server_hndl.await.unwrap();
|
config.server = ServerConfig::Centralized { server: Server::new(result.redis_host.clone(), result.redis_port.clone()) };
|
||||||
server_main().await;
|
|
||||||
|
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,20 +19,13 @@ pub struct NMAppState {
|
||||||
pub redis_client: RedisClient,
|
pub redis_client: RedisClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
use crate::cli::WebServerArgs;
|
||||||
use crate::web_server::old_device_sensor_api::device_handler;
|
use crate::web_server::old_device_sensor_api::device_handler;
|
||||||
|
use crate::{Cli};
|
||||||
|
|
||||||
use ntex::web;
|
use ntex::web;
|
||||||
|
|
||||||
pub async fn server_main() {
|
pub async fn server_main(args: Cli, specific_args: WebServerArgs, client: RedisClient) {
|
||||||
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();
|
|
||||||
|
|
||||||
let asd: Str = client.ping().await.unwrap();
|
let asd: Str = client.ping().await.unwrap();
|
||||||
|
|
||||||
println!("Ping result: {}", asd);
|
println!("Ping result: {}", asd);
|
||||||
|
@ -48,7 +41,7 @@ pub async fn server_main() {
|
||||||
.route("/get", web::route().to(device_handler))
|
.route("/get", web::route().to(device_handler))
|
||||||
.route("/post", 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()
|
.unwrap()
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
|
|
Loading…
Add table
Reference in a new issue