Use configuration file

This commit is contained in:
2022-02-19 20:37:30 +01:00
parent f85da365e9
commit 2b1df4b6fd
11 changed files with 356 additions and 39 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
/target
config/local.*

287
Cargo.lock generated
View File

@@ -29,7 +29,7 @@ dependencies = [
"actix-rt",
"actix-service",
"actix-utils",
"ahash",
"ahash 0.7.6",
"base64",
"bitflags",
"brotli2",
@@ -51,7 +51,7 @@ dependencies = [
"percent-encoding",
"pin-project-lite",
"rand",
"sha-1",
"sha-1 0.10.0",
"smallvec",
"zstd",
]
@@ -145,7 +145,7 @@ dependencies = [
"actix-service",
"actix-utils",
"actix-web-codegen",
"ahash",
"ahash 0.7.6",
"bytes",
"cfg-if",
"cookie",
@@ -187,6 +187,12 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "ahash"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "739f4a8db6605981345c5654f3a85b056ce52f37a39d34da03f25bf2151ea16e"
[[package]]
name = "ahash"
version = "0.7.6"
@@ -222,6 +228,17 @@ version = "1.0.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94a45b455c14666b85fc40a019e8ab9eb75e3a124e05494f5397122bc9eb06e0"
[[package]]
name = "async-trait"
version = "0.1.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "061a7acccaa286c011ddc30970520b98fa40e00c9d644633fb26b5fc63a265e3"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "autocfg"
version = "1.0.1"
@@ -255,13 +272,34 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "block-buffer"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b"
dependencies = [
"block-padding",
"byte-tools",
"byteorder",
"generic-array 0.12.4",
]
[[package]]
name = "block-buffer"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1d36a02058e76b040de25a4464ba1c80935655595b661505c8b39b664828b95"
dependencies = [
"generic-array",
"generic-array 0.14.5",
]
[[package]]
name = "block-padding"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5"
dependencies = [
"byte-tools",
]
[[package]]
@@ -290,6 +328,12 @@ version = "3.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899"
[[package]]
name = "byte-tools"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7"
[[package]]
name = "byteorder"
version = "1.4.3"
@@ -340,6 +384,25 @@ dependencies = [
"winapi",
]
[[package]]
name = "config"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54ad70579325f1a38ea4c13412b82241c5900700a69785d73e2736bd65a33f86"
dependencies = [
"async-trait",
"json5",
"lazy_static",
"nom",
"pathdiff",
"ron",
"rust-ini",
"serde",
"serde_json",
"toml",
"yaml-rust",
]
[[package]]
name = "configparser"
version = "3.0.0"
@@ -354,9 +417,9 @@ dependencies = [
"actix-web",
"anyhow",
"chrono",
"config",
"configparser",
"contextswitch-types",
"dotenv",
"http",
"lazy_static",
"listenfd",
@@ -446,7 +509,7 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d6b536309245c849479fba3da410962a43ed8e51c26b729208ec0ac2798d0"
dependencies = [
"generic-array",
"generic-array 0.14.5",
]
[[package]]
@@ -462,22 +525,34 @@ dependencies = [
"syn",
]
[[package]]
name = "digest"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5"
dependencies = [
"generic-array 0.12.4",
]
[[package]]
name = "digest"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b697d66081d42af4fba142d56918a3cb21dc8eb63372c6b85d14f44fb9c5979b"
dependencies = [
"block-buffer",
"block-buffer 0.10.0",
"crypto-common",
"generic-array",
"generic-array 0.14.5",
]
[[package]]
name = "dotenv"
version = "0.15.0"
name = "dlv-list"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
checksum = "68df3f2b690c1b86e65ef7830956aededf3cb0a16f898f79b9a6f421a7b6211b"
dependencies = [
"rand",
]
[[package]]
name = "encoding_rs"
@@ -488,6 +563,12 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "fake-simd"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"
[[package]]
name = "fastrand"
version = "1.6.0"
@@ -585,6 +666,15 @@ dependencies = [
"pin-utils",
]
[[package]]
name = "generic-array"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffdf9f34f1447443d37393cc6c2b8313aebddcd96906caf34e54c68d8e57d7bd"
dependencies = [
"typenum",
]
[[package]]
name = "generic-array"
version = "0.14.5"
@@ -635,6 +725,15 @@ dependencies = [
"tracing",
]
[[package]]
name = "hashbrown"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
dependencies = [
"ahash 0.4.7",
]
[[package]]
name = "hashbrown"
version = "0.11.2"
@@ -739,7 +838,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223"
dependencies = [
"autocfg",
"hashbrown",
"hashbrown 0.11.2",
]
[[package]]
@@ -787,6 +886,17 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "json5"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96b0db21af676c1ce64250b5f40f3ce2cf27e4e47cb91ed91eb6fe9350b430c1"
dependencies = [
"pest",
"pest_derive",
"serde",
]
[[package]]
name = "language-tags"
version = "0.3.2"
@@ -805,6 +915,12 @@ version = "0.2.112"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125"
[[package]]
name = "linked-hash-map"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
[[package]]
name = "listenfd"
version = "0.3.5"
@@ -852,6 +968,12 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "maplit"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d"
[[package]]
name = "matchers"
version = "0.1.0"
@@ -879,6 +1001,12 @@ version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[package]]
name = "minimal-lexical"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "miniz_oxide"
version = "0.4.4"
@@ -951,6 +1079,17 @@ dependencies = [
"tempfile",
]
[[package]]
name = "nom"
version = "7.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b1d11e1ef389c76fe5b81bcaf2ea32cf88b62bc494e19f493d0b30e7a930109"
dependencies = [
"memchr",
"minimal-lexical",
"version_check",
]
[[package]]
name = "ntapi"
version = "0.3.6"
@@ -995,6 +1134,12 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5"
[[package]]
name = "opaque-debug"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c"
[[package]]
name = "openssl"
version = "0.10.38"
@@ -1028,6 +1173,16 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "ordered-multimap"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c672c7ad9ec066e428c00eb917124a06f08db19e2584de982cc34b1f4c12485"
dependencies = [
"dlv-list",
"hashbrown 0.9.1",
]
[[package]]
name = "parking_lot"
version = "0.11.2"
@@ -1059,12 +1214,61 @@ version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0744126afe1a6dd7f394cb50a716dbe086cb06e255e53d8d0185d82828358fb5"
[[package]]
name = "pathdiff"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd"
[[package]]
name = "percent-encoding"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
[[package]]
name = "pest"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10f4872ae94d7b90ae48754df22fd42ad52ce740b8f370b03da4835417403e53"
dependencies = [
"ucd-trie",
]
[[package]]
name = "pest_derive"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "833d1ae558dc601e9a60366421196a8d94bc0ac980476d0b67e1d0988d72b2d0"
dependencies = [
"pest",
"pest_generator",
]
[[package]]
name = "pest_generator"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99b8db626e31e5b81787b9783425769681b347011cc59471e33ea46d2ea0cf55"
dependencies = [
"pest",
"pest_meta",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "pest_meta"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54be6e404f5317079812fc8f9f5279de376d8856929e21c184ecf6bbd692a11d"
dependencies = [
"maplit",
"pest",
"sha-1 0.8.2",
]
[[package]]
name = "pin-project"
version = "1.0.10"
@@ -1287,6 +1491,17 @@ dependencies = [
"winreg",
]
[[package]]
name = "ron"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b861ecaade43ac97886a512b360d01d66be9f41f3c61088b42cedf92e03d678"
dependencies = [
"base64",
"bitflags",
"serde",
]
[[package]]
name = "rstest"
version = "0.12.0"
@@ -1300,6 +1515,16 @@ dependencies = [
"syn",
]
[[package]]
name = "rust-ini"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63471c4aa97a1cf8332a5f97709a79a4234698de6a1f5087faf66f2dae810e22"
dependencies = [
"cfg-if",
"ordered-multimap",
]
[[package]]
name = "rustc_version"
version = "0.4.0"
@@ -1415,6 +1640,18 @@ dependencies = [
"serde",
]
[[package]]
name = "sha-1"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7d94d0bede923b3cea61f3f1ff57ff8cdfd77b400fb8f9998949e0cf04163df"
dependencies = [
"block-buffer 0.7.3",
"digest 0.8.1",
"fake-simd",
"opaque-debug",
]
[[package]]
name = "sha-1"
version = "0.10.0"
@@ -1423,7 +1660,7 @@ checksum = "028f48d513f9678cda28f6e4064755b3fbb2af6acd672f2c209b62323f7aea0f"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
"digest 0.10.1",
]
[[package]]
@@ -1616,6 +1853,15 @@ dependencies = [
"tokio",
]
[[package]]
name = "toml"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
dependencies = [
"serde",
]
[[package]]
name = "tower-service"
version = "0.3.1"
@@ -1749,6 +1995,12 @@ version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987"
[[package]]
name = "ucd-trie"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c"
[[package]]
name = "unicode-bidi"
version = "0.3.7"
@@ -1936,6 +2188,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "yaml-rust"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
dependencies = [
"linked-hash-map",
]
[[package]]
name = "zstd"
version = "0.9.2+zstd.1.5.1"

View File

@@ -22,7 +22,6 @@ uuid = { version = "0.8.0", features = ["serde"] }
chrono = { version = "0.4.0", features = ["serde"] }
mktemp = "0.4.0"
configparser = "3.0.0"
dotenv = "0.15.0"
listenfd = "0.3.0"
tracing = { version = "0.1.0", features = ["log"] }
tracing-subscriber = { version = "0.3.0", features = ["std", "env-filter", "fmt", "json"] }
@@ -34,6 +33,7 @@ tracing-bunyan-formatter = "0.3.0"
thiserror = "1.0"
anyhow = "1.0"
http = "0.2.0"
config = "0.12.0"
[dev-dependencies]
proptest = "1.0.0"

7
config/default.toml Normal file
View File

@@ -0,0 +1,7 @@
[application]
port = 8000
# See https://docs.rs/tracing-subscriber/latest/tracing_subscriber/struct.EnvFilter.html
log_directive = "info"
[taskwarrior]
data_location = "/tmp"

2
config/test.toml Normal file
View File

@@ -0,0 +1,2 @@
[application]
log_directive = "debug"

42
src/configuration.rs Normal file
View File

@@ -0,0 +1,42 @@
use config::{Config, ConfigError, Environment, File};
use serde::Deserialize;
use std::env;
#[derive(Deserialize)]
pub struct Settings {
pub application: ApplicationSettings,
pub taskwarrior: TaskwarriorSettings,
}
#[derive(Deserialize)]
pub struct ApplicationSettings {
pub port: u16,
pub log_directive: String,
}
#[derive(Deserialize)]
pub struct TaskwarriorSettings {
pub data_location: Option<String>,
pub taskrc: Option<String>,
}
impl Settings {
pub fn new_from_file(file: Option<String>) -> Result<Self, ConfigError> {
let config_file_required = file.is_some();
let config_file =
file.unwrap_or_else(|| env::var("CONFIG").unwrap_or_else(|_| "dev".into()));
let config = Config::builder()
.add_source(File::with_name("config/default"))
.add_source(File::with_name("config/local").required(false))
.add_source(File::with_name(&config_file).required(config_file_required))
.add_source(Environment::with_prefix("cs"))
.build()?;
config.try_deserialize()
}
pub fn new() -> Result<Self, ConfigError> {
Settings::new_from_file(None)
}
}

View File

@@ -1,3 +1,4 @@
use crate::configuration::TaskwarriorSettings;
use anyhow::{anyhow, Context};
use chrono::{DateTime, Utc};
use configparser::ini::Ini;
@@ -156,8 +157,8 @@ fn write_default_config(data_location: &str) -> String {
taskrc_location.into()
}
pub fn load_config(task_data_location: Option<&str>) -> String {
if let Ok(taskrc_location) = env::var("TASKRC") {
pub fn load_config(settings: &TaskwarriorSettings) -> String {
if let Some(taskrc_location) = &settings.taskrc {
let mut taskrc = Ini::new();
taskrc
.load(&taskrc_location)
@@ -168,6 +169,8 @@ pub fn load_config(task_data_location: Option<&str>) -> String {
taskrc_location
)
});
env::set_var("TASKRC", &taskrc_location);
debug!(
"Extracted data location `{}` from existing taskrc `{}`",
data_location, taskrc_location
@@ -175,12 +178,11 @@ pub fn load_config(task_data_location: Option<&str>) -> String {
data_location
} else {
let data_location = task_data_location
.map(|s| s.to_string())
.unwrap_or_else(|| {
env::var("TASK_DATA_LOCATION")
.expect("Expecting TASKRC or TASK_DATA_LOCATION environment variable value")
});
let data_location = settings
.data_location
.as_ref()
.expect("Expecting taskwarrior.taskrc or taskwarrior.data_location setting to be set")
.to_string();
let taskrc_location = write_default_config(&data_location);
env::set_var("TASKRC", &taskrc_location);

View File

@@ -7,6 +7,7 @@ use tracing_actix_web::TracingLogger;
#[macro_use]
extern crate lazy_static;
pub mod configuration;
pub mod contextswitch;
pub mod observability;
pub mod routes;

View File

@@ -1,22 +1,19 @@
extern crate dotenv;
extern crate listenfd;
use contextswitch_api::configuration::Settings;
use contextswitch_api::observability::{get_subscriber, init_subscriber};
use contextswitch_api::{contextswitch::taskwarrior, run};
use dotenv::dotenv;
use std::env;
use std::net::TcpListener;
#[tokio::main]
async fn main() -> std::io::Result<()> {
let subscriber = get_subscriber("info".into());
let settings = Settings::new().expect("Cannot load Contextswitch configuration");
let subscriber = get_subscriber(&settings.application.log_directive);
init_subscriber(subscriber);
dotenv().ok();
taskwarrior::load_config(&settings.taskwarrior);
let port = env::var("PORT").unwrap_or_else(|_| "8000".to_string());
taskwarrior::load_config(None);
let listener = TcpListener::bind(format!("0.0.0.0:{}", port)).expect("Failed to bind port");
let listener = TcpListener::bind(format!("0.0.0.0:{}", settings.application.port))
.expect("Failed to bind port");
run(listener)?.await
}

View File

@@ -4,7 +4,7 @@ use tracing_log::LogTracer;
use tracing_subscriber::fmt::TestWriter;
use tracing_subscriber::{layer::SubscriberExt, EnvFilter};
pub fn get_subscriber(env_filter_str: String) -> impl Subscriber + Send + Sync {
pub fn get_subscriber(env_filter_str: &str) -> impl Subscriber + Send + Sync {
let formatting_layer = BunyanFormattingLayer::new("contextswitch-api".into(), TestWriter::new);
let env_filter =

View File

@@ -1,3 +1,4 @@
use contextswitch_api::configuration::Settings;
use contextswitch_api::contextswitch::taskwarrior;
use contextswitch_api::observability::{get_subscriber, init_subscriber};
use mktemp::Temp;
@@ -5,9 +6,9 @@ use rstest::*;
use std::net::TcpListener;
use tracing::info;
fn setup_tracing() {
fn setup_tracing(settings: &Settings) {
info!("Setting up tracing");
let subscriber = get_subscriber("debug".to_string());
let subscriber = get_subscriber(&settings.application.log_directive);
init_subscriber(subscriber);
}
@@ -21,10 +22,11 @@ fn setup_server() -> String {
format!("http://127.0.0.1:{}", port)
}
fn setup_taskwarrior() -> String {
info!("Setting up TW");
fn setup_taskwarrior(mut settings: Settings) -> String {
info!("Setting up Taskwarrior");
let tmp_dir = Temp::new_dir().unwrap();
let task_data_location = taskwarrior::load_config(tmp_dir.to_str());
settings.taskwarrior.data_location = tmp_dir.to_str().map(String::from);
let task_data_location = taskwarrior::load_config(&settings.taskwarrior);
tmp_dir.release();
task_data_location
@@ -33,7 +35,9 @@ fn setup_taskwarrior() -> String {
#[fixture]
#[once]
pub fn app_address() -> String {
setup_tracing();
setup_taskwarrior();
let settings = Settings::new_from_file(Some("config/test".to_string()))
.expect("Cannot load test configuration");
setup_tracing(&settings);
setup_taskwarrior(settings);
setup_server()
}