1
1
extern crate async_std;
2
- extern crate clap;
3
2
extern crate futures;
4
3
extern crate futures_util;
5
4
extern crate serde_json;
@@ -19,13 +18,15 @@ use async_std::{
19
18
sync:: { Arc , Mutex } ,
20
19
task,
21
20
} ;
22
- use clap:: { App , Arg } ;
23
21
use futures:: channel:: {
24
22
mpsc:: { UnboundedReceiver , UnboundedSender } ,
25
23
oneshot:: { channel, Sender } ,
26
24
} ;
27
25
use futures_util:: sink:: SinkExt ;
28
- use std:: collections:: HashMap ;
26
+ use std:: {
27
+ collections:: HashMap ,
28
+ net:: { AddrParseError , SocketAddr } ,
29
+ } ;
29
30
30
31
type ArcRequestList = Arc < Mutex < HashMap < String , Sender < Result < Value > > > > > ;
31
32
type ArcFunctionList = Arc < Mutex < HashMap < String , fn ( HashMap < String , Value > ) -> Value > > > ;
@@ -42,90 +43,14 @@ pub struct GothamModule {
42
43
}
43
44
44
45
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 ( ) )
114
52
} 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)
129
54
}
130
55
}
131
56
0 commit comments