Matrix commited on
Commit
9ce120f
·
1 Parent(s): 697a86c

chore: use config crate to initial settings

Browse files
Files changed (4) hide show
  1. Cargo.lock +179 -3
  2. Cargo.toml +2 -0
  3. config.yaml → config/dev.yaml +0 -0
  4. src/config.rs +28 -15
Cargo.lock CHANGED
@@ -17,6 +17,17 @@ version = "1.0.2"
17
  source = "registry+https://github.com/rust-lang/crates.io-index"
18
  checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
19
 
 
 
 
 
 
 
 
 
 
 
 
20
  [[package]]
21
  name = "aho-corasick"
22
  version = "1.1.2"
@@ -26,6 +37,12 @@ dependencies = [
26
  "memchr",
27
  ]
28
 
 
 
 
 
 
 
29
  [[package]]
30
  name = "async-stream"
31
  version = "0.3.5"
@@ -491,6 +508,12 @@ dependencies = [
491
  "rustc-demangle",
492
  ]
493
 
 
 
 
 
 
 
494
  [[package]]
495
  name = "base64"
496
  version = "0.21.5"
@@ -613,6 +636,25 @@ dependencies = [
613
  "libloading",
614
  ]
615
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
616
  [[package]]
617
  name = "core-foundation"
618
  version = "0.9.3"
@@ -683,6 +725,12 @@ dependencies = [
683
  "subtle",
684
  ]
685
 
 
 
 
 
 
 
686
  [[package]]
687
  name = "either"
688
  version = "1.9.0"
@@ -836,6 +884,9 @@ name = "hashbrown"
836
  version = "0.12.3"
837
  source = "registry+https://github.com/rust-lang/crates.io-index"
838
  checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
 
 
 
839
 
840
  [[package]]
841
  name = "hashbrown"
@@ -849,7 +900,7 @@ version = "0.3.9"
849
  source = "registry+https://github.com/rust-lang/crates.io-index"
850
  checksum = "06683b93020a07e3dbcf5f8c0f6d40080d725bea7936fc01ad345c01b97dc270"
851
  dependencies = [
852
- "base64",
853
  "bytes",
854
  "headers-core",
855
  "http",
@@ -1016,6 +1067,17 @@ dependencies = [
1016
  "wasm-bindgen",
1017
  ]
1018
 
 
 
 
 
 
 
 
 
 
 
 
1019
  [[package]]
1020
  name = "lazy_static"
1021
  version = "1.4.0"
@@ -1044,6 +1106,12 @@ dependencies = [
1044
  "winapi",
1045
  ]
1046
 
 
 
 
 
 
 
1047
  [[package]]
1048
  name = "linux-raw-sys"
1049
  version = "0.4.10"
@@ -1184,6 +1252,16 @@ version = "0.1.5"
1184
  source = "registry+https://github.com/rust-lang/crates.io-index"
1185
  checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
1186
 
 
 
 
 
 
 
 
 
 
 
1187
  [[package]]
1188
  name = "outref"
1189
  version = "0.5.1"
@@ -1219,6 +1297,12 @@ dependencies = [
1219
  "windows-targets",
1220
  ]
1221
 
 
 
 
 
 
 
1222
  [[package]]
1223
  name = "peeking_take_while"
1224
  version = "0.1.2"
@@ -1231,6 +1315,51 @@ version = "2.3.0"
1231
  source = "registry+https://github.com/rust-lang/crates.io-index"
1232
  checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94"
1233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1234
  [[package]]
1235
  name = "pin-project"
1236
  version = "1.1.3"
@@ -1270,7 +1399,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1270
  checksum = "ebc7ae19f3e791ae8108b08801abb3708d64d3a16490c720e0b81040cae87b5d"
1271
  dependencies = [
1272
  "async-trait",
1273
- "base64",
1274
  "bytes",
1275
  "futures-util",
1276
  "headers",
@@ -1312,11 +1441,13 @@ dependencies = [
1312
  name = "polyhedron"
1313
  version = "0.1.0"
1314
  dependencies = [
 
1315
  "async-stream",
1316
  "aws-config",
1317
  "aws-sdk-polly",
1318
  "aws-sdk-transcribestreaming",
1319
  "aws-sdk-translate",
 
1320
  "futures-util",
1321
  "lazy_static",
1322
  "poem",
@@ -1477,6 +1608,27 @@ dependencies = [
1477
  "windows-sys",
1478
  ]
1479
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1480
  [[package]]
1481
  name = "rustc-demangle"
1482
  version = "0.1.23"
@@ -1541,7 +1693,7 @@ version = "1.0.3"
1541
  source = "registry+https://github.com/rust-lang/crates.io-index"
1542
  checksum = "2d3987094b1d07b653b7dfdc3f70ce9a1da9c51ac18c1b06b662e4f9a0e9f4b2"
1543
  dependencies = [
1544
- "base64",
1545
  ]
1546
 
1547
  [[package]]
@@ -1930,6 +2082,15 @@ dependencies = [
1930
  "tracing",
1931
  ]
1932
 
 
 
 
 
 
 
 
 
 
1933
  [[package]]
1934
  name = "toml_datetime"
1935
  version = "0.6.5"
@@ -2064,6 +2225,12 @@ version = "1.17.0"
2064
  source = "registry+https://github.com/rust-lang/crates.io-index"
2065
  checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
2066
 
 
 
 
 
 
 
2067
  [[package]]
2068
  name = "uncased"
2069
  version = "0.9.9"
@@ -2381,6 +2548,15 @@ version = "0.13.6"
2381
  source = "registry+https://github.com/rust-lang/crates.io-index"
2382
  checksum = "66fee0b777b0f5ac1c69bb06d361268faafa61cd4682ae064a171c16c433e9e4"
2383
 
 
 
 
 
 
 
 
 
 
2384
  [[package]]
2385
  name = "zeroize"
2386
  version = "1.6.0"
 
17
  source = "registry+https://github.com/rust-lang/crates.io-index"
18
  checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
19
 
20
+ [[package]]
21
+ name = "ahash"
22
+ version = "0.7.7"
23
+ source = "registry+https://github.com/rust-lang/crates.io-index"
24
+ checksum = "5a824f2aa7e75a0c98c5a504fceb80649e9c35265d44525b5f94de4771a395cd"
25
+ dependencies = [
26
+ "getrandom",
27
+ "once_cell",
28
+ "version_check",
29
+ ]
30
+
31
  [[package]]
32
  name = "aho-corasick"
33
  version = "1.1.2"
 
37
  "memchr",
38
  ]
39
 
40
+ [[package]]
41
+ name = "anyhow"
42
+ version = "1.0.75"
43
+ source = "registry+https://github.com/rust-lang/crates.io-index"
44
+ checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
45
+
46
  [[package]]
47
  name = "async-stream"
48
  version = "0.3.5"
 
508
  "rustc-demangle",
509
  ]
510
 
511
+ [[package]]
512
+ name = "base64"
513
+ version = "0.13.1"
514
+ source = "registry+https://github.com/rust-lang/crates.io-index"
515
+ checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
516
+
517
  [[package]]
518
  name = "base64"
519
  version = "0.21.5"
 
636
  "libloading",
637
  ]
638
 
639
+ [[package]]
640
+ name = "config"
641
+ version = "0.13.3"
642
+ source = "registry+https://github.com/rust-lang/crates.io-index"
643
+ checksum = "d379af7f68bfc21714c6c7dea883544201741d2ce8274bb12fa54f89507f52a7"
644
+ dependencies = [
645
+ "async-trait",
646
+ "json5",
647
+ "lazy_static",
648
+ "nom",
649
+ "pathdiff",
650
+ "ron",
651
+ "rust-ini",
652
+ "serde",
653
+ "serde_json",
654
+ "toml",
655
+ "yaml-rust",
656
+ ]
657
+
658
  [[package]]
659
  name = "core-foundation"
660
  version = "0.9.3"
 
725
  "subtle",
726
  ]
727
 
728
+ [[package]]
729
+ name = "dlv-list"
730
+ version = "0.3.0"
731
+ source = "registry+https://github.com/rust-lang/crates.io-index"
732
+ checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257"
733
+
734
  [[package]]
735
  name = "either"
736
  version = "1.9.0"
 
884
  version = "0.12.3"
885
  source = "registry+https://github.com/rust-lang/crates.io-index"
886
  checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
887
+ dependencies = [
888
+ "ahash",
889
+ ]
890
 
891
  [[package]]
892
  name = "hashbrown"
 
900
  source = "registry+https://github.com/rust-lang/crates.io-index"
901
  checksum = "06683b93020a07e3dbcf5f8c0f6d40080d725bea7936fc01ad345c01b97dc270"
902
  dependencies = [
903
+ "base64 0.21.5",
904
  "bytes",
905
  "headers-core",
906
  "http",
 
1067
  "wasm-bindgen",
1068
  ]
1069
 
1070
+ [[package]]
1071
+ name = "json5"
1072
+ version = "0.4.1"
1073
+ source = "registry+https://github.com/rust-lang/crates.io-index"
1074
+ checksum = "96b0db21af676c1ce64250b5f40f3ce2cf27e4e47cb91ed91eb6fe9350b430c1"
1075
+ dependencies = [
1076
+ "pest",
1077
+ "pest_derive",
1078
+ "serde",
1079
+ ]
1080
+
1081
  [[package]]
1082
  name = "lazy_static"
1083
  version = "1.4.0"
 
1106
  "winapi",
1107
  ]
1108
 
1109
+ [[package]]
1110
+ name = "linked-hash-map"
1111
+ version = "0.5.6"
1112
+ source = "registry+https://github.com/rust-lang/crates.io-index"
1113
+ checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
1114
+
1115
  [[package]]
1116
  name = "linux-raw-sys"
1117
  version = "0.4.10"
 
1252
  source = "registry+https://github.com/rust-lang/crates.io-index"
1253
  checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
1254
 
1255
+ [[package]]
1256
+ name = "ordered-multimap"
1257
+ version = "0.4.3"
1258
+ source = "registry+https://github.com/rust-lang/crates.io-index"
1259
+ checksum = "ccd746e37177e1711c20dd619a1620f34f5c8b569c53590a72dedd5344d8924a"
1260
+ dependencies = [
1261
+ "dlv-list",
1262
+ "hashbrown 0.12.3",
1263
+ ]
1264
+
1265
  [[package]]
1266
  name = "outref"
1267
  version = "0.5.1"
 
1297
  "windows-targets",
1298
  ]
1299
 
1300
+ [[package]]
1301
+ name = "pathdiff"
1302
+ version = "0.2.1"
1303
+ source = "registry+https://github.com/rust-lang/crates.io-index"
1304
+ checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd"
1305
+
1306
  [[package]]
1307
  name = "peeking_take_while"
1308
  version = "0.1.2"
 
1315
  source = "registry+https://github.com/rust-lang/crates.io-index"
1316
  checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94"
1317
 
1318
+ [[package]]
1319
+ name = "pest"
1320
+ version = "2.7.5"
1321
+ source = "registry+https://github.com/rust-lang/crates.io-index"
1322
+ checksum = "ae9cee2a55a544be8b89dc6848072af97a20f2422603c10865be2a42b580fff5"
1323
+ dependencies = [
1324
+ "memchr",
1325
+ "thiserror",
1326
+ "ucd-trie",
1327
+ ]
1328
+
1329
+ [[package]]
1330
+ name = "pest_derive"
1331
+ version = "2.7.5"
1332
+ source = "registry+https://github.com/rust-lang/crates.io-index"
1333
+ checksum = "81d78524685f5ef2a3b3bd1cafbc9fcabb036253d9b1463e726a91cd16e2dfc2"
1334
+ dependencies = [
1335
+ "pest",
1336
+ "pest_generator",
1337
+ ]
1338
+
1339
+ [[package]]
1340
+ name = "pest_generator"
1341
+ version = "2.7.5"
1342
+ source = "registry+https://github.com/rust-lang/crates.io-index"
1343
+ checksum = "68bd1206e71118b5356dae5ddc61c8b11e28b09ef6a31acbd15ea48a28e0c227"
1344
+ dependencies = [
1345
+ "pest",
1346
+ "pest_meta",
1347
+ "proc-macro2",
1348
+ "quote",
1349
+ "syn 2.0.39",
1350
+ ]
1351
+
1352
+ [[package]]
1353
+ name = "pest_meta"
1354
+ version = "2.7.5"
1355
+ source = "registry+https://github.com/rust-lang/crates.io-index"
1356
+ checksum = "7c747191d4ad9e4a4ab9c8798f1e82a39affe7ef9648390b7e5548d18e099de6"
1357
+ dependencies = [
1358
+ "once_cell",
1359
+ "pest",
1360
+ "sha2",
1361
+ ]
1362
+
1363
  [[package]]
1364
  name = "pin-project"
1365
  version = "1.1.3"
 
1399
  checksum = "ebc7ae19f3e791ae8108b08801abb3708d64d3a16490c720e0b81040cae87b5d"
1400
  dependencies = [
1401
  "async-trait",
1402
+ "base64 0.21.5",
1403
  "bytes",
1404
  "futures-util",
1405
  "headers",
 
1441
  name = "polyhedron"
1442
  version = "0.1.0"
1443
  dependencies = [
1444
+ "anyhow",
1445
  "async-stream",
1446
  "aws-config",
1447
  "aws-sdk-polly",
1448
  "aws-sdk-transcribestreaming",
1449
  "aws-sdk-translate",
1450
+ "config",
1451
  "futures-util",
1452
  "lazy_static",
1453
  "poem",
 
1608
  "windows-sys",
1609
  ]
1610
 
1611
+ [[package]]
1612
+ name = "ron"
1613
+ version = "0.7.1"
1614
+ source = "registry+https://github.com/rust-lang/crates.io-index"
1615
+ checksum = "88073939a61e5b7680558e6be56b419e208420c2adb92be54921fa6b72283f1a"
1616
+ dependencies = [
1617
+ "base64 0.13.1",
1618
+ "bitflags 1.3.2",
1619
+ "serde",
1620
+ ]
1621
+
1622
+ [[package]]
1623
+ name = "rust-ini"
1624
+ version = "0.18.0"
1625
+ source = "registry+https://github.com/rust-lang/crates.io-index"
1626
+ checksum = "f6d5f2436026b4f6e79dc829837d467cc7e9a55ee40e750d716713540715a2df"
1627
+ dependencies = [
1628
+ "cfg-if",
1629
+ "ordered-multimap",
1630
+ ]
1631
+
1632
  [[package]]
1633
  name = "rustc-demangle"
1634
  version = "0.1.23"
 
1693
  source = "registry+https://github.com/rust-lang/crates.io-index"
1694
  checksum = "2d3987094b1d07b653b7dfdc3f70ce9a1da9c51ac18c1b06b662e4f9a0e9f4b2"
1695
  dependencies = [
1696
+ "base64 0.21.5",
1697
  ]
1698
 
1699
  [[package]]
 
2082
  "tracing",
2083
  ]
2084
 
2085
+ [[package]]
2086
+ name = "toml"
2087
+ version = "0.5.11"
2088
+ source = "registry+https://github.com/rust-lang/crates.io-index"
2089
+ checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234"
2090
+ dependencies = [
2091
+ "serde",
2092
+ ]
2093
+
2094
  [[package]]
2095
  name = "toml_datetime"
2096
  version = "0.6.5"
 
2225
  source = "registry+https://github.com/rust-lang/crates.io-index"
2226
  checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
2227
 
2228
+ [[package]]
2229
+ name = "ucd-trie"
2230
+ version = "0.1.6"
2231
+ source = "registry+https://github.com/rust-lang/crates.io-index"
2232
+ checksum = "ed646292ffc8188ef8ea4d1e0e0150fb15a5c2e12ad9b8fc191ae7a8a7f3c4b9"
2233
+
2234
  [[package]]
2235
  name = "uncased"
2236
  version = "0.9.9"
 
2548
  source = "registry+https://github.com/rust-lang/crates.io-index"
2549
  checksum = "66fee0b777b0f5ac1c69bb06d361268faafa61cd4682ae064a171c16c433e9e4"
2550
 
2551
+ [[package]]
2552
+ name = "yaml-rust"
2553
+ version = "0.4.5"
2554
+ source = "registry+https://github.com/rust-lang/crates.io-index"
2555
+ checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
2556
+ dependencies = [
2557
+ "linked-hash-map",
2558
+ ]
2559
+
2560
  [[package]]
2561
  name = "zeroize"
2562
  version = "1.6.0"
Cargo.toml CHANGED
@@ -4,11 +4,13 @@ version = "0.1.0"
4
  edition = "2021"
5
 
6
  [dependencies]
 
7
  async-stream = "0.3"
8
  aws-config = "0.56"
9
  aws-sdk-transcribestreaming = "0.34"
10
  aws-sdk-translate = "0.34"
11
  aws-sdk-polly = "0.34"
 
12
  futures-util = "0.3"
13
  lazy_static = "1.4"
14
  serde = { version = "1.0", features = ["derive"] }
 
4
  edition = "2021"
5
 
6
  [dependencies]
7
+ anyhow = "1.0"
8
  async-stream = "0.3"
9
  aws-config = "0.56"
10
  aws-sdk-transcribestreaming = "0.34"
11
  aws-sdk-translate = "0.34"
12
  aws-sdk-polly = "0.34"
13
+ config = "0.13"
14
  futures-util = "0.3"
15
  lazy_static = "1.4"
16
  serde = { version = "1.0", features = ["derive"] }
config.yaml → config/dev.yaml RENAMED
File without changes
src/config.rs CHANGED
@@ -1,7 +1,8 @@
 
 
 
1
  use lazy_static::lazy_static;
2
  use serde::Deserialize;
3
- use std::ffi::c_int;
4
- use std::net::IpAddr;
5
  use whisper_rs::FullParams;
6
 
7
  #[derive(Debug)]
@@ -10,15 +11,15 @@ pub enum Error {
10
  ConfigError(serde_yaml::Error),
11
  }
12
 
13
- pub(crate) fn load_config() -> Result<Config, Error> {
14
- let config_str = std::fs::read_to_string("config.yaml").map_err(|e| Error::IoError(e))?;
15
- let config: Config =
16
  serde_yaml::from_str(config_str.as_str()).map_err(|e| Error::ConfigError(e))?;
17
  return Ok(config);
18
  }
19
 
20
  lazy_static! {
21
- pub static ref CONFIG: Config = load_config().expect("failed to load config");
22
  }
23
 
24
  #[derive(Debug, Deserialize, Clone)]
@@ -83,19 +84,31 @@ pub(crate) struct Server {
83
  }
84
 
85
  #[derive(Debug, Deserialize)]
86
- pub struct Config {
87
  pub(crate) whisper: WhisperConfig,
88
  pub(crate) server: Server,
89
  }
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  mod tests {
92
- #[tokio::test]
93
- async fn load() {
94
- let config_str = tokio::fs::read_to_string("config.yaml")
95
- .await
96
- .expect("failed to read config file");
97
- let params: crate::config::Config =
98
- serde_yaml::from_str(config_str.as_str()).expect("failed to parse config file");
99
- println!("{:?}", params);
100
  }
101
  }
 
1
+ use std::{ffi::c_int, net::IpAddr, env};
2
+
3
+ use config::{Environment, Config, File};
4
  use lazy_static::lazy_static;
5
  use serde::Deserialize;
 
 
6
  use whisper_rs::FullParams;
7
 
8
  #[derive(Debug)]
 
11
  ConfigError(serde_yaml::Error),
12
  }
13
 
14
+ pub(crate) fn load_config() -> Result<Settings, Error> {
15
+ let config_str = std::fs::read_to_string("../config/dev.yaml").map_err(|e| Error::IoError(e))?;
16
+ let config: Settings =
17
  serde_yaml::from_str(config_str.as_str()).map_err(|e| Error::ConfigError(e))?;
18
  return Ok(config);
19
  }
20
 
21
  lazy_static! {
22
+ pub static ref CONFIG: Settings = load_config().expect("failed to load config");
23
  }
24
 
25
  #[derive(Debug, Deserialize, Clone)]
 
84
  }
85
 
86
  #[derive(Debug, Deserialize)]
87
+ pub struct Settings {
88
  pub(crate) whisper: WhisperConfig,
89
  pub(crate) server: Server,
90
  }
91
 
92
+ impl Settings {
93
+ pub(crate) fn new() -> Result<Self, anyhow::Error> {
94
+ let run_mode = env::var("APP_RUN_MODE").unwrap_or("dev".into());
95
+ let config = Config::builder()
96
+ .add_source(File::with_name(&format!("config/{run_mode}.yaml")).required(false))
97
+ .add_source(Environment::with_prefix("APP").separator("-"))
98
+ .build()
99
+ .map_err(anyhow::Error::from)?;
100
+
101
+ config.try_deserialize::<Self>().map_err(Into::into)
102
+ }
103
+ }
104
+
105
+ #[cfg(test)]
106
  mod tests {
107
+ use super::*;
108
+
109
+ #[test]
110
+ fn load_dev_settings_should_success() {
111
+ let settings = Settings::new().unwrap();
112
+ println!("{:?}", settings);
 
 
113
  }
114
  }