use async_compression::tokio::bufread::GzipDecoder; use axum::{ extract::BodyStream, http::StatusCode, routing::{get_service, post}, Router, }; use hyper::{Body, Request}; use std::net::SocketAddr; use tar::Archive; use tower_http::{auth::RequireAuthorizationLayer, services::ServeDir, trace::TraceLayer}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; #[tokio::main] async fn main() { // Enable tracing tracing_subscriber::registry() .with(tracing_subscriber::EnvFilter::new( std::env::var("RUST_LOG") .unwrap_or_else(|_| "site_backend=debug,tower_http=debug".into()), )) .with(tracing_subscriber::fmt::layer()) .init(); // Get required variables from env vars let api_key = std::env::var("API_KEY").expect("No API_KEY was provided."); let data_dir = std::env::var("DATA_DIR").expect("No DATA_DIR was provided."); std::fs::create_dir_all(&data_dir).unwrap(); let app = Router::new() .route( "/api/deploy", post(post_deploy).layer(RequireAuthorizationLayer::bearer(&api_key)), ) // The fallback option is to serve the actual static files .fallback( get_service(ServeDir::new(format!("{}/static", data_dir))).handle_error( |error: std::io::Error| async move { ( StatusCode::INTERNAL_SERVER_ERROR, format!("Unhandled internal error: {}", error), ) }, ), ) .layer(TraceLayer::new_for_http()); let addr = SocketAddr::from(([0, 0, 0, 0], 3000)); tracing::debug!("listening on {}", addr); axum::Server::bind(&addr) .serve(app.into_make_service()) .await .unwrap(); } async fn post_deploy(body: BodyStream) { // let tar = GzDecoder::new(body); let tar = GzipDecoder::new(body); let mut archive = Archive::new(tar); archive.unpack("./static").unwrap(); }