Skip to content

Commit a9fd962

Browse files
authored
feat: Replace unnecessary getters on Config struct (#423)
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.
1 parent 3285956 commit a9fd962

File tree

5 files changed

+39
-97
lines changed

5 files changed

+39
-97
lines changed

src/addon/file_server/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<'a> FileServer {
4242
/// Creates a new instance of the `FileExplorer` with the provided `root_dir`
4343
pub fn new(config: Arc<Config>) -> Self {
4444
let handlebars = FileServer::make_handlebars_engine();
45-
let scoped_file_system = ScopedFileSystem::new(config.root_dir().clone()).unwrap();
45+
let scoped_file_system = ScopedFileSystem::new(config.root_dir.clone()).unwrap();
4646

4747
FileServer {
4848
handlebars,
@@ -136,7 +136,7 @@ impl<'a> FileServer {
136136
match self.scoped_file_system.resolve(path).await {
137137
Ok(entry) => match entry {
138138
Entry::Directory(dir) => {
139-
if self.config.index() {
139+
if self.config.index {
140140
let mut filepath = dir.path();
141141

142142
filepath.push("index.html");
@@ -160,10 +160,10 @@ impl<'a> FileServer {
160160
}
161161
},
162162
Err(err) => {
163-
if self.config.spa() {
163+
if self.config.spa {
164164
return make_http_file_response(
165165
{
166-
let mut path = self.config.root_dir();
166+
let mut path = self.config.root_dir.clone();
167167
path.push("index.html");
168168

169169
let file = tokio::fs::File::open(&path).await?;
@@ -217,7 +217,7 @@ impl<'a> FileServer {
217217
query_params: Option<QueryParams>,
218218
) -> Result<Response<Body>> {
219219
let directory_index =
220-
FileServer::index_directory(self.config.root_dir().clone(), path, query_params)?;
220+
FileServer::index_directory(self.config.root_dir.clone(), path, query_params)?;
221221
let html = self
222222
.handlebars
223223
.render(EXPLORER_TEMPLATE, &directory_index)

src/config/mod.rs

Lines changed: 14 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -23,78 +23,20 @@ use self::tls::TlsConfig;
2323

2424
/// Server instance configuration used on initialization
2525
pub struct Config {
26-
address: SocketAddr,
27-
host: IpAddr,
28-
port: u16,
29-
index: bool,
30-
spa: bool,
31-
root_dir: PathBuf,
32-
quiet: bool,
33-
tls: Option<TlsConfig>,
34-
cors: Option<CorsConfig>,
35-
compression: Option<CompressionConfig>,
36-
basic_auth: Option<BasicAuthConfig>,
37-
logger: Option<bool>,
38-
proxy: Option<ProxyConfig>,
39-
graceful_shutdown: bool,
40-
}
41-
42-
impl Config {
43-
pub fn host(&self) -> IpAddr {
44-
self.host
45-
}
46-
47-
pub fn port(&self) -> u16 {
48-
self.port
49-
}
50-
51-
pub fn index(&self) -> bool {
52-
self.index
53-
}
54-
55-
pub fn spa(&self) -> bool {
56-
self.spa
57-
}
58-
59-
pub fn address(&self) -> SocketAddr {
60-
self.address
61-
}
62-
63-
pub fn root_dir(&self) -> PathBuf {
64-
self.root_dir.clone()
65-
}
66-
67-
pub fn quiet(&self) -> bool {
68-
self.quiet
69-
}
70-
71-
pub fn tls(&self) -> Option<TlsConfig> {
72-
self.tls.clone()
73-
}
74-
75-
pub fn cors(&self) -> Option<CorsConfig> {
76-
self.cors.clone()
77-
}
78-
79-
pub fn compression(&self) -> Option<CompressionConfig> {
80-
self.compression.clone()
81-
}
82-
83-
pub fn basic_auth(&self) -> Option<BasicAuthConfig> {
84-
self.basic_auth.clone()
85-
}
86-
87-
pub fn logger(&self) -> Option<bool> {
88-
self.logger
89-
}
90-
91-
pub fn proxy(&self) -> Option<ProxyConfig> {
92-
self.proxy.clone()
93-
}
94-
95-
pub fn graceful_shutdown(&self) -> bool {
96-
self.graceful_shutdown
97-
}
26+
pub address: SocketAddr,
27+
pub host: IpAddr,
28+
pub port: u16,
29+
pub index: bool,
30+
pub spa: bool,
31+
pub root_dir: PathBuf,
32+
pub quiet: bool,
33+
pub tls: Option<TlsConfig>,
34+
pub cors: Option<CorsConfig>,
35+
pub compression: Option<CompressionConfig>,
36+
pub basic_auth: Option<BasicAuthConfig>,
37+
pub logger: Option<bool>,
38+
pub proxy: Option<ProxyConfig>,
39+
pub graceful_shutdown: bool,
9840
}
9941

10042
impl Default for Config {

src/server/handler/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl HttpHandler {
4343

4444
impl From<Arc<Config>> for HttpHandler {
4545
fn from(config: Arc<Config>) -> Self {
46-
if let Some(proxy_config) = config.proxy() {
46+
if let Some(proxy_config) = config.proxy.clone() {
4747
let proxy = Proxy::new(&proxy_config.url);
4848
let request_handler = Arc::new(ProxyHandler::new(proxy));
4949
let middleware = Middleware::try_from(config).unwrap();

src/server/middleware/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,25 +102,25 @@ impl TryFrom<Arc<Config>> for Middleware {
102102
fn try_from(config: Arc<Config>) -> std::result::Result<Self, Self::Error> {
103103
let mut middleware = Middleware::default();
104104

105-
if let Some(basic_auth_config) = config.basic_auth() {
105+
if let Some(basic_auth_config) = config.basic_auth.clone() {
106106
let basic_auth_middleware = make_basic_auth_middleware(basic_auth_config);
107107

108108
middleware.before(basic_auth_middleware);
109109
}
110110

111-
if let Some(cors_config) = config.cors() {
111+
if let Some(cors_config) = config.cors.clone() {
112112
let cors_middleware = make_cors_middleware(cors_config);
113113

114114
middleware.after(cors_middleware);
115115
}
116116

117-
if let Some(compression_config) = config.compression() {
117+
if let Some(compression_config) = config.compression.clone() {
118118
if compression_config.gzip {
119119
middleware.after(make_gzip_compression_middleware());
120120
}
121121
}
122122

123-
if let Some(should_log) = config.logger() {
123+
if let Some(should_log) = config.logger {
124124
if should_log {
125125
middleware.after(make_logger_middleware());
126126
}

src/server/mod.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ impl Server {
2727

2828
pub async fn run(self) {
2929
let config = Arc::clone(&self.config);
30-
let address = config.address();
30+
let address = config.address;
3131
let handler = handler::HttpHandler::from(Arc::clone(&config));
3232
let server = Arc::new(self);
3333
let mut server_instances: Vec<tokio::task::JoinHandle<()>> = Vec::new();
3434

35-
if config.spa() {
36-
let mut index_html = config.root_dir();
35+
if config.spa {
36+
let mut index_html = config.root_dir.clone();
3737
index_html.push("index.html");
3838

3939
if !index_html.exists() {
@@ -44,11 +44,11 @@ impl Server {
4444
}
4545
}
4646

47-
if config.tls().is_some() {
48-
let https_config = config.tls().unwrap();
47+
if config.tls.is_some() {
48+
let https_config = config.tls.clone().unwrap();
4949
let handler = handler.clone();
50-
let host = config.address().ip();
51-
let port = config.address().port().saturating_add(1);
50+
let host = config.address.ip();
51+
let port = config.address.port().saturating_add(1);
5252
let address = SocketAddr::new(host, port);
5353
let server = Arc::clone(&server);
5454
let task = tokio::spawn(async move {
@@ -86,17 +86,17 @@ impl Server {
8686
}
8787
}));
8888

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

92-
if self.config.address().ip() == Ipv4Addr::from_str("0.0.0.0").unwrap() {
92+
if self.config.address.ip() == Ipv4Addr::from_str("0.0.0.0").unwrap() {
9393
if let Ok(ip) = local_ip_address::local_ip() {
94-
println!("Local Network IP: http://{}:{}", ip, self.config.port());
94+
println!("Local Network IP: http://{}:{}", ip, self.config.port);
9595
}
9696
}
9797
}
9898

99-
if self.config.graceful_shutdown() {
99+
if self.config.graceful_shutdown {
100100
let graceful = server.with_graceful_shutdown(crate::utils::signal::shutdown_signal());
101101

102102
if let Err(e) = graceful.await {
@@ -131,17 +131,17 @@ impl Server {
131131
}
132132
}));
133133

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

137-
if self.config.address().ip() == Ipv4Addr::from_str("0.0.0.0").unwrap() {
137+
if self.config.address.ip() == Ipv4Addr::from_str("0.0.0.0").unwrap() {
138138
if let Ok(ip) = local_ip_address::local_ip() {
139-
println!("Local Network IP: https://{}:{}", ip, self.config.port());
139+
println!("Local Network IP: https://{}:{}", ip, self.config.port);
140140
}
141141
}
142142
}
143143

144-
if self.config.graceful_shutdown() {
144+
if self.config.graceful_shutdown {
145145
let graceful = server.with_graceful_shutdown(crate::utils::signal::shutdown_signal());
146146

147147
if let Err(e) = graceful.await {

0 commit comments

Comments
 (0)