Input parameter detection
This commit is contained in:
@@ -2,7 +2,7 @@ version: "1.0"
|
|||||||
project: "legacy-student-api"
|
project: "legacy-student-api"
|
||||||
|
|
||||||
endpoints:
|
endpoints:
|
||||||
- path: "/students/{id}"
|
- path: "/students/:id"
|
||||||
method: "GET"
|
method: "GET"
|
||||||
cobol_source: "src/fetch_student.cbl"
|
cobol_source: "src/fetch_student.cbl"
|
||||||
procedure: "FETCH-STUDENT"
|
procedure: "FETCH-STUDENT"
|
||||||
|
|||||||
+32
-9
@@ -1,15 +1,17 @@
|
|||||||
use axum::{
|
use axum::{
|
||||||
|
extract::Path as AxumPath,
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
routing::get,
|
routing::get,
|
||||||
Extension, Json, Router,
|
Extension, Json, Router,
|
||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::Arc;
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
struct ApiConfig {
|
struct ApiConfig {
|
||||||
@@ -50,12 +52,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
|
|
||||||
let cobol_path = Path::new(&endpoint.cobol_source);
|
let cobol_path = Path::new(&endpoint.cobol_source);
|
||||||
let working_dir = cobol_path.parent().unwrap_or_else(|| Path::new("."));
|
let working_dir = cobol_path.parent().unwrap_or_else(|| Path::new("."));
|
||||||
let file_name = cobol_path.file_name()
|
let file_name = cobol_path.file_name().ok_or("Invalid COBOL filename")?;
|
||||||
.ok_or("Invalid COBOL filename")?;
|
let library_name = Path::new(file_name).with_extension("so");
|
||||||
let library_name = Path::new(file_name)
|
|
||||||
.with_extension("so");
|
|
||||||
let file_name_str = file_name.to_str().ok_or("Filename conversion error")?;
|
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 library_name_str = library_name.to_str().ok_or("Library name conversion error")?;
|
||||||
|
|
||||||
let output = Command::new("cobc")
|
let output = Command::new("cobc")
|
||||||
.current_dir(working_dir)
|
.current_dir(working_dir)
|
||||||
.args(["-m", "-O", "-o", library_name_str, file_name_str])
|
.args(["-m", "-O", "-o", library_name_str, file_name_str])
|
||||||
@@ -65,7 +66,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
eprintln!("COBOL Compilation failed:\n{}", String::from_utf8_lossy(&output.stderr));
|
eprintln!("COBOL Compilation failed:\n{}", String::from_utf8_lossy(&output.stderr));
|
||||||
std::process::exit(1);
|
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();
|
let path = endpoint.path.clone();
|
||||||
|
|
||||||
@@ -91,10 +92,32 @@ async fn project_handler() -> Result<Json<serde_json::Value>, (StatusCode, Strin
|
|||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn cobol_handler(Extension(endpoint): Extension<Arc<Endpoint>>) -> Result<Json<serde_json::Value>, (StatusCode, String)> {
|
async fn cobol_handler(
|
||||||
|
Extension(endpoint): Extension<Arc<Endpoint>>,
|
||||||
|
AxumPath(params): AxumPath<HashMap<String, String>>,
|
||||||
|
) -> Result<Json<serde_json::Value>, (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!({
|
Ok(Json(serde_json::json!({
|
||||||
"source_file": endpoint.cobol_source
|
"source_file": endpoint.cobol_source,
|
||||||
|
"procedure": endpoint.procedure,
|
||||||
|
"input_arguments": mapped_arguments
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user