Skip to content

Commit 673b648

Browse files
committed
Added default config that takes a string and parses accordingly.
1 parent 926e309 commit 673b648

File tree

5 files changed

+18
-100
lines changed

5 files changed

+18
-100
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ serde_json = "*"
1414
async-trait = "0.1.24"
1515
futures = "0.3.4"
1616
futures-util = "0.3.4"
17-
clap = "2.33.0"
1817

1918
[profile.release]
2019
lto = true

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ use std::{time::Duration, collections::HashMap};
1919

2020
#[async_std::main]
2121
async fn main() {
22-
let mut module = GothamModule::default(String::from("../path/to/gotham.sock"));
22+
let mut module = GothamModule::default("./path/to/gotham.sock");
23+
// The hashmap below is used to mark dependencies
2324
module
24-
.initialize("module-name".to_string(), "1.0.0".to_string(), HashMap::new())
25+
.initialize("module-name", "1.0.0", HashMap::new())
2526
.await
2627
.unwrap();
27-
// The hashmap is used to mark dependencies
2828
println!("Initialized!");
2929
module
30-
.declare_function("print_hello".to_string(), |args| {
30+
.declare_function("print_hello", |args| {
3131
println!("Hello");
3232
Value::Null
3333
})
3434
.await
3535
.unwrap();
36+
// The HashMap::new() below marks the arguments passed to the function
3637
module
37-
.call_function("module2.print_hello_world".to_string(), HashMap::new())
38+
.call_function("module2.print_hello_world", HashMap::new())
3839
.await
3940
.unwrap();
40-
// The HashMap::new() here marks the arguments passed to the function
4141
loop {
4242
task::sleep(Duration::from_millis(1000)).await;
4343
}

src/gotham_module.rs

Lines changed: 11 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
extern crate async_std;
2-
extern crate clap;
32
extern crate futures;
43
extern crate futures_util;
54
extern crate serde_json;
@@ -19,13 +18,15 @@ use async_std::{
1918
sync::{Arc, Mutex},
2019
task,
2120
};
22-
use clap::{App, Arg};
2321
use futures::channel::{
2422
mpsc::{UnboundedReceiver, UnboundedSender},
2523
oneshot::{channel, Sender},
2624
};
2725
use futures_util::sink::SinkExt;
28-
use std::collections::HashMap;
26+
use std::{
27+
collections::HashMap,
28+
net::{AddrParseError, SocketAddr},
29+
};
2930

3031
type ArcRequestList = Arc<Mutex<HashMap<String, Sender<Result<Value>>>>>;
3132
type ArcFunctionList = Arc<Mutex<HashMap<String, fn(HashMap<String, Value>) -> Value>>>;
@@ -42,90 +43,14 @@ pub struct GothamModule {
4243
}
4344

4445
impl GothamModule {
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-
}
46+
pub fn default(connection_path: &str) -> Self {
47+
let is_ip: std::result::Result<SocketAddr, AddrParseError> =
48+
connection_path.to_string().parse();
49+
if is_ip.is_ok() {
50+
let ip = is_ip.unwrap();
51+
Self::from_inet_socket(&format!("{}", ip.ip()), ip.port())
11452
} 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-
}
53+
Self::from_unix_socket(connection_path)
12954
}
13055
}
13156

src/utils/constants.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
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-
71
pub mod request_keys {
82
pub const TYPE: &str = "type";
93
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, APP_AUTHORS, APP_NAME, APP_VERSION};
4+
pub use constants::{errors, request_keys, request_types};
55
pub use error::{Error, Result};

0 commit comments

Comments
 (0)