implemented file serving

This commit is contained in:
antifallobst 2023-08-25 00:33:45 +02:00
parent 76180aaa5a
commit 1985d87d97
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
4 changed files with 74 additions and 5 deletions

55
server/Cargo.lock generated
View File

@ -19,6 +19,29 @@ dependencies = [
"tracing",
]
[[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 1.3.2",
"bytes",
"derive_more",
"futures-core",
"http-range",
"log",
"mime",
"mime_guess",
"percent-encoding",
"pin-project-lite",
]
[[package]]
name = "actix-http"
version = "3.3.1"
@ -251,6 +274,12 @@ version = "1.0.75"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
[[package]]
name = "askama_escape"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341"
[[package]]
name = "autocfg"
version = "1.1.0"
@ -592,6 +621,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"
@ -720,6 +755,16 @@ version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
[[package]]
name = "mime_guess"
version = "2.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef"
dependencies = [
"mime",
"unicase",
]
[[package]]
name = "miniz_oxide"
version = "0.7.1"
@ -1004,6 +1049,7 @@ dependencies = [
name = "server"
version = "0.1.0"
dependencies = [
"actix-files",
"actix-web",
"anyhow",
"env_logger",
@ -1198,6 +1244,15 @@ version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba"
[[package]]
name = "unicase"
version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89"
dependencies = [
"version_check",
]
[[package]]
name = "unicode-bidi"
version = "0.3.13"

View File

@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
actix-web = "4"
actix-files = "0.6.2"
env_logger = "0.10"
log = "0.4"
anyhow = "1.0.75"

View File

@ -1,7 +1,7 @@
use anyhow::Result;
use crate::parser;
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Sites {
home: String,
// services: String,
@ -12,7 +12,7 @@ pub struct Sites {
}
impl Sites {
pub async fn new(path: &std::path::Path) -> Result<Self> {
pub fn new(path: &std::path::Path) -> Result<Self> {
std::env::set_current_dir(path)?;
let mut home_file = std::fs::File::open("home.html")?;

View File

@ -1,20 +1,33 @@
mod cache;
mod parser;
use actix_web::{get, web, App, HttpServer, Responder, HttpResponse};
use anyhow::Result;
use log::info;
#[get("/")]
async fn home(data: web::Data<cache::Sites>) -> impl Responder {
HttpResponse::Ok().body(data.home().clone())
}
#[actix_web::main]
async fn main() -> Result<()>{
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
info!("Generating sites");
let sites = cache::Sites::new(&std::path::Path::new("../static/")).await?;
info!("{}", sites.home());
let sites = cache::Sites::new(&std::path::Path::new("../static"))?;
info!("Starting server");
HttpServer::new(move || {
App::new()
.service(actix_files::Files::new("/style", "./style"))
.service(home)
.app_data(web::Data::new(sites.clone()))
})
.bind(("0.0.0.0", 8080))?
.run()
.await?;
Ok(())
}