-
Notifications
You must be signed in to change notification settings - Fork 1
/
controllers.rs
43 lines (36 loc) · 1.23 KB
/
controllers.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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)
}