39 lines
1.1 KiB
Rust
39 lines
1.1 KiB
Rust
use actix_web::{web::{self, Data}, App, HttpServer};
|
|
use tera::Tera;
|
|
|
|
use crate::{handlers, templates, Args};
|
|
|
|
const ADDRESS: &str = "127.0.0.1";
|
|
|
|
pub async fn run(args: &Args) -> Result<(), ServerError> {
|
|
std::fs::create_dir_all(&args.schem_dir_path)?;
|
|
|
|
let cloned_args = args.clone();
|
|
let mut tera = Tera::default();
|
|
tera.add_raw_template("index.html", templates::INDEX_HTML)?;
|
|
|
|
HttpServer::new(move || {
|
|
App::new()
|
|
.app_data(Data::new(cloned_args.clone()))
|
|
.app_data(Data::new(tera.clone()))
|
|
.route("/robots.txt", web::get().to(handlers::robots_txt))
|
|
.route("/favicon.ico", web::get().to(handlers::favicon_ico))
|
|
.route("/", web::get().to(handlers::index))
|
|
.route("/download/{filename:.*}", web::get().to(handlers::download))
|
|
.route("/upload", web::post().to(handlers::upload))
|
|
})
|
|
.bind((ADDRESS, args.port))?
|
|
.run()
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum ServerError {
|
|
#[error(transparent)]
|
|
Io(#[from] std::io::Error),
|
|
#[error(transparent)]
|
|
Tera(#[from] tera::Error),
|
|
}
|