1
+ extern crate async_std;
2
+ extern crate clap;
3
+ extern crate futures;
4
+ extern crate futures_util;
5
+ extern crate serde_json;
6
+
1
7
use crate :: {
2
8
connection:: { BaseConnection , Buffer , InetSocketConnection } ,
3
9
models:: { BaseMessage , Value } ,
@@ -8,17 +14,18 @@ use crate::{
8
14
#[ cfg( target_family = "unix" ) ]
9
15
use crate :: connection:: UnixSocketConnection ;
10
16
11
- use std:: collections:: HashMap ;
12
17
use async_std:: {
13
18
prelude:: * ,
14
19
sync:: { Arc , Mutex } ,
15
20
task,
16
21
} ;
22
+ use clap:: { App , Arg } ;
17
23
use futures:: channel:: {
18
24
mpsc:: { UnboundedReceiver , UnboundedSender } ,
19
25
oneshot:: { channel, Sender } ,
20
26
} ;
21
27
use futures_util:: sink:: SinkExt ;
28
+ use std:: collections:: HashMap ;
22
29
23
30
type ArcRequestList = Arc < Mutex < HashMap < String , Sender < Result < Value > > > > > ;
24
31
type ArcFunctionList = Arc < Mutex < HashMap < String , fn ( HashMap < String , Value > ) -> Value > > > ;
@@ -35,24 +42,103 @@ pub struct GothamModule {
35
42
}
36
43
37
44
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
+ }
48
129
}
49
130
}
50
131
51
132
#[ 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 {
53
139
GothamModule {
54
140
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 ( ) ) ) ,
56
142
requests : Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ,
57
143
functions : Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ,
58
144
hook_listeners : Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ,
@@ -61,10 +147,10 @@ impl GothamModule {
61
147
}
62
148
}
63
149
64
- pub fn with_inet_socket ( socket : String ) -> Self {
150
+ pub fn from_inet_socket ( host : & str , port : u16 ) -> Self {
65
151
GothamModule {
66
152
protocol : BaseProtocol :: default ( ) ,
67
- connection : Box :: new ( InetSocketConnection :: new ( socket ) ) ,
153
+ connection : Box :: new ( InetSocketConnection :: new ( format ! ( "{}:{}" , host , port ) ) ) ,
68
154
requests : Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ,
69
155
functions : Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ,
70
156
hook_listeners : Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ,
@@ -87,13 +173,15 @@ impl GothamModule {
87
173
88
174
pub async fn initialize (
89
175
& mut self ,
90
- module_id : String ,
91
- version : String ,
176
+ module_id : & str ,
177
+ version : & str ,
92
178
dependencies : HashMap < String , String > ,
93
179
) -> Result < ( ) > {
94
180
self . setup_connections ( ) . await ?;
95
181
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) ;
97
185
self . send_request ( request) . await ?;
98
186
99
187
self . registered = true ;
@@ -102,9 +190,10 @@ impl GothamModule {
102
190
103
191
pub async fn declare_function (
104
192
& mut self ,
105
- fn_name : String ,
193
+ fn_name : & str ,
106
194
function : fn ( HashMap < String , Value > ) -> Value ,
107
195
) -> Result < ( ) > {
196
+ let fn_name = fn_name. to_string ( ) ;
108
197
self . functions
109
198
. lock ( )
110
199
. await
@@ -117,15 +206,17 @@ impl GothamModule {
117
206
118
207
pub async fn call_function (
119
208
& mut self ,
120
- fn_name : String ,
209
+ fn_name : & str ,
121
210
args : HashMap < String , Value > ,
122
211
) -> Result < Value > {
212
+ let fn_name = fn_name. to_string ( ) ;
123
213
self . ensure_registered ( ) ?;
124
214
let request = self . protocol . call_function ( fn_name, args) ;
125
215
self . send_request ( request) . await
126
216
}
127
217
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 ( ) ;
129
220
self . ensure_registered ( ) ?;
130
221
let mut hook_listeners = self . hook_listeners . lock ( ) . await ;
131
222
if hook_listeners. contains_key ( & hook) {
@@ -140,7 +231,8 @@ impl GothamModule {
140
231
Ok ( ( ) )
141
232
}
142
233
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 ( ) ;
144
236
let request = self . protocol . trigger_hook ( hook) ;
145
237
self . send_request ( request) . await ?;
146
238
Ok ( ( ) )
0 commit comments