Skip to content

Commit 926e309

Browse files
committed
Added cli args based parsing
1 parent f49de9d commit 926e309

File tree

8 files changed

+132
-38
lines changed

8 files changed

+132
-38
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ homepage = "https://github.com/bytesonus/gotham-rust"
99
repository = "https://github.com/bytesonus/gotham-rust"
1010

1111
[dependencies]
12-
async-std = "1.4.0"
12+
async-std = "1.5.0"
1313
serde_json = "*"
1414
async-trait = "0.1.24"
1515
futures = "0.3.4"
1616
futures-util = "0.3.4"
17+
clap = "2.33.0"
1718

1819
[profile.release]
1920
lto = true

src/connection/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
mod base_connection;
2+
mod inet_socket_connection;
23
#[cfg(target_family = "unix")]
34
mod unix_socket_connection;
4-
mod inet_socket_connection;
55

66
pub use base_connection::BaseConnection;
7+
pub use inet_socket_connection::InetSocketConnection;
78
#[cfg(target_family = "unix")]
89
pub use unix_socket_connection::UnixSocketConnection;
9-
pub use inet_socket_connection::InetSocketConnection;
1010

1111
pub type Buffer = Vec<u8>;

src/gotham_module.rs

Lines changed: 114 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
extern crate async_std;
2+
extern crate clap;
3+
extern crate futures;
4+
extern crate futures_util;
5+
extern crate serde_json;
6+
17
use crate::{
28
connection::{BaseConnection, Buffer, InetSocketConnection},
39
models::{BaseMessage, Value},
@@ -8,17 +14,18 @@ use crate::{
814
#[cfg(target_family = "unix")]
915
use crate::connection::UnixSocketConnection;
1016

11-
use std::collections::HashMap;
1217
use async_std::{
1318
prelude::*,
1419
sync::{Arc, Mutex},
1520
task,
1621
};
22+
use clap::{App, Arg};
1723
use futures::channel::{
1824
mpsc::{UnboundedReceiver, UnboundedSender},
1925
oneshot::{channel, Sender},
2026
};
2127
use futures_util::sink::SinkExt;
28+
use std::collections::HashMap;
2229

2330
type ArcRequestList = Arc<Mutex<HashMap<String, Sender<Result<Value>>>>>;
2431
type ArcFunctionList = Arc<Mutex<HashMap<String, fn(HashMap<String, Value>) -> Value>>>;
@@ -35,24 +42,103 @@ pub struct GothamModule {
3542
}
3643

3744
impl GothamModule {
38-
#[cfg(target_family = "unix")]
39-
pub fn default(socket_path: String) -> Self {
40-
GothamModule {
41-
protocol: BaseProtocol::default(),
42-
connection: Box::new(UnixSocketConnection::new(socket_path)),
43-
requests: Arc::new(Mutex::new(HashMap::new())),
44-
functions: Arc::new(Mutex::new(HashMap::new())),
45-
hook_listeners: Arc::new(Mutex::new(HashMap::new())),
46-
message_buffer: vec![],
47-
registered: false,
45+
#[allow(clippy::collapsible_if)]
46+
pub fn from_cli_args() -> Self {
47+
let args = App::new(utils::APP_NAME)
48+
.version(utils::APP_VERSION)
49+
.author(utils::APP_AUTHORS)
50+
.about("Micro-services framework")
51+
.arg(
52+
Arg::with_name("socket-location")
53+
.conflicts_with("port")
54+
.conflicts_with("host")
55+
.short("s")
56+
.long("socket-location")
57+
.takes_value(true)
58+
.value_name("FILE")
59+
.help("Sets the location of the socket to connect"),
60+
)
61+
.arg(
62+
Arg::with_name("port")
63+
.conflicts_with("socket-location")
64+
.short("p")
65+
.long("port")
66+
.takes_value(true)
67+
.value_name("PORT")
68+
.help("Sets the port for the socket to connect to"),
69+
)
70+
.arg(
71+
Arg::with_name("host")
72+
.conflicts_with("socket-location")
73+
.short("h")
74+
.long("host")
75+
.takes_value(true)
76+
.value_name("HOST-IP")
77+
.help("Sets the host address for the socket to connect"),
78+
)
79+
.arg(
80+
Arg::with_name("V")
81+
.short("V")
82+
.multiple(true)
83+
.help("Sets the level of verbosity (max 3)"),
84+
)
85+
.arg(
86+
Arg::with_name("version")
87+
.short("v")
88+
.long("version")
89+
.help("Prints version information"),
90+
)
91+
.get_matches();
92+
93+
if args.is_present("version") {
94+
println!("{}", utils::APP_VERSION);
95+
panic!();
96+
}
97+
98+
let mut default_socket_location = std::env::current_dir().unwrap();
99+
default_socket_location.push(args.value_of("socket-location").unwrap_or("../gotham.sock"));
100+
let default_socket_location = default_socket_location.as_os_str().to_str().unwrap();
101+
102+
if cfg!(target_family = "windows") {
103+
if args.value_of("socket-location").is_some() {
104+
panic!("Listening on unix sockets are not supported on windows");
105+
} else {
106+
GothamModule::from_inet_socket(
107+
args.value_of("host").unwrap_or("127.0.0.1"),
108+
args.value_of("port")
109+
.unwrap_or("2203")
110+
.parse::<u16>()
111+
.unwrap(),
112+
)
113+
}
114+
} else {
115+
if args.value_of("port").is_some() {
116+
GothamModule::from_inet_socket(
117+
args.value_of("host").unwrap_or("127.0.0.1"),
118+
args.value_of("port")
119+
.unwrap_or("2203")
120+
.parse::<u16>()
121+
.unwrap(),
122+
)
123+
} else {
124+
GothamModule::from_unix_socket(
125+
args.value_of("socket-location")
126+
.unwrap_or(default_socket_location),
127+
)
128+
}
48129
}
49130
}
50131

51132
#[cfg(target_family = "windows")]
52-
pub fn default(port: u16) -> Self {
133+
pub fn from_unix_socket(socket_path: &str) -> Self {
134+
panic!("Unix sockets are not supported on windows");
135+
}
136+
137+
#[cfg(target_family = "unix")]
138+
pub fn from_unix_socket(socket_path: &str) -> Self {
53139
GothamModule {
54140
protocol: BaseProtocol::default(),
55-
connection: Box::new(InetSocketConnection::new(format!("127.0.0.1:{}", port))),
141+
connection: Box::new(UnixSocketConnection::new(socket_path.to_string())),
56142
requests: Arc::new(Mutex::new(HashMap::new())),
57143
functions: Arc::new(Mutex::new(HashMap::new())),
58144
hook_listeners: Arc::new(Mutex::new(HashMap::new())),
@@ -61,10 +147,10 @@ impl GothamModule {
61147
}
62148
}
63149

64-
pub fn with_inet_socket(socket: String) -> Self {
150+
pub fn from_inet_socket(host: &str, port: u16) -> Self {
65151
GothamModule {
66152
protocol: BaseProtocol::default(),
67-
connection: Box::new(InetSocketConnection::new(socket)),
153+
connection: Box::new(InetSocketConnection::new(format!("{}:{}", host, port))),
68154
requests: Arc::new(Mutex::new(HashMap::new())),
69155
functions: Arc::new(Mutex::new(HashMap::new())),
70156
hook_listeners: Arc::new(Mutex::new(HashMap::new())),
@@ -87,13 +173,15 @@ impl GothamModule {
87173

88174
pub async fn initialize(
89175
&mut self,
90-
module_id: String,
91-
version: String,
176+
module_id: &str,
177+
version: &str,
92178
dependencies: HashMap<String, String>,
93179
) -> Result<()> {
94180
self.setup_connections().await?;
95181

96-
let request = self.protocol.initialize(module_id, version, dependencies);
182+
let request =
183+
self.protocol
184+
.initialize(String::from(module_id), String::from(version), dependencies);
97185
self.send_request(request).await?;
98186

99187
self.registered = true;
@@ -102,9 +190,10 @@ impl GothamModule {
102190

103191
pub async fn declare_function(
104192
&mut self,
105-
fn_name: String,
193+
fn_name: &str,
106194
function: fn(HashMap<String, Value>) -> Value,
107195
) -> Result<()> {
196+
let fn_name = fn_name.to_string();
108197
self.functions
109198
.lock()
110199
.await
@@ -117,15 +206,17 @@ impl GothamModule {
117206

118207
pub async fn call_function(
119208
&mut self,
120-
fn_name: String,
209+
fn_name: &str,
121210
args: HashMap<String, Value>,
122211
) -> Result<Value> {
212+
let fn_name = fn_name.to_string();
123213
self.ensure_registered()?;
124214
let request = self.protocol.call_function(fn_name, args);
125215
self.send_request(request).await
126216
}
127217

128-
pub async fn register_hook(&mut self, hook: String, callback: fn(Value)) -> Result<()> {
218+
pub async fn register_hook(&mut self, hook: &str, callback: fn(Value)) -> Result<()> {
219+
let hook = hook.to_string();
129220
self.ensure_registered()?;
130221
let mut hook_listeners = self.hook_listeners.lock().await;
131222
if hook_listeners.contains_key(&hook) {
@@ -140,7 +231,8 @@ impl GothamModule {
140231
Ok(())
141232
}
142233

143-
pub async fn trigger_hook(&mut self, hook: String) -> Result<()> {
234+
pub async fn trigger_hook(&mut self, hook: &str) -> Result<()> {
235+
let hook = hook.to_string();
144236
let request = self.protocol.trigger_hook(hook);
145237
self.send_request(request).await?;
146238
Ok(())

src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
extern crate async_std;
2-
extern crate futures;
3-
extern crate futures_util;
4-
extern crate serde_json;
5-
61
mod gotham_module;
72
mod utils;
83

src/protocol/json_protocol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::{
44
protocol::base_protocol::BaseProtocol,
55
utils::{request_keys, request_types},
66
};
7-
use std::collections::HashMap;
87
use serde_json::{from_slice, json, Map, Result, Value};
8+
use std::collections::HashMap;
99

1010
pub fn default() -> BaseProtocol {
1111
BaseProtocol::JsonProtocol {

src/utils/constants.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
use clap::{crate_authors, crate_name, crate_version};
2+
3+
pub const APP_NAME: &str = crate_name!();
4+
pub const APP_VERSION: &str = crate_version!();
5+
pub const APP_AUTHORS: &str = crate_authors!();
6+
17
pub mod request_keys {
28
pub const TYPE: &str = "type";
39
pub const REQUEST_ID: &str = "requestId";

src/utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod constants;
22
mod error;
33

4-
pub use constants::{errors, request_keys, request_types};
4+
pub use constants::{errors, request_keys, request_types, APP_AUTHORS, APP_NAME, APP_VERSION};
55
pub use error::{Error, Result};

tests/sample.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ use std::collections::HashMap;
55
#[test]
66
fn sample() {
77
let module1 = async {
8-
let mut module = GothamModule::default(String::from("../gotham.sock"));
8+
let mut module = GothamModule::from_unix_socket("../gotham.sock");
99
module
10-
.initialize("module1".to_string(), "1.0.0".to_string(), HashMap::new())
10+
.initialize("module1", "1.0.0", HashMap::new())
1111
.await
1212
.unwrap();
1313
println!("initialized");
1414
module
15-
.declare_function("print_hello".to_string(), |_| {
15+
.declare_function("print_hello", |_| {
1616
println!("Hello");
1717
Value::Null
1818
})
@@ -23,13 +23,13 @@ fn sample() {
2323
}
2424
};
2525
let module2 = async {
26-
let mut module = GothamModule::default(String::from("../gotham.sock"));
26+
let mut module = GothamModule::from_unix_socket("../gotham.sock");
2727
module
28-
.initialize("module2".to_string(), "1.0.0".to_string(), HashMap::new())
28+
.initialize("module2", "1.0.0", HashMap::new())
2929
.await
3030
.unwrap();
3131
module
32-
.call_function("module1.print_hello".to_string(), HashMap::new())
32+
.call_function("module1.print_hello", HashMap::new())
3333
.await
3434
.unwrap();
3535
loop {

0 commit comments

Comments
 (0)