From 74b5de9193862271ed15d96de514446922b8b17f Mon Sep 17 00:00:00 2001 From: Simon Oberzier Date: Thu, 11 Jun 2026 11:12:09 +0200 Subject: [PATCH] Added endpoint detection from yaml file --- code/src/main.rs | 51 ++++++++++++++++++++++++++++++------------------ justfile | 25 +++++++++++++++++++++--- 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/code/src/main.rs b/code/src/main.rs index 0d4aec8..69998ce 100644 --- a/code/src/main.rs +++ b/code/src/main.rs @@ -1,12 +1,13 @@ use axum::{ - routing::get, - Json, Router, http::StatusCode, + routing::get, + Extension, Json, Router, }; use serde::{Deserialize, Serialize}; use std::fs::File; use std::io::Read; use std::net::SocketAddr; +use std::sync::Arc; #[derive(Debug, Serialize, Deserialize)] struct ApiConfig { @@ -42,25 +43,30 @@ struct Student { } #[tokio::main] -async fn main() { - eprintln!("Starting RestCOBOL Rust server..."); - let app = Router::new() - .route("/project", get(project_handler)); +async fn main() -> Result<(), Box> { + let config = read_conf("api.yaml")?; + + println!("Starting RestCOBOL Rust server..."); + let mut app = Router::new().route("/project", get(project_handler)); + + println!("Registering project routes..."); + for endpoint in config.endpoints.into_iter() { + println!("Binding route {}", endpoint.path); + + let path = endpoint.path.clone(); + + app = app.route(&path, get(cobol_handler).layer(Extension(Arc::new(endpoint)))); + } 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); - } + println!("Binding to {}...", addr); + + let listener = tokio::net::TcpListener::bind(addr).await?; + + println!("Server starting on port 8080..."); + axum::serve(listener, app).await?; + + Ok(()) } async fn project_handler() -> Result, (StatusCode, String)> { @@ -71,6 +77,13 @@ async fn project_handler() -> Result, (StatusCode, Strin }))) } +async fn cobol_handler(Extension(endpoint): Extension>) -> Result, (StatusCode, String)> { + + Ok(Json(serde_json::json!({ + "source_file": endpoint.cobol_source + }))) +} + fn read_conf(filename: &str) -> Result> { let mut file = File::open(filename)?; let mut contents = String::new(); diff --git a/justfile b/justfile index 12720b5..7deedc4 100644 --- a/justfile +++ b/justfile @@ -1,7 +1,26 @@ +# Variables +container_name := "rest-cobol-app" +image_name := "rest-cobol" + # Build the docker image build: - docker build -t rest-cobol . + docker build -t {{image_name}} . -# Run the image +# Run the image in the foreground run: build - docker run -p 8080:8080 rest-cobol \ No newline at end of file + docker run --name {{container_name}} -p 8080:8080 {{image_name}} + +# Run the image in the background (detached) +run-d: build + docker run -d --name {{container_name}} -p 8080:8080 {{image_name}} + +# Stop the running container +stop: + docker stop {{container_name}} + +# Remove the container +rm: + docker rm {{container_name}} + +# Stop and remove the container in one command +clean: stop rm \ No newline at end of file