Added the possibility to serve static files

This commit is contained in:
Slatian
2023-02-25 13:30:46 +01:00
parent a48050b234
commit 97507634df
6 changed files with 90 additions and 8 deletions

View File

@ -17,6 +17,7 @@ pub struct ServerConfig {
pub ip_header: SecureClientIpSource,
pub allow_private_ip_lookup: bool,
pub static_location: Option<String>,
}
#[derive(serde::Deserialize, Clone)]
@ -52,6 +53,7 @@ impl Default for ServerConfig {
listen_on: "127.0.0.1:3000".parse().unwrap(),
ip_header: SecureClientIpSource::ConnectInfo,
allow_private_ip_lookup: false,
static_location: None,
}
}
}

View File

@ -7,6 +7,7 @@ use axum::{
},
headers,
http::Request,
handler::Handler,
middleware::{self, Next},
response::Response,
Router,
@ -19,6 +20,7 @@ use lazy_static::lazy_static;
use regex::Regex;
use tera::Tera;
use tower::ServiceBuilder;
use tower_http::services::ServeDir;
use trust_dns_resolver::{
TokioAsyncResolver,
// config::ResolverOpts,
@ -103,9 +105,11 @@ struct CliArgs {
#[arg(short, long)]
listen_on: Option<String>,
#[arg(short, long)]
templates: Option<String>,
template_location: Option<String>,
#[arg(short,long)]
extra_config: Option<String>,
#[arg(short,long)]
static_location: Option<String>,
}
fn match_domain_hidden_list(domain: &String, hidden_list: &Vec<String>) -> bool {
@ -155,7 +159,10 @@ async fn main() {
};
// Initalize Tera templates
let mut template_base_dir = (&config.template.template_location).to_owned();
let mut template_base_dir = match cli_args.template_location {
Some(template_base_dir) => template_base_dir,
None => (&config.template.template_location).to_owned(),
};
if !template_base_dir.ends_with("/") {
template_base_dir = template_base_dir + "/";
}
@ -170,7 +177,7 @@ async fn main() {
},
},
};
let template_glob = template_base_dir+"*.html";
let template_glob = template_base_dir.clone()+"*.html";
println!("Parsing Templates from '{}' ...", &template_glob);
let res = Tera::new((template_glob).as_str());
let tera = match res {
@ -186,8 +193,13 @@ async fn main() {
template_config: template_extra_config,
};
// Initalize Rate Limiter
// Static file directory
let static_file_directory = cli_args.static_location.unwrap_or(
config.server.static_location.clone().unwrap_or(
template_base_dir+"/static"
)
);
// Initalize GeoIP Database
@ -243,8 +255,11 @@ async fn main() {
.route("/ip/:address", get(handle_ip_route_with_path))
.route("/ua", get(user_agent_handler))
.route("/hi", get(hello_world_handler))
.fallback(not_found_handler)
.with_state(shared_state)
.fallback_service(
ServeDir::new(static_file_directory)
.fallback(not_found_handler.with_state(shared_state.clone()))
)
.with_state(shared_state)
.layer(
ServiceBuilder::new()
.layer(ip_header.into_extension())