diff --git a/api.yaml b/api.yaml index 636c202..dfc78df 100644 --- a/api.yaml +++ b/api.yaml @@ -2,7 +2,7 @@ version: "1.0" project: "legacy-student-api" endpoints: - - path: "/students/{id}" + - path: "/students/:id" method: "GET" cobol_source: "src/fetch_student.cbl" procedure: "FETCH-STUDENT" diff --git a/code/src/main.rs b/code/src/main.rs index 24fa381..f3bd259 100644 --- a/code/src/main.rs +++ b/code/src/main.rs @@ -1,15 +1,17 @@ use axum::{ + extract::Path as AxumPath, http::StatusCode, routing::get, Extension, Json, Router, }; use serde::{Deserialize, Serialize}; +use std::collections::HashMap; use std::fs::File; use std::io::Read; use std::net::SocketAddr; -use std::sync::Arc; use std::path::Path; use std::process::Command; +use std::sync::Arc; #[derive(Debug, Serialize, Deserialize)] struct ApiConfig { @@ -50,12 +52,11 @@ async fn main() -> Result<(), Box> { let cobol_path = Path::new(&endpoint.cobol_source); let working_dir = cobol_path.parent().unwrap_or_else(|| Path::new(".")); - let file_name = cobol_path.file_name() - .ok_or("Invalid COBOL filename")?; - let library_name = Path::new(file_name) - .with_extension("so"); + let file_name = cobol_path.file_name().ok_or("Invalid COBOL filename")?; + let library_name = Path::new(file_name).with_extension("so"); let file_name_str = file_name.to_str().ok_or("Filename conversion error")?; let library_name_str = library_name.to_str().ok_or("Library name conversion error")?; + let output = Command::new("cobc") .current_dir(working_dir) .args(["-m", "-O", "-o", library_name_str, file_name_str]) @@ -65,7 +66,7 @@ async fn main() -> Result<(), Box> { eprintln!("COBOL Compilation failed:\n{}", String::from_utf8_lossy(&output.stderr)); std::process::exit(1); } - println!("Compilation successful: {} generated.", endpoint.cobol_source.replace(".cbl", ".so").as_str()); + println!("Compilation successful: {} generated.", library_name_str); let path = endpoint.path.clone(); @@ -91,10 +92,32 @@ async fn project_handler() -> Result, (StatusCode, Strin }))) } -async fn cobol_handler(Extension(endpoint): Extension>) -> Result, (StatusCode, String)> { - +async fn cobol_handler( + Extension(endpoint): Extension>, + AxumPath(params): AxumPath>, +) -> Result, (StatusCode, String)> { + let mut mapped_arguments = serde_json::Map::new(); + + for arg in &endpoint.arguments { + if arg.source == "path" { + if let Some(value) = params.get(&arg.name) { + mapped_arguments.insert( + arg.target.clone(), + serde_json::Value::String(value.clone()) + ); + } else { + return Err(( + StatusCode::BAD_REQUEST, + format!("Missing required path parameter: {}", arg.name) + )); + } + } + } + Ok(Json(serde_json::json!({ - "source_file": endpoint.cobol_source + "source_file": endpoint.cobol_source, + "procedure": endpoint.procedure, + "input_arguments": mapped_arguments }))) }