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

60
Cargo.lock generated
View File

@ -67,7 +67,7 @@ dependencies = [
"sync_wrapper",
"tokio",
"tower",
"tower-http",
"tower-http 0.3.5",
"tower-layer",
"tower-service",
]
@ -378,6 +378,7 @@ dependencies = [
"tokio",
"toml",
"tower",
"tower-http 0.4.0",
"trust-dns-proto",
"trust-dns-resolver",
]
@ -978,6 +979,16 @@ version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[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 = "mio"
version = "0.8.5"
@ -1664,6 +1675,19 @@ dependencies = [
"syn",
]
[[package]]
name = "tokio-util"
version = "0.7.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5427d89453009325de0d8f342c9490009f76e999cb7672d77e46267448f7e6b2"
dependencies = [
"bytes",
"futures-core",
"futures-sink",
"pin-project-lite",
"tokio",
]
[[package]]
name = "toml"
version = "0.7.2"
@ -1733,6 +1757,31 @@ dependencies = [
"tower-service",
]
[[package]]
name = "tower-http"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d1d42a9b3f3ec46ba828e8d376aec14592ea199f70a06a548587ecd1c4ab658"
dependencies = [
"bitflags",
"bytes",
"futures-core",
"futures-util",
"http",
"http-body",
"http-range-header",
"httpdate",
"mime",
"mime_guess",
"percent-encoding",
"pin-project-lite",
"tokio",
"tokio-util",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "tower-layer"
version = "0.3.2"
@ -1900,6 +1949,15 @@ dependencies = [
"unic-common",
]
[[package]]
name = "unicase"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6"
dependencies = [
"version_check",
]
[[package]]
name = "unicode-bidi"
version = "0.3.10"

View File

@ -18,7 +18,8 @@ serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["full"] }
tera = "1"
toml = "0.7"
tower = "*"
tower = "0.4"
tower-http = { version = "0.4", features = ["fs"] }
trust-dns-proto = "0.22"
trust-dns-resolver = "0.22"
maxminddb = "0.23"

View File

@ -11,6 +11,10 @@ ip_header = "RightmostXForwardedFor"
# When you don't want to use a proxy server:
#ip_header = "ConnectInfo"
# You can specify a custom location for static files here
# Defaults to the static directory in the templete direcotry
# Acts as an "underlay"
#static_location = ""
# Allow querying of private range ips
# enable if you want to use this service

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())

View File

@ -0,0 +1,2 @@
User-agent: *
Disallow: /*?