Added endpoint detection from yaml file

This commit is contained in:
2026-06-11 11:12:09 +02:00
parent 9f7410b7c7
commit 74b5de9193
2 changed files with 54 additions and 22 deletions
+32 -19
View File
@@ -1,12 +1,13 @@
use axum::{ use axum::{
routing::get,
Json, Router,
http::StatusCode, http::StatusCode,
routing::get,
Extension, Json, Router,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
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;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct ApiConfig { struct ApiConfig {
@@ -42,25 +43,30 @@ struct Student {
} }
#[tokio::main] #[tokio::main]
async fn main() { async fn main() -> Result<(), Box<dyn std::error::Error>> {
eprintln!("Starting RestCOBOL Rust server..."); let config = read_conf("api.yaml")?;
let app = Router::new()
.route("/project", get(project_handler)); 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)); let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
eprintln!("Binding to {}...", addr); println!("Binding to {}...", addr);
let listener = match tokio::net::TcpListener::bind(addr).await {
Ok(l) => l, let listener = tokio::net::TcpListener::bind(addr).await?;
Err(e) => {
eprintln!("Failed to bind to {}: {}", addr, e); println!("Server starting on port 8080...");
std::process::exit(1); axum::serve(listener, app).await?;
}
}; Ok(())
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)> { async fn project_handler() -> Result<Json<serde_json::Value>, (StatusCode, String)> {
@@ -71,6 +77,13 @@ 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)> {
Ok(Json(serde_json::json!({
"source_file": endpoint.cobol_source
})))
}
fn read_conf(filename: &str) -> Result<ApiConfig, Box<dyn std::error::Error>> { fn read_conf(filename: &str) -> Result<ApiConfig, Box<dyn std::error::Error>> {
let mut file = File::open(filename)?; let mut file = File::open(filename)?;
let mut contents = String::new(); let mut contents = String::new();
+22 -3
View File
@@ -1,7 +1,26 @@
# Variables
container_name := "rest-cobol-app"
image_name := "rest-cobol"
# Build the docker image # Build the docker image
build: build:
docker build -t rest-cobol . docker build -t {{image_name}} .
# Run the image # Run the image in the foreground
run: build run: build
docker run -p 8080:8080 rest-cobol 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