Skip to content

Commit

Permalink
feat: Replace unnecessary getters on Config struct (#423)
Browse files Browse the repository at this point in the history
Implemented #401 

All I did was make the fields public and delete the getters. I decided
to try to remove the unnecessary `clone`s in a separate PR after this
one gets merged for transparency.
  • Loading branch information
Antosser committed Feb 28, 2024
1 parent 3285956 commit a9fd962
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 97 deletions.
10 changes: 5 additions & 5 deletions src/addon/file_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<'a> FileServer {
/// Creates a new instance of the `FileExplorer` with the provided `root_dir`
pub fn new(config: Arc<Config>) -> Self {
let handlebars = FileServer::make_handlebars_engine();
let scoped_file_system = ScopedFileSystem::new(config.root_dir().clone()).unwrap();
let scoped_file_system = ScopedFileSystem::new(config.root_dir.clone()).unwrap();

FileServer {
handlebars,
Expand Down Expand Up @@ -136,7 +136,7 @@ impl<'a> FileServer {
match self.scoped_file_system.resolve(path).await {
Ok(entry) => match entry {
Entry::Directory(dir) => {
if self.config.index() {
if self.config.index {
let mut filepath = dir.path();

filepath.push("index.html");
Expand All @@ -160,10 +160,10 @@ impl<'a> FileServer {
}
},
Err(err) => {
if self.config.spa() {
if self.config.spa {
return make_http_file_response(
{
let mut path = self.config.root_dir();
let mut path = self.config.root_dir.clone();
path.push("index.html");

let file = tokio::fs::File::open(&path).await?;
Expand Down Expand Up @@ -217,7 +217,7 @@ impl<'a> FileServer {
query_params: Option<QueryParams>,
) -> Result<Response<Body>> {
let directory_index =
FileServer::index_directory(self.config.root_dir().clone(), path, query_params)?;
FileServer::index_directory(self.config.root_dir.clone(), path, query_params)?;
let html = self
.handlebars
.render(EXPLORER_TEMPLATE, &directory_index)
Expand Down
86 changes: 14 additions & 72 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,78 +23,20 @@ use self::tls::TlsConfig;

/// Server instance configuration used on initialization
pub struct Config {
address: SocketAddr,
host: IpAddr,
port: u16,
index: bool,
spa: bool,
root_dir: PathBuf,
quiet: bool,
tls: Option<TlsConfig>,
cors: Option<CorsConfig>,
compression: Option<CompressionConfig>,
basic_auth: Option<BasicAuthConfig>,
logger: Option<bool>,
proxy: Option<ProxyConfig>,
graceful_shutdown: bool,
}

impl Config {
pub fn host(&self) -> IpAddr {
self.host
}

pub fn port(&self) -> u16 {
self.port
}

pub fn index(&self) -> bool {
self.index
}

pub fn spa(&self) -> bool {
self.spa
}

pub fn address(&self) -> SocketAddr {
self.address
}

pub fn root_dir(&self) -> PathBuf {
self.root_dir.clone()
}

pub fn quiet(&self) -> bool {
self.quiet
}

pub fn tls(&self) -> Option<TlsConfig> {
self.tls.clone()
}

pub fn cors(&self) -> Option<CorsConfig> {
self.cors.clone()
}

pub fn compression(&self) -> Option<CompressionConfig> {
self.compression.clone()
}

pub fn basic_auth(&self) -> Option<BasicAuthConfig> {
self.basic_auth.clone()
}

pub fn logger(&self) -> Option<bool> {
self.logger
}

pub fn proxy(&self) -> Option<ProxyConfig> {
self.proxy.clone()
}

pub fn graceful_shutdown(&self) -> bool {
self.graceful_shutdown
}
pub address: SocketAddr,
pub host: IpAddr,
pub port: u16,
pub index: bool,
pub spa: bool,
pub root_dir: PathBuf,
pub quiet: bool,
pub tls: Option<TlsConfig>,
pub cors: Option<CorsConfig>,
pub compression: Option<CompressionConfig>,
pub basic_auth: Option<BasicAuthConfig>,
pub logger: Option<bool>,
pub proxy: Option<ProxyConfig>,
pub graceful_shutdown: bool,
}

impl Default for Config {
Expand Down
2 changes: 1 addition & 1 deletion src/server/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl HttpHandler {

impl From<Arc<Config>> for HttpHandler {
fn from(config: Arc<Config>) -> Self {
if let Some(proxy_config) = config.proxy() {
if let Some(proxy_config) = config.proxy.clone() {
let proxy = Proxy::new(&proxy_config.url);
let request_handler = Arc::new(ProxyHandler::new(proxy));
let middleware = Middleware::try_from(config).unwrap();
Expand Down
8 changes: 4 additions & 4 deletions src/server/middleware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,25 @@ impl TryFrom<Arc<Config>> for Middleware {
fn try_from(config: Arc<Config>) -> std::result::Result<Self, Self::Error> {
let mut middleware = Middleware::default();

if let Some(basic_auth_config) = config.basic_auth() {
if let Some(basic_auth_config) = config.basic_auth.clone() {
let basic_auth_middleware = make_basic_auth_middleware(basic_auth_config);

middleware.before(basic_auth_middleware);
}

if let Some(cors_config) = config.cors() {
if let Some(cors_config) = config.cors.clone() {
let cors_middleware = make_cors_middleware(cors_config);

middleware.after(cors_middleware);
}

if let Some(compression_config) = config.compression() {
if let Some(compression_config) = config.compression.clone() {
if compression_config.gzip {
middleware.after(make_gzip_compression_middleware());
}
}

if let Some(should_log) = config.logger() {
if let Some(should_log) = config.logger {
if should_log {
middleware.after(make_logger_middleware());
}
Expand Down
30 changes: 15 additions & 15 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ impl Server {

pub async fn run(self) {
let config = Arc::clone(&self.config);
let address = config.address();
let address = config.address;
let handler = handler::HttpHandler::from(Arc::clone(&config));
let server = Arc::new(self);
let mut server_instances: Vec<tokio::task::JoinHandle<()>> = Vec::new();

if config.spa() {
let mut index_html = config.root_dir();
if config.spa {
let mut index_html = config.root_dir.clone();
index_html.push("index.html");

if !index_html.exists() {
Expand All @@ -44,11 +44,11 @@ impl Server {
}
}

if config.tls().is_some() {
let https_config = config.tls().unwrap();
if config.tls.is_some() {
let https_config = config.tls.clone().unwrap();
let handler = handler.clone();
let host = config.address().ip();
let port = config.address().port().saturating_add(1);
let host = config.address.ip();
let port = config.address.port().saturating_add(1);
let address = SocketAddr::new(host, port);
let server = Arc::clone(&server);
let task = tokio::spawn(async move {
Expand Down Expand Up @@ -86,17 +86,17 @@ impl Server {
}
}));

if !self.config.quiet() {
if !self.config.quiet {
println!("Serving HTTP: http://{}", address);

if self.config.address().ip() == Ipv4Addr::from_str("0.0.0.0").unwrap() {
if self.config.address.ip() == Ipv4Addr::from_str("0.0.0.0").unwrap() {
if let Ok(ip) = local_ip_address::local_ip() {
println!("Local Network IP: http://{}:{}", ip, self.config.port());
println!("Local Network IP: http://{}:{}", ip, self.config.port);
}
}
}

if self.config.graceful_shutdown() {
if self.config.graceful_shutdown {
let graceful = server.with_graceful_shutdown(crate::utils::signal::shutdown_signal());

if let Err(e) = graceful.await {
Expand Down Expand Up @@ -131,17 +131,17 @@ impl Server {
}
}));

if !self.config.quiet() {
if !self.config.quiet {
println!("Serving HTTPS: http://{}", address);

if self.config.address().ip() == Ipv4Addr::from_str("0.0.0.0").unwrap() {
if self.config.address.ip() == Ipv4Addr::from_str("0.0.0.0").unwrap() {
if let Ok(ip) = local_ip_address::local_ip() {
println!("Local Network IP: https://{}:{}", ip, self.config.port());
println!("Local Network IP: https://{}:{}", ip, self.config.port);
}
}
}

if self.config.graceful_shutdown() {
if self.config.graceful_shutdown {
let graceful = server.with_graceful_shutdown(crate::utils::signal::shutdown_signal());

if let Err(e) = graceful.await {
Expand Down

0 comments on commit a9fd962

Please sign in to comment.