Skip to content

Commit

Permalink
Update route configuration to support new external path and enhance m…
Browse files Browse the repository at this point in the history
…ethod handling
  • Loading branch information
DanielSarmiento04 committed Dec 29, 2024
1 parent 1cd6e17 commit a0e3d1b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 40 deletions.
6 changes: 3 additions & 3 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ routes:
external_path: /identity/register
internal_path: /api/identity/register
methods:
- POST
- GET
- POST

- google:
domain: google.com
port: 443
protocol: https
external_path: /identity/register
internal_path: /api/identity/register
external_path: /identity/register/v2
internal_path: /api/identity/register/v2
methods:
- POST
- GET
83 changes: 46 additions & 37 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ async fn hello_world() -> &'static str {
"Hello, world!"
}

async fn internal_function () -> &'static str {
async fn internal_function (internal_url: String) -> &'static str {
print!("Internal URL: {}", internal_url);
"Internal function"
}

Expand All @@ -34,34 +35,61 @@ fn configure_route(cfg: &mut web::ServiceConfig, routes: &[Route])
&route.internal_path,
);

let methods = route.methods.clone();
for method in methods {
for method in &route.methods {

println!("Method: {} formate_url {} ", method, formatted_route);

// handle the method use internal function and pass formatted_route
match method.as_str() {
"GET" => {
cfg.route(&route.external_path, web::get().to(hello_world));
},
cfg.app_data(web::Data::new(formatted_route.clone()))
.service(
web::resource(&route.external_path)
.route(web::get().to(internal_function))
);
}
"POST" => {
cfg.route(&route.external_path, web::post().to(hello_world));
},
cfg.app_data(web::Data::new(formatted_route.clone()))
.service(
web::resource(&route.external_path)
.route(web::post().to(internal_function))
);
}
"PUT" => {
cfg.route(&route.external_path, web::put().to(hello_world));
},
cfg.service(
web::resource(&route.external_path)
.route(web::put().to(hello_world))
);
}
"DELETE" => {
cfg.route(&route.external_path, web::delete().to(hello_world));
},
cfg.service(
web::resource(&route.external_path)
.route(web::delete().to(hello_world))
);
}
"PATCH" => {
cfg.route(&route.external_path, web::patch().to(hello_world));
},
cfg.service(
web::resource(&route.external_path)
.route(web::patch().to(hello_world))
);
}
"HEAD" => {
cfg.route(&route.external_path, web::head().to(hello_world));
},
cfg.service(
web::resource(&route.external_path)
.route(web::head().to(hello_world))
);
}
"TRACE" => {
cfg.route(&route.external_path, web::trace().to(hello_world));
},
cfg.service(
web::resource(&route.external_path)
.route(web::trace().to(hello_world))
);
}
_ => {
cfg.route(&route.external_path, web::get().to(hello_world));
cfg.service(
web::resource(&route.external_path)
.route(web::get().to(hello_world))
);
}
}
}
Expand All @@ -80,25 +108,6 @@ async fn main() -> std::io::Result<()>{
println!("Version: {}", config.version);

let routes = config.routes;
for route in &routes {

println!("Domain: {}", route.domain);
println!("Port: {}", route.port);
println!("Protocol: {}", route.protocol);
println!("External Path: {}", route.external_path);
println!("Internal Path: {}", route.internal_path);
println!("Methods: {:?}", route.methods);

let formatted_route = format_route(
&route.domain,
route.port,
&route.protocol,
&route.internal_path,
);
println!("Route formate {}", formatted_route);

println!();
}

let shared_routes = Arc::new(routes);

Expand Down

0 comments on commit a0e3d1b

Please sign in to comment.