refactor serialization, add json
This commit is contained in:
parent
921b62ed97
commit
aa508a43cb
5 changed files with 155 additions and 69 deletions
57
src/main.rs
57
src/main.rs
|
|
@ -1,14 +1,16 @@
|
|||
use std::{sync::Arc, time::Duration};
|
||||
|
||||
use actix_web::{get, web, App, HttpResponse, HttpServer, Responder};
|
||||
use actix_web::{get, http::header::ContentType, web, App, HttpResponse, HttpServer, Responder};
|
||||
use lazy_static::lazy_static;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
mod extract;
|
||||
mod fetch;
|
||||
mod serialize;
|
||||
|
||||
use extract::DataSet;
|
||||
use fetch::PdfFetcher;
|
||||
use serialize::{Csv, DataSerializer, Json};
|
||||
|
||||
lazy_static! {
|
||||
static ref UPDATE_INTERVAL: Duration = Duration::from_secs(3600);
|
||||
|
|
@ -64,6 +66,7 @@ async fn main() -> std::io::Result<()> {
|
|||
App::new()
|
||||
.app_data(state.clone())
|
||||
.service(csv)
|
||||
.service(json)
|
||||
.service(actix_files::Files::new("/", "./static/").index_file("index.html"))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
|
|
@ -71,48 +74,26 @@ async fn main() -> std::io::Result<()> {
|
|||
.await
|
||||
}
|
||||
|
||||
struct DataIterator {
|
||||
dataset: Arc<DataSet>,
|
||||
index: Option<usize>,
|
||||
}
|
||||
|
||||
impl DataIterator {
|
||||
fn new(dataset: Arc<DataSet>) -> Self {
|
||||
Self {
|
||||
dataset,
|
||||
index: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for DataIterator {
|
||||
type Item = Result<String, std::fmt::Error>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
match self.index {
|
||||
None => {
|
||||
self.index = Some(0);
|
||||
Some(self.dataset.csv_header().map(|s| s + "\n"))
|
||||
}
|
||||
Some(i) => {
|
||||
if let Some(row) = self.dataset.rows.get(i) {
|
||||
self.index = Some(i + 1);
|
||||
Some(self.dataset.csv_row(row).map(|s| s + "\n"))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/data.csv")]
|
||||
async fn csv(data: web::Data<AppState>) -> impl Responder {
|
||||
let dataset = { data.dataset.read().await.clone() };
|
||||
|
||||
let rows =
|
||||
tokio_stream::iter(DataIterator::new(dataset).map(|item| item.map(bytes::Bytes::from)));
|
||||
let rows = tokio_stream::iter(
|
||||
DataSerializer::new(dataset, Csv).map(|item| item.map(bytes::Bytes::from)),
|
||||
);
|
||||
HttpResponse::Ok()
|
||||
.content_type("text/csv; charset=utf-8")
|
||||
.body(actix_web::body::BodyStream::new(rows))
|
||||
}
|
||||
|
||||
#[get("/data.json")]
|
||||
async fn json(data: web::Data<AppState>) -> impl Responder {
|
||||
let dataset = { data.dataset.read().await.clone() };
|
||||
|
||||
let rows = tokio_stream::iter(
|
||||
DataSerializer::new(dataset, Json).map(|item| item.map(bytes::Bytes::from)),
|
||||
);
|
||||
HttpResponse::Ok()
|
||||
.insert_header(ContentType::json())
|
||||
.body(actix_web::body::BodyStream::new(rows))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue