mirror of
https://codeberg.org/slatian/service.echoip-slatecave.git
synced 2025-07-16 14:03:28 +02:00
Added the possibility to serve static files
This commit is contained in:
27
src/main.rs
27
src/main.rs
@ -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())
|
||||
|
Reference in New Issue
Block a user