2021-09-18 17:55:39 +00:00
|
|
|
use crate::{
|
|
|
|
config::Config,
|
|
|
|
data::State,
|
|
|
|
error::{Error, ErrorKind},
|
|
|
|
};
|
2020-03-23 17:38:39 +00:00
|
|
|
use actix_web::{web, HttpResponse};
|
2020-03-26 16:17:59 +00:00
|
|
|
use rand::{seq::SliceRandom, thread_rng};
|
2020-03-23 17:38:39 +00:00
|
|
|
use std::io::BufWriter;
|
2021-09-18 17:55:39 +00:00
|
|
|
use tracing::error;
|
2020-03-23 17:38:39 +00:00
|
|
|
|
2021-09-18 17:55:39 +00:00
|
|
|
#[tracing::instrument(name = "Index")]
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) async fn route(
|
2020-03-23 17:38:39 +00:00
|
|
|
state: web::Data<State>,
|
|
|
|
config: web::Data<Config>,
|
2021-09-18 17:55:39 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2021-02-10 04:05:06 +00:00
|
|
|
let mut nodes = state.node_cache().nodes().await?;
|
2020-03-26 16:17:59 +00:00
|
|
|
nodes.shuffle(&mut thread_rng());
|
2020-03-23 17:38:39 +00:00
|
|
|
let mut buf = BufWriter::new(Vec::new());
|
|
|
|
|
|
|
|
crate::templates::index(&mut buf, &nodes, &config)?;
|
|
|
|
let buf = buf.into_inner().map_err(|e| {
|
|
|
|
error!("Error rendering template, {}", e.error());
|
2021-09-18 17:55:39 +00:00
|
|
|
ErrorKind::FlushBuffer
|
2020-03-23 17:38:39 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().content_type("text/html").body(buf))
|
|
|
|
}
|