Skip to content

Commit

Permalink
Move controllers into a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikStefancik committed Oct 29, 2023
1 parent 0bd5ac1 commit 3218072
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 47 deletions.
43 changes: 43 additions & 0 deletions 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)
}
54 changes: 7 additions & 47 deletions 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<()> {
Expand All @@ -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)
}

0 comments on commit 3218072

Please sign in to comment.