Added endpoint detection from yaml file
This commit is contained in:
+32
-19
@@ -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<dyn std::error::Error>> {
|
||||
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<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>> {
|
||||
let mut file = File::open(filename)?;
|
||||
let mut contents = String::new();
|
||||
|
||||
Reference in New Issue
Block a user