diff --git a/server/src/controllers.rs b/server/src/controllers.rs new file mode 100644 index 0000000..565c742 --- /dev/null +++ b/server/src/controllers.rs @@ -0,0 +1,43 @@ +use serde_json::Value; +use std::env::current_dir; +use std::fs::{read_to_string, File}; +use std::path::PathBuf; +use actix_web::{get, http, HttpResponse, Responder}; + +#[get("/")] +pub async fn get_root() -> impl Responder { + HttpResponse::Ok() +} + +#[get("/health")] +pub async fn get_health() -> impl Responder { + HttpResponse::Ok().body("Server running") +} + +#[get("/json")] +pub async fn get_json() -> impl Responder { + let file_path: PathBuf = get_data_file_path("web-diggers-alpha.json"); + let file = File::open(file_path).expect("Failed to open JSON file"); + let json_obj: Value = serde_json::from_reader(file).expect("Failed to parse JSON file"); + + HttpResponse::Ok().json(json_obj) +} + +#[get("/csv")] +pub async fn get_csv() -> impl Responder { + let file_path = get_data_file_path("web-diggers-alpha.csv"); + let file = read_to_string(file_path).expect("Failed to read CSV file"); + + HttpResponse::Ok() + .append_header(http::header::ContentType(mime::TEXT_CSV)) + .body(file) +} + +fn get_data_file_path(filename: &str) -> PathBuf { + current_dir() + .expect("Error while listing current directory") + .parent() + .expect("Error while navigating to the parent") + .join("data") + .join(filename) +} \ No newline at end of file diff --git a/server/src/main.rs b/server/src/main.rs index 193ad31..480bf75 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,38 +1,7 @@ -use actix_cors::Cors; -use actix_web::{get, http, middleware, App, HttpResponse, HttpServer, Responder}; -use serde_json::Value; -use std::env::current_dir; -use std::fs::{read_to_string, File}; -use std::path::PathBuf; - -#[get("/")] -async fn get_root() -> impl Responder { - HttpResponse::Ok() -} +mod controllers; -#[get("/health")] -async fn get_health() -> impl Responder { - HttpResponse::Ok().body("Server running") -} - -#[get("/json")] -async fn get_json() -> impl Responder { - let file_path: PathBuf = get_data_file_path("web-diggers-alpha.json"); - let file = File::open(file_path).expect("Failed to open JSON file"); - let json_obj: Value = serde_json::from_reader(file).expect("Failed to parse JSON file"); - - HttpResponse::Ok().json(json_obj) -} - -#[get("/csv")] -async fn get_csv() -> impl Responder { - let file_path = get_data_file_path("web-diggers-alpha.csv"); - let file = read_to_string(file_path).expect("Failed to read CSV file"); - - HttpResponse::Ok() - .append_header(http::header::ContentType(mime::TEXT_CSV)) - .body(file) -} +use actix_cors::Cors; +use actix_web::{http, middleware, App, HttpServer}; #[actix_web::main] async fn main() -> std::io::Result<()> { @@ -47,21 +16,12 @@ async fn main() -> std::io::Result<()> { App::new() .wrap(middleware::Logger::default()) .wrap(cors) - .service(get_root) - .service(get_health) - .service(get_json) - .service(get_csv) + .service(controllers::get_root) + .service(controllers::get_health) + .service(controllers::get_json) + .service(controllers::get_csv) }) .bind(("127.0.0.1", 8080))? .run() .await } - -fn get_data_file_path(filename: &str) -> PathBuf { - current_dir() - .expect("Error while listing current directory") - .parent() - .expect("Error while navigating to the parent") - .join("data") - .join(filename) -}