feat: read publication dir config and serve it

This commit is contained in:
Aravinth Manivannan 2023-03-12 20:10:13 +05:30
parent d2c52cc62c
commit 9411c2ba9f
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
6 changed files with 55 additions and 16 deletions

36
Cargo.lock generated
View File

@ -46,6 +46,29 @@ dependencies = [
"smallvec",
]
[[package]]
name = "actix-files"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d832782fac6ca7369a70c9ee9a20554623c5e51c76e190ad151780ebea1cf689"
dependencies = [
"actix-http",
"actix-service",
"actix-utils",
"actix-web",
"askama_escape",
"bitflags",
"bytes",
"derive_more",
"futures-core",
"http-range",
"log",
"mime",
"mime_guess",
"percent-encoding",
"pin-project-lite",
]
[[package]]
name = "actix-http"
version = "3.3.1"
@ -405,6 +428,12 @@ version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6"
[[package]]
name = "askama_escape"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341"
[[package]]
name = "async-trait"
version = "0.1.66"
@ -1298,6 +1327,12 @@ dependencies = [
"itoa",
]
[[package]]
name = "http-range"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21dec9db110f5f872ed9699c3ecf50cf16f423502706ba5c72462e28d3157573"
[[package]]
name = "httparse"
version = "1.8.0"
@ -2529,6 +2564,7 @@ version = "0.1.0"
dependencies = [
"actix-auth-middleware",
"actix-cors",
"actix-files",
"actix-http",
"actix-identity",
"actix-rt",

View File

@ -28,6 +28,7 @@ actix-session = { version = "0.6.1", features = ["cookie-session"]}
actix-http = "3.0.4"
actix-rt = "2"
actix-cors = "0.6.1"
actix-files = "0.6.0"
actix-service = "2.0.0"
#actix = "0.12"
actix-web-codegen-const-routes = { version = "0.1.0", tag = "0.1.0", git = "https://github.com/realaravinth/actix-web-codegen-const-routes" }

View File

@ -34,8 +34,8 @@ password = "password"
name = "postgres"
pool = 4
[archive]
base_path = "/tmp/mcaptcha-survey"
[publish]
dir = "/tmp/mcaptcha-survey"
[footer]
about = "https://mcapthca.org/about"

View File

@ -17,6 +17,7 @@
use std::env;
use std::sync::Arc;
use actix_files::Files;
use actix_identity::{CookieIdentityPolicy, IdentityService};
use actix_session::{storage::CookieSessionStore, SessionMiddleware};
use actix_web::{
@ -106,6 +107,7 @@ async fn main() -> std::io::Result<()> {
actix_middleware::TrailingSlash::Trim,
))
.configure(services)
.service(Files::new("/download", &settings.publish.dir).show_files_listing())
.app_data(data.clone())
})
.bind(ip)

View File

@ -85,21 +85,20 @@ pub struct Footer {
pub thanks: Url,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Archive {
pub base_path: String,
pub struct Publish {
pub dir: String,
}
impl Archive {
fn create_archive_base_path(&self) {
let base_path = Path::new(&self.base_path);
if base_path.exists() {
if !base_path.is_dir() {
fs::remove_file(&base_path).unwrap();
fs::create_dir_all(&base_path).unwrap();
impl Publish {
fn create_root_dir(&self) {
let root = Path::new(&self.dir);
if root.exists() {
if !root.is_dir() {
std::fs::remove_file(&root).unwrap();
std::fs::create_dir_all(&root).unwrap();
}
} else {
fs::create_dir_all(&base_path).unwrap();
std::fs::create_dir_all(&root).unwrap();
}
}
}
@ -114,7 +113,7 @@ pub struct Settings {
pub support_email: String,
pub default_campaign: String,
pub footer: Footer,
pub archive: Archive,
pub publish: Publish,
}
#[cfg(not(tarpaulin_include))]
@ -166,7 +165,7 @@ impl Settings {
match s.try_into::<Settings>() {
Ok(val) => {
val.archive.create_archive_base_path();
val.publish.create_root_dir();
Ok(val)
},
Err(e) => Err(ConfigError::Message(format!("\n\nError: {}. If it says missing fields, then please refer to https://github.com/mCaptcha/mcaptcha#configuration to learn more about how mcaptcha reads configuration\n\n", e))),

View File

@ -44,7 +44,7 @@ use crate::V1_API_ROUTES;
pub async fn get_test_data() -> Arc<Data> {
let mut settings = Settings::new().unwrap();
let tmp_dir = Temp::new_dir().unwrap();
settings.archive.base_path = tmp_dir.join("base_path").to_str().unwrap().into();
settings.publish.dir = tmp_dir.join("base_path").to_str().unwrap().into();
settings.allow_registration = true;
Data::new(settings).await
}
@ -126,6 +126,7 @@ macro_rules! get_app {
.wrap(actix_web::middleware::NormalizePath::new(
actix_web::middleware::TrailingSlash::Trim,
))
.service(Files::new("/download", &$settings.publish.dir).show_files_listing())
.configure($crate::services)
};