Converted project to Rust
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
use axum::{
|
||||
routing::get,
|
||||
Json, Router,
|
||||
http::StatusCode,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::net::SocketAddr;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct ApiConfig {
|
||||
version: String,
|
||||
project: String,
|
||||
endpoints: Vec<Endpoint>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct Endpoint {
|
||||
path: String,
|
||||
method: String,
|
||||
cobol_source: String,
|
||||
procedure: String,
|
||||
arguments: Vec<Argument>,
|
||||
response_copybook: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct Argument {
|
||||
name: String,
|
||||
target: String,
|
||||
#[serde(rename = "type")]
|
||||
arg_type: String,
|
||||
source: String,
|
||||
}
|
||||
|
||||
#[repr(C, packed)]
|
||||
struct Student {
|
||||
st_id: [u8; 5],
|
||||
first_name: [u8; 20],
|
||||
last_name: [u8; 20],
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
eprintln!("Starting RestCOBOL Rust server...");
|
||||
let app = Router::new()
|
||||
.route("/project", get(project_handler));
|
||||
|
||||
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
|
||||
eprintln!("Binding to {}...", addr);
|
||||
let listener = match tokio::net::TcpListener::bind(addr).await {
|
||||
Ok(l) => l,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to bind to {}: {}", addr, e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
eprintln!("Server starting on port 8080...");
|
||||
if let Err(e) = axum::serve(listener, app).await {
|
||||
eprintln!("Server error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
async fn project_handler() -> Result<Json<serde_json::Value>, (StatusCode, String)> {
|
||||
let config = read_conf("api.yaml").map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||
|
||||
Ok(Json(serde_json::json!({
|
||||
"project": config.project
|
||||
})))
|
||||
}
|
||||
|
||||
fn read_conf(filename: &str) -> Result<ApiConfig, Box<dyn std::error::Error>> {
|
||||
let mut file = File::open(filename)?;
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents)?;
|
||||
let config: ApiConfig = serde_yaml::from_str(&contents)?;
|
||||
Ok(config)
|
||||
}
|
||||
Reference in New Issue
Block a user