moved config settings to separate module.

This commit is contained in:
Jeff Baskin 2022-06-18 21:24:11 -04:00
parent e79cf82b62
commit 217aea7a97
7 changed files with 240 additions and 34 deletions

154
Cargo.lock generated
View File

@ -974,6 +974,16 @@ version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3" checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
[[package]]
name = "lock_api"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "327fa5b6a6940e4699ec49a9beae1ea4845c6bab9314e4f84ac68742139d8c53"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.14" version = "0.4.14"
@ -1030,6 +1040,8 @@ version = "0.1.0"
dependencies = [ dependencies = [
"async-std", "async-std",
"config", "config",
"serde",
"serial_test",
"tide", "tide",
"tide-testing", "tide-testing",
] ]
@ -1107,6 +1119,29 @@ version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72" checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72"
[[package]]
name = "parking_lot"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09a279cbf25cb0757810394fbc1e359949b59e348145c643a939a525692e6929"
dependencies = [
"cfg-if 1.0.0",
"libc",
"redox_syscall",
"smallvec",
"windows-sys",
]
[[package]] [[package]]
name = "pathdiff" name = "pathdiff"
version = "0.2.1" version = "0.2.1"
@ -1230,6 +1265,30 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]] [[package]]
name = "proc-macro-hack" name = "proc-macro-hack"
version = "0.5.19" version = "0.5.19"
@ -1325,6 +1384,15 @@ dependencies = [
"rand_core 0.5.1", "rand_core 0.5.1",
] ]
[[package]]
name = "redox_syscall"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62f25bc4c7e55e0b0b7a1d43fb893f4fa1361d0abe38b9ce4f323c2adfe6ef42"
dependencies = [
"bitflags",
]
[[package]] [[package]]
name = "ron" name = "ron"
version = "0.7.0" version = "0.7.0"
@ -1361,12 +1429,24 @@ dependencies = [
"semver", "semver",
] ]
[[package]]
name = "rustversion"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2cc38e8fa666e2de3c4aba7edeb5ffc5246c1c2ed0e3d17e560aeeba736b23f"
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.9" version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f"
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]] [[package]]
name = "semver" name = "semver"
version = "0.9.0" version = "0.9.0"
@ -1436,6 +1516,31 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serial_test"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d19dbfb999a147cedbfe82f042eb9555f5b0fa4ef95ee4570b74349103d9c9f4"
dependencies = [
"lazy_static",
"log",
"parking_lot",
"serial_test_derive",
]
[[package]]
name = "serial_test_derive"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb9e2050b2be1d681f8f1c1a528bcfe4e00afa2d8995f713974f5333288659f2"
dependencies = [
"proc-macro-error",
"proc-macro2",
"quote",
"rustversion",
"syn",
]
[[package]] [[package]]
name = "sha-1" name = "sha-1"
version = "0.8.2" version = "0.8.2"
@ -1510,6 +1615,12 @@ version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9def91fd1e018fe007022791f865d0ccc9b3a0d5001e01aabb8b40e46000afb5" checksum = "9def91fd1e018fe007022791f865d0ccc9b3a0d5001e01aabb8b40e46000afb5"
[[package]]
name = "smallvec"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83"
[[package]] [[package]]
name = "socket2" name = "socket2"
version = "0.4.4" version = "0.4.4"
@ -1958,6 +2069,49 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2"
dependencies = [
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_msvc"
version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47"
[[package]]
name = "windows_i686_gnu"
version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6"
[[package]]
name = "windows_i686_msvc"
version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024"
[[package]]
name = "windows_x86_64_gnu"
version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1"
[[package]]
name = "windows_x86_64_msvc"
version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680"
[[package]] [[package]]
name = "yaml-rust" name = "yaml-rust"
version = "0.4.5" version = "0.4.5"

View File

@ -8,8 +8,10 @@ edition = "2021"
[dependencies] [dependencies]
# morethantext = { git=https://gitea.baskinprattle.com/jbaskin/MoreThanText.git } # morethantext = { git=https://gitea.baskinprattle.com/jbaskin/MoreThanText.git }
async-std = { version = "1.11", features = ["attributes"] } async-std = { version = "1.11", features = ["attributes"] }
tide = "0.16"
config = "0.13" config = "0.13"
serde = "1.0"
tide = "0.16"
[dev-dependencies] [dev-dependencies]
tide-testing = "0.1.3" serial_test = "0.7"
tide-testing = "0.1"

View File

@ -1,26 +1,26 @@
use config::Config;
use tide::{http::StatusCode, Request, Response}; use tide::{http::StatusCode, Request, Response};
mod settings;
use settings::Settings;
#[async_std::main] #[async_std::main]
async fn main() -> tide::Result<()> { async fn main() -> tide::Result<()> {
let settings = Config::builder() //let settings = Config::builder()
.set_default("address", "127.0.0.1")? // .set_default("address", "127.0.0.1")?
.set_default("port", "9090")? // .set_default("port", 9090)?
.add_source( // .add_source(
config::Environment::with_prefix("MTT") // config::Environment::with_prefix("MTT")
.try_parsing(true) // .try_parsing(true)
.separator("_") // .separator("_")
.list_separator(" "), // .list_separator(" "),
) // )
.build() // .build()
.unwrap(); // .unwrap();
let sett = Settings::new().unwrap();
let app = app_setup().await; let app = app_setup().await;
app.listen(format!( app.listen(format!("{}:{}", sett.address, sett.port))
"{}:{}", .await?;
settings.get_string("address").unwrap(),
settings.get_string("port").unwrap()
))
.await?;
Ok(()) Ok(())
} }
@ -45,7 +45,7 @@ async fn home(_req: Request<()>) -> tide::Result {
} }
#[cfg(test)] #[cfg(test)]
mod basic_tests { mod server_app {
use super::*; use super::*;
use tide_testing::TideTestingExt; use tide_testing::TideTestingExt;

50
src/settings.rs Normal file
View File

@ -0,0 +1,50 @@
use config::{Config, ConfigError};
use serde::Deserialize;
#[derive(Debug, Deserialize)]
//#[allow(unused)]
pub struct Settings {
pub address: String,
pub port: u16,
}
impl Settings {
pub fn new() -> Result<Self, ConfigError> {
let s = Config::builder()
.set_default("port", 9090)?
.set_default("address", "127.0.0.1")?
.add_source(
config::Environment::with_prefix("MTT")
.try_parsing(true)
.separator("_")
.list_separator(" "),
)
.build()?;
s.try_deserialize()
}
}
#[cfg(test)]
mod settings_tests {
use super::*;
use serial_test::serial;
#[test]
#[serial]
fn defaults() {
let set = Settings::new().unwrap();
println!("{:?}", set);
assert_eq!(set.port, 9090);
assert_eq!(set.address, "127.0.0.1");
}
#[test]
#[serial]
fn port_env() {
std::env::set_var("MTT_PORT", "7825");
let set = Settings::new().unwrap();
println!("{:?}", set);
assert_eq!(set.port, 7825);
std::env::remove_var("MTT_PORT");
}
}

View File

@ -14,10 +14,3 @@ Feature: Server Start
And it is running And it is running
When the home page is accessed When the home page is accessed
Then the status should be OK Then the status should be OK
#Scenario: Start server with a different address
#Given a server
#And address is set to 127.250.48.58
#And it is running
#When the home page is accessed
#Then the status should be OK

View File

@ -2,7 +2,7 @@
from asyncio import create_subprocess_exec, get_event_loop, sleep from asyncio import create_subprocess_exec, get_event_loop, sleep
from pathlib import Path from pathlib import Path
from socket import socket from socket import gethostname, gethostbyname, socket
class Server: class Server:
@ -73,3 +73,10 @@ class Server:
sock.bind((self.address, 0)) sock.bind((self.address, 0))
self.port = sock.getsockname()[1] self.port = sock.getsockname()[1]
sock.close() sock.close()
def set_to_host_ip(self):
"""Set the server to use something other than localhost."""
hostname = gethostname()
self.address = gethostbyname(hostname)
print(self.address)
self.set_safe_port()

View File

@ -1,6 +1,6 @@
"""Interpratures for server start features.""" """Interpratures for server start features."""
from pytest_bdd import given, parsers, scenarios, then, when from pytest_bdd import given, scenarios, then, when
scenarios("../features/server_start.feature") scenarios("../features/server_start.feature")
@ -10,10 +10,10 @@ def create_server():
"""Set up a server.""" """Set up a server."""
@given(parsers.re(r"address is set to (?P<addr>\S+)")) @given("it is not using localhost")
def set_server_address(server, addr): def set_server_address(server):
"""Set the Server address.""" """Sets tthe server to not use localhost."""
server.address = addr server.set_to_host_ip()
@given("port is changed to something different") @given("port is changed to something different")