Compare commits
1 Commits
3b5ef7bdf0
...
f2c32220fd
Author | SHA1 | Date |
---|---|---|
Renovate Bot | f2c32220fd |
|
@ -18,7 +18,7 @@ COPY . ./
|
||||||
RUN cargo build --release
|
RUN cargo build --release
|
||||||
|
|
||||||
|
|
||||||
FROM alpine:3.19
|
FROM alpine:3.18
|
||||||
|
|
||||||
COPY --from=builder /app/target/release/site /bin/site
|
COPY --from=builder /app/target/release/site /bin/site
|
||||||
COPY --from=builder /app/dumb-init /bin/dumb-init
|
COPY --from=builder /app/dumb-init /bin/dumb-init
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
[toolchain]
|
||||||
|
channel = "1.69"
|
|
@ -8,7 +8,6 @@ use axum::{
|
||||||
use flate2::read::GzDecoder;
|
use flate2::read::GzDecoder;
|
||||||
use futures_util::TryStreamExt;
|
use futures_util::TryStreamExt;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::io;
|
|
||||||
use tar::Archive;
|
use tar::Archive;
|
||||||
use tokio_util::io::StreamReader;
|
use tokio_util::io::StreamReader;
|
||||||
|
|
||||||
|
@ -23,15 +22,15 @@ pub async fn post_deploy(
|
||||||
Extension(data_dir): Extension<String>,
|
Extension(data_dir): Extension<String>,
|
||||||
Query(params): Query<StaticDirParams>,
|
Query(params): Query<StaticDirParams>,
|
||||||
res: BodyStream,
|
res: BodyStream,
|
||||||
) -> crate::Result<()> {
|
) -> impl IntoResponse {
|
||||||
// This converts a stream into something that implements AsyncRead, which we can then use to
|
// This converts a stream into something that implements AsyncRead, which we can then use to
|
||||||
// asynchronously write the file to disk
|
// asynchronously write the file to disk
|
||||||
let mut read =
|
let mut read =
|
||||||
StreamReader::new(res.map_err(|axum_err| std::io::Error::new(ErrorKind::Other, axum_err)));
|
StreamReader::new(res.map_err(|axum_err| std::io::Error::new(ErrorKind::Other, axum_err)));
|
||||||
let uuid = uuid::Uuid::new_v4();
|
let uuid = uuid::Uuid::new_v4();
|
||||||
let file_path = Path::new(&data_dir).join(uuid.as_hyphenated().to_string());
|
let file_path = Path::new(&data_dir).join(uuid.as_hyphenated().to_string());
|
||||||
let mut file = tokio::fs::File::create(&file_path).await?;
|
let mut file = tokio::fs::File::create(&file_path).await.unwrap();
|
||||||
tokio::io::copy(&mut read, &mut file).await?;
|
tokio::io::copy(&mut read, &mut file).await;
|
||||||
|
|
||||||
// If no dir is provided, we use the default one. Otherwise, use the provided one.
|
// If no dir is provided, we use the default one. Otherwise, use the provided one.
|
||||||
let static_path = Path::new(&data_dir)
|
let static_path = Path::new(&data_dir)
|
||||||
|
@ -39,30 +38,34 @@ pub async fn post_deploy(
|
||||||
.join(params.dir.unwrap_or(DEFAULT_STATIC_SITE.to_string()));
|
.join(params.dir.unwrap_or(DEFAULT_STATIC_SITE.to_string()));
|
||||||
|
|
||||||
// Make sure the static directory exists
|
// Make sure the static directory exists
|
||||||
tokio::fs::create_dir_all(&static_path).await?;
|
tokio::fs::create_dir_all(&static_path).await;
|
||||||
|
|
||||||
let fp_clone = file_path.clone();
|
let fp_clone = file_path.clone();
|
||||||
// Extract the contents of the tarball synchronously
|
// Extract the contents of the tarball synchronously
|
||||||
tokio::task::spawn_blocking(move || process_archive(&fp_clone, &static_path)).await??;
|
let res =
|
||||||
|
match tokio::task::spawn_blocking(move || process_archive(&fp_clone, &static_path)).await {
|
||||||
|
Ok(_) => StatusCode::OK,
|
||||||
|
Err(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
};
|
||||||
|
|
||||||
// Remove archive file after use
|
// Remove archive file after use
|
||||||
tokio::fs::remove_file(&file_path).await?;
|
tokio::fs::remove_file(&file_path).await;
|
||||||
|
|
||||||
Ok(())
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process_archive(archive_path: &Path, static_dir: &Path) -> io::Result<()> {
|
fn process_archive(archive_path: &Path, static_dir: &Path) -> Result<(), ()> {
|
||||||
let file = std::fs::File::open(archive_path)?;
|
let file = std::fs::File::open(archive_path).map_err(|_| ())?;
|
||||||
let tar = GzDecoder::new(file);
|
let tar = GzDecoder::new(file);
|
||||||
let mut archive = Archive::new(tar);
|
let mut archive = Archive::new(tar);
|
||||||
|
|
||||||
let mut paths = HashSet::new();
|
let mut paths = HashSet::new();
|
||||||
|
|
||||||
let entries = archive.entries()?;
|
let entries = archive.entries().map_err(|_| ())?;
|
||||||
// Extract each entry into the output directory
|
// Extract each entry into the output directory
|
||||||
for entry in entries {
|
for entry_res in entries {
|
||||||
let mut entry = entry?;
|
let mut entry = entry_res.map_err(|_| ())?;
|
||||||
entry.unpack_in(static_dir)?;
|
entry.unpack_in(static_dir).map_err(|_| ())?;
|
||||||
|
|
||||||
if let Ok(path) = entry.path() {
|
if let Ok(path) = entry.path() {
|
||||||
paths.insert(path.into_owned());
|
paths.insert(path.into_owned());
|
||||||
|
@ -73,20 +76,20 @@ fn process_archive(archive_path: &Path, static_dir: &Path) -> io::Result<()> {
|
||||||
let mut items = vec![];
|
let mut items = vec![];
|
||||||
|
|
||||||
// Start by populating the vec with the initial files
|
// Start by populating the vec with the initial files
|
||||||
let iter = static_dir.read_dir()?;
|
let iter = static_dir.read_dir().map_err(|_| ())?;
|
||||||
iter.filter_map(|r| r.ok())
|
iter.filter_map(|r| r.ok())
|
||||||
.for_each(|e| items.push(e.path()));
|
.for_each(|e| items.push(e.path()));
|
||||||
|
|
||||||
// As long as there are still items in the vec, we keep going
|
// As long as there are still items in the vec, we keep going
|
||||||
while !items.is_empty() {
|
while items.len() > 0 {
|
||||||
let item = items.pop().unwrap();
|
let item = items.pop().unwrap();
|
||||||
tracing::debug!("{:?}", item);
|
tracing::debug!("{:?}", item);
|
||||||
|
|
||||||
if !paths.contains(item.strip_prefix(&static_dir).unwrap()) {
|
if !paths.contains(item.strip_prefix(&static_dir).unwrap()) {
|
||||||
if item.is_dir() {
|
if item.is_dir() {
|
||||||
std::fs::remove_dir_all(item)?;
|
std::fs::remove_dir_all(item);
|
||||||
} else {
|
} else {
|
||||||
std::fs::remove_file(item)?;
|
std::fs::remove_file(item);
|
||||||
}
|
}
|
||||||
} else if let Ok(iter) = item.read_dir() {
|
} else if let Ok(iter) = item.read_dir() {
|
||||||
iter.filter_map(|r| r.ok())
|
iter.filter_map(|r| r.ok())
|
||||||
|
|
51
src/error.rs
51
src/error.rs
|
@ -1,51 +0,0 @@
|
||||||
use axum::http::StatusCode;
|
|
||||||
use axum::response::{IntoResponse, Response};
|
|
||||||
use std::error::Error;
|
|
||||||
use std::fmt;
|
|
||||||
use std::io;
|
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, ServerError>;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum ServerError {
|
|
||||||
IO(io::Error),
|
|
||||||
Axum(axum::Error),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for ServerError {
|
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
match self {
|
|
||||||
ServerError::IO(err) => write!(fmt, "{}", err),
|
|
||||||
ServerError::Axum(err) => write!(fmt, "{}", err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Error for ServerError {}
|
|
||||||
|
|
||||||
impl IntoResponse for ServerError {
|
|
||||||
fn into_response(self) -> Response {
|
|
||||||
match self {
|
|
||||||
ServerError::IO(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
|
|
||||||
ServerError::Axum(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<io::Error> for ServerError {
|
|
||||||
fn from(err: io::Error) -> Self {
|
|
||||||
ServerError::IO(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<axum::Error> for ServerError {
|
|
||||||
fn from(err: axum::Error) -> Self {
|
|
||||||
ServerError::Axum(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<tokio::task::JoinError> for ServerError {
|
|
||||||
fn from(err: tokio::task::JoinError) -> Self {
|
|
||||||
ServerError::IO(err.into())
|
|
||||||
}
|
|
||||||
}
|
|
11
src/main.rs
11
src/main.rs
|
@ -1,10 +1,3 @@
|
||||||
mod api;
|
|
||||||
mod error;
|
|
||||||
mod matrix;
|
|
||||||
mod metrics;
|
|
||||||
|
|
||||||
pub use error::Result;
|
|
||||||
|
|
||||||
use std::{future::ready, net::SocketAddr};
|
use std::{future::ready, net::SocketAddr};
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
|
@ -19,6 +12,10 @@ use tower_http::{
|
||||||
};
|
};
|
||||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||||
|
|
||||||
|
mod api;
|
||||||
|
mod matrix;
|
||||||
|
mod metrics;
|
||||||
|
|
||||||
/// Name of the directory where static sites are stored inside the data directory
|
/// Name of the directory where static sites are stored inside the data directory
|
||||||
const STATIC_DIR_NAME: &str = "static";
|
const STATIC_DIR_NAME: &str = "static";
|
||||||
/// Name of the subdir of STATIC_DIR_NAME where the default (fallback) site is located
|
/// Name of the subdir of STATIC_DIR_NAME where the default (fallback) site is located
|
||||||
|
|
Loading…
Reference in New Issue