Input parameter detection
This commit is contained in:
+32
-9
@@ -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<dyn std::error::Error>> {
|
||||
|
||||
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<dyn std::error::Error>> {
|
||||
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<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!({
|
||||
"source_file": endpoint.cobol_source
|
||||
"source_file": endpoint.cobol_source,
|
||||
"procedure": endpoint.procedure,
|
||||
"input_arguments": mapped_arguments
|
||||
})))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user