@@ -145,20 +145,31 @@ async fn serve_static(req: actix_web::HttpRequest) -> actix_web::HttpResponse {
145
145
146
146
#[ actix_web:: main]
147
147
pub async fn serve ( port : & str ) -> std:: io:: Result < ( ) > {
148
- // For debugging (will be refactored later)
149
- let use_controller = false ;
148
+ let use_controller = {
149
+ let mut use_controller = false ;
150
+ if let Ok ( val) = std:: env:: var ( "CONTROLLER" ) {
151
+ if let Ok ( val) = val. parse :: < bool > ( ) {
152
+ use_controller = val;
153
+ }
154
+ }
155
+ use_controller
156
+ } ;
150
157
151
158
if use_controller {
152
159
// fpm-controller base path and ec2 instance id (hardcoded for now)
153
- let fpm_controller: String = "https:///controller.fifthtry.com" . to_string ( ) ;
154
- let fpm_instance: String = "<some-ec2-instance-id>" . to_string ( ) ;
160
+ let fpm_controller: String = std:: env:: var ( "FPM_CONTROLLER" )
161
+ . unwrap_or ( "https:///controller.fifthtry.com" . to_string ( ) ) ;
162
+ let fpm_instance: String =
163
+ std:: env:: var ( "FPM_INSTANCE_ID" ) . unwrap_or ( "<instance_id>" . to_string ( ) ) ;
155
164
156
165
match controller:: resolve_dependencies ( fpm_instance, fpm_controller) . await {
157
166
Ok ( _) => println ! ( "Dependencies resolved" ) ,
158
167
Err ( _) => panic ! ( "Error resolving dependencies using controller!!" ) ,
159
168
}
160
169
}
161
170
171
+ let mut config = fpm:: Config :: read ( None ) . await . unwrap ( ) ;
172
+
162
173
println ! ( "### Server Started ###" ) ;
163
174
println ! ( "Go to: http://127.0.0.1:{}" , port) ;
164
175
actix_web:: HttpServer :: new ( || {
@@ -199,38 +210,61 @@ mod controller {
199
210
let package_response = get_package ( fpm_instance. as_str ( ) , fpm_controller. as_str ( ) ) . await ?;
200
211
let gp_status = match package_response[ "success" ] . as_bool ( ) {
201
212
Some ( res) => res,
202
- None => panic ! ( "success parameter doesn't exist in Json or isn't valid boolean type" ) ,
213
+ None => {
214
+ return Err ( fpm:: Error :: UsageError {
215
+ message : "success parameter doesn't exist in Json or isn't valid boolean type"
216
+ . to_string ( ) ,
217
+ } )
218
+ }
203
219
} ;
204
220
205
- match gp_status {
206
- true => {
207
- // package name and git repo url
208
- let package_name = match package_response[ "result" ] [ "package" ] . as_str ( ) {
209
- Some ( valid_name) => valid_name,
210
- None => panic ! ( "received invalid package name from get_package API" ) ,
211
- } ;
212
- if let Some ( git_url) = package_response[ "result" ] [ "git" ] . as_str ( ) {
213
- // Clone the git package into the current directory
214
- // Need to execute shell commands from rust
215
- // git_url https format: https://github.com/<user>/<repo>.git
216
- let out = std:: process:: Command :: new ( "git" )
217
- . arg ( "clone" )
218
- . arg ( git_url)
219
- . output ( )
220
- . expect ( "unable to execute git clone command" ) ;
221
-
222
- if out. status . success ( ) {
223
- // By this time the cloned repo should be available in the current directory
224
- println ! ( "Git cloning successful for the package {}" , package_name) ;
225
- // Resolve dependencies by reading the FPM.ftd using config.read()
226
- // Assuming package_name and repo name are identical
227
- let _config = fpm:: Config :: read ( Some ( package_name. to_string ( ) ) ) . await ?;
228
- }
229
- } else {
230
- panic ! ( "Invalid git url for the package {}" , package_name) ;
231
- }
232
- } ,
233
- false => panic ! ( "get-package api success status returned false!!" )
221
+ if !gp_status {
222
+ return Err ( fpm:: Error :: UsageError {
223
+ message : "get-package api success status returned false!!" . to_string ( ) ,
224
+ } ) ;
225
+ }
226
+
227
+ // package name and git repo url
228
+ let package_name = match package_response[ "result" ] [ "package" ] . as_str ( ) {
229
+ Some ( valid_name) => valid_name,
230
+ None => {
231
+ return Err ( fpm:: Error :: UsageError {
232
+ message : "received invalid package name from get_package API" . to_string ( ) ,
233
+ } )
234
+ }
235
+ } ;
236
+
237
+ if let Some ( git_url) = package_response[ "result" ] [ "git" ] . as_str ( ) {
238
+ // Clone the git package into the current directory
239
+ // Need to execute shell commands from rust
240
+ // git_url https format: https://github.com/<user>/<repo>.git
241
+
242
+ let package = {
243
+ let mut package = fpm:: Package :: new ( package_name) ;
244
+ package. zip = Some ( git_url. to_string ( ) ) ;
245
+ package
246
+ } ;
247
+
248
+ package. unzip_package ( ) . await ?;
249
+ fpm:: Config :: read ( None ) . await ?;
250
+
251
+ /*let out = std::process::Command::new("git")
252
+ .arg("clone")
253
+ .arg(git_url)
254
+ .output()
255
+ .expect("unable to execute git clone command");
256
+
257
+ if out.status.success() {
258
+ // By this time the cloned repo should be available in the current directory
259
+ println!("Git cloning successful for the package {}", package_name);
260
+ // Resolve dependencies by reading the FPM.ftd using config.read()
261
+ // Assuming package_name and repo name are identical
262
+ let _config = fpm::Config::read(Some(package_name.to_string())).await?;
263
+ }*/
264
+ } else {
265
+ return Err ( fpm:: Error :: UsageError {
266
+ message : "received invalid package name from get_package API" . to_string ( ) ,
267
+ } ) ;
234
268
}
235
269
236
270
// Once the dependencies are resolved for the package
@@ -251,72 +285,61 @@ mod controller {
251
285
Ok ( ( ) )
252
286
}
253
287
254
- async fn make_get_request ( url : url:: Url ) -> fpm:: Result < serde_json:: Value > {
255
- use fpm:: library:: http;
256
- let response = match http:: _get ( url) . await {
257
- Ok ( some_response) => some_response,
258
- Err ( e) => {
259
- panic ! ( "failed to fetch data, error: {}" , e. to_string( ) )
260
- }
261
- } ;
262
-
263
- match serde_json:: from_str ( response. as_str ( ) ) {
264
- Ok ( v) => Ok ( v) ,
265
- Err ( e) => panic ! (
266
- "failed parsing json from response text, error: {}" ,
267
- e. to_string( )
268
- ) ,
269
- }
270
- }
271
-
288
+ /// get-package API
289
+ /// input: fpm_instance
290
+ /// output: package_name and git repo URL
291
+ /// format: {
292
+ /// "success": true,
293
+ /// "result": {
294
+ /// "package": "<package name>"
295
+ /// "git": "<git url>"
296
+ /// }
297
+ /// }
272
298
async fn get_package (
273
299
fpm_instance : & str ,
274
300
fpm_controller : & str ,
275
301
) -> fpm:: Result < serde_json:: Value > {
276
- // get-package API
277
- // input: fpm_instance
278
- // output: package_name and git repo URL
279
- // format: {
280
- // "success": true,
281
- // "result": {
282
- // "package": "<package name>"
283
- // "git": "<git url>"
284
- // }
285
- // }
286
-
287
- let query_string = format ! ( "instance={}" , fpm_instance) ;
288
- let controller_api = format ! ( "{}/get-package?{}" , fpm_controller, query_string) ;
289
-
302
+ let controller_api = format ! ( "{}/get-package?instance={}" , fpm_controller, fpm_instance) ;
290
303
let url = match url:: Url :: parse ( controller_api. as_str ( ) ) {
291
304
Ok ( safe_url) => safe_url,
292
- Err ( e) => panic ! ( "Invalid get-package API endpoint, Parse error: {}" , e. to_string( ) ) ,
305
+ Err ( e) => panic ! (
306
+ "Invalid get-package API endpoint, Parse error: {}" ,
307
+ e. to_string( )
308
+ ) ,
293
309
} ;
294
310
295
- make_get_request ( url) . await
311
+ let val = fpm:: library:: http:: get ( url, "" , 0 ) . await ?;
312
+ Ok ( val)
296
313
}
297
314
315
+ /// fpm-ready API
316
+ /// input: fpm_instance, *(git commit hash)
317
+ /// output: success: true/false
318
+ /// format: lang: json
319
+ /// {
320
+ /// "success": true
321
+ /// }
322
+
323
+ /// Git commit hash needs to be computed before making a call to the fpm_ready API
298
324
async fn fpm_ready ( fpm_instance : & str , fpm_controller : & str ) -> fpm:: Result < serde_json:: Value > {
299
- // fpm-ready API
300
- // input: fpm_instance, *(git commit hash)
301
- // output: success: true/false
302
- // format: lang: json
303
- // {
304
- // "success": true
305
- // }
306
-
307
- // Git commit hash needs to be computed before making a call to the fpm_ready API
308
325
let git_commit = "<dummy-git-commit-hash-xxx123>" ;
309
326
310
- let query_string = format ! ( "instance={}&git-commit={}" , fpm_instance, git_commit) ;
311
- let controller_api = format ! ( "{}/fpm-ready?{}" , fpm_controller, query_string) ;
327
+ let controller_api = format ! (
328
+ "{}/fpm-ready?instance={}&git-commit={}" ,
329
+ fpm_controller, fpm_instance, git_commit
330
+ ) ;
312
331
313
332
let url = match url:: Url :: parse ( controller_api. as_str ( ) ) {
314
333
Ok ( safe_url) => safe_url,
315
- Err ( e) => panic ! ( "Invalid fpm_ready API endpoint, Parse error: {}" , e. to_string( ) ) ,
334
+ Err ( e) => panic ! (
335
+ "Invalid fpm_ready API endpoint, Parse error: {}" ,
336
+ e. to_string( )
337
+ ) ,
316
338
} ;
317
339
318
340
// This request should be put request for fpm_ready API to update the instance status to ready
319
341
// Using http::_get() function to make request to this API for now
320
- make_get_request ( url) . await
342
+ let val = fpm:: library:: http:: get ( url, "" , 0 ) . await ?;
343
+ Ok ( val)
321
344
}
322
345
}
0 commit comments