improve dashboard startup

This commit is contained in:
nora 2022-03-07 15:24:46 +01:00
parent 08fa9163b8
commit 2fe3b4b77b
3 changed files with 27 additions and 19 deletions

View file

@ -41,15 +41,17 @@ pub struct StaticFileService {
} }
impl StaticFileService { impl StaticFileService {
pub fn new(zip: &[u8]) -> Self { /// Creates a new static file service from zip data. This is a blocking operation!
let mut archive = ZipArchive::new(Cursor::new(zip)).unwrap(); #[tracing::instrument(skip(zip))]
pub fn new(zip: &[u8]) -> anyhow::Result<Self> {
let mut archive = ZipArchive::new(Cursor::new(zip))?;
let mut files = HashMap::with_capacity(archive.len()); let mut files = HashMap::with_capacity(archive.len());
for i in 0..archive.len() { for i in 0..archive.len() {
let mut file = archive.by_index(i).unwrap(); let mut file = archive.by_index(i)?;
let mut data = Vec::with_capacity(usize::try_from(file.size()).unwrap()); let mut data = Vec::with_capacity(usize::try_from(file.size())?);
std::io::copy(&mut file, &mut data).unwrap(); std::io::copy(&mut file, &mut data)?;
trace!(name = %file.name(), size = %file.size(),"Unpacking dashboard frontend file"); trace!(name = %file.name(), size = %file.size(),"Unpacking dashboard frontend file");
@ -75,7 +77,7 @@ impl StaticFileService {
trace!(?files, "Created StaticFileService"); trace!(?files, "Created StaticFileService");
Self { files } Ok(Self { files })
} }
fn call_inner(&mut self, req: Request<Body>) -> Result<Response<Body>, anyhow::Error> { fn call_inner(&mut self, req: Request<Body>) -> Result<Response<Body>, anyhow::Error> {

View file

@ -16,13 +16,23 @@ use tracing::{error, info};
const DATA_ZIP: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/frontend.zip")); const DATA_ZIP: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/frontend.zip"));
pub async fn dashboard(global_data: GlobalData) { pub async fn start_dashboard(global_data: GlobalData) {
match dashboard(global_data).await {
Ok(()) => {}
Err(err) => error!(%err, "Failed to start dashboard"),
}
}
#[tracing::instrument(skip(global_data))]
pub async fn dashboard(global_data: GlobalData) -> anyhow::Result<()> {
let cors = CorsLayer::new() let cors = CorsLayer::new()
.allow_methods(vec![Method::GET]) .allow_methods(vec![Method::GET])
.allow_origin(Any); .allow_origin(Any);
let static_file_service = let static_file_service =
get_service(StaticFileService::new(DATA_ZIP)).handle_error(|error| async move { tokio::task::spawn_blocking(|| StaticFileService::new(DATA_ZIP)).await??;
let static_file_service = get_service(static_file_service).handle_error(|error| async move {
error!(?error, "Error in static file service"); error!(?error, "Error in static file service");
StatusCode::INTERNAL_SERVER_ERROR StatusCode::INTERNAL_SERVER_ERROR
}); });
@ -37,8 +47,8 @@ pub async fn dashboard(global_data: GlobalData) {
axum::Server::bind(&socket_addr) axum::Server::bind(&socket_addr)
.serve(app.into_make_service()) .serve(app.into_make_service())
.await .await?;
.unwrap(); Ok(())
} }
#[derive(Serialize)] #[derive(Serialize)]

View file

@ -3,7 +3,7 @@
use anyhow::Result; use anyhow::Result;
use clap::Parser; use clap::Parser;
use std::str::FromStr; use std::str::FromStr;
use tracing::{info, info_span, Instrument}; use tracing::info;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};
/// An AMQP 0-9-1 broker implementation. /// An AMQP 0-9-1 broker implementation.
@ -28,11 +28,7 @@ async fn main() -> Result<()> {
if args.dashboard { if args.dashboard {
let global_data = global_data.clone(); let global_data = global_data.clone();
tokio::spawn(async move { tokio::spawn(async move { amqp_dashboard::start_dashboard(global_data).await });
amqp_dashboard::dashboard(global_data)
.instrument(info_span!("dashboard"))
.await
});
} }
let res = amqp_transport::do_thing_i_guess(global_data, terminate()).await; let res = amqp_transport::do_thing_i_guess(global_data, terminate()).await;