Skip to content

Commit

Permalink
Simplify reading and sending csv
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikStefancik committed Oct 29, 2023
1 parent 68d7e68 commit 0bd5ac1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 33 deletions.
23 changes: 1 addition & 22 deletions server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/Cargo.toml
Expand Up @@ -8,5 +8,5 @@ edition = "2021"
[dependencies]
actix-cors = "0.6.4"
actix-web = "4.4.0"
csv = "1.3.0"
mime = "0.3.17"
serde_json = "1.0.107"
15 changes: 5 additions & 10 deletions server/src/main.rs
Expand Up @@ -2,9 +2,8 @@ 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::File;
use std::fs::{read_to_string, File};
use std::path::PathBuf;
use csv::Reader;

#[get("/")]
async fn get_root() -> impl Responder {
Expand All @@ -28,15 +27,11 @@ async fn get_json() -> impl Responder {
#[get("/csv")]
async fn get_csv() -> impl Responder {
let file_path = get_data_file_path("web-diggers-alpha.csv");
let mut reader = Reader::from_path(file_path).expect("Failed to read CSV file");
let file = read_to_string(file_path).expect("Failed to read CSV file");

for string_record in reader.records() {
let record = string_record.expect("Failed to read CSV file");
println!("Record {:?}", record);
}

HttpResponse::Ok().body("CSV TEST")
// HttpResponse::Ok().body(reader.records().collect::<Vec<_>>().as_slice())
HttpResponse::Ok()
.append_header(http::header::ContentType(mime::TEXT_CSV))
.body(file)
}

#[actix_web::main]
Expand Down

0 comments on commit 0bd5ac1

Please sign in to comment.