More progress

This commit is contained in:
Half-Shot 2022-01-03 23:09:08 +00:00
parent f5919007da
commit fcb380cf19
6 changed files with 179 additions and 26 deletions

View File

@ -19,6 +19,5 @@ rgb = "0"
md-5 = "0.8.0"
hex = "0.4.3"
[build-dependencies]
napi-build = "1"

View File

@ -44,7 +44,7 @@
"ioredis": "^4.28.0",
"jira-client": "^8.0.0",
"markdown-it": "^12.2.0",
"matrix-bot-sdk": "^0.5.19",
"matrix-bot-sdk": "^v0.6.0-beta.3",
"matrix-widget-api": "^0.1.0-beta.17",
"micromatch": "^4.0.4",
"mime": "^3.0.0",

View File

@ -1,33 +1,74 @@
#[napi(object)]
#[derive(Serialize, Debug, Deserialize)]
#[derive(Serialize, Deserialize, Clone)]
pub struct BridgeConfigBot {
pub displayname: Option<String>,
pub avatar: Option<String>,
}
#[napi(object)]
#[derive(Serialize, Deserialize, Clone)]
pub struct BridgeConfigBridge {
pub domain: String,
pub url: String,
pub media_url: Option<String>,
pub port: u32,
pub bind_address: String,
// TODO: Ignoring pantalaimon, we'll use encrypted bridges when it lands.
}
#[napi(object)]
#[derive(Serialize, Deserialize, Clone)]
pub struct BridgeConfigLogging {
pub level: String,
}
#[napi(object)]
#[derive(Serialize, Debug, Deserialize)]
#[derive(Serialize, Deserialize)]
pub struct BridgeConfigLoggingYAML {
pub level: Option<String>,
}
#[napi(object)]
#[derive(Serialize, Debug, Deserialize)]
#[derive(Serialize, Deserialize, Clone)]
pub struct BridgeConfigMessageQueue {
pub monolithic: bool,
pub port: Option<u32>,
pub host: Option<String>,
}
#[napi(object)]
#[derive(Serialize, Deserialize, Clone)]
pub struct BridgeConfigListener {
pub port: u32,
pub bind_address: Option<String>,
pub resources: Vec<String>,
}
// Legacy yaml config sections
#[napi(object)]
#[derive(Serialize, Debug, Deserialize)]
pub struct BridgeConfig {
pub logging: BridgeConfigLogging,
#[derive(Serialize, Deserialize, Clone)]
pub struct BridgeConfigWebhook {
pub port: u32,
pub bind_address: Option<String>,
}
#[napi(object)]
#[derive(Serialize, Debug, Deserialize)]
#[derive(Serialize, Deserialize)]
pub struct BridgeConfigYAML {
pub bot: Option<BridgeConfigBot>,
pub bridge: BridgeConfigBridge,
pub listeners: Option<Vec<BridgeConfigListener>>,
pub logging: Option<BridgeConfigLoggingYAML>,
pub queue: Option<BridgeConfigMessageQueue>
pub queue: Option<BridgeConfigMessageQueue>,
pub webhook: Option<BridgeConfigWebhook>,
}
#[napi]
pub struct BridgeConfig {
pub bot: Option<BridgeConfigBot>,
pub bridge: BridgeConfigBridge,
pub logging: BridgeConfigLogging,
pub queue: Option<BridgeConfigMessageQueue>,
pub listeners: Vec<BridgeConfigListener>,
}

64
src/config/defaults.rs Normal file
View File

@ -0,0 +1,64 @@
use super::config::{*};
pub struct DefaultBridgeConfigItem<T> {
pub comment: String,
pub v: T,
}
pub struct DefaultBridgeConfig {
pub bot: DefaultBridgeConfigItem<BridgeConfigBot>,
pub bridge: DefaultBridgeConfigItem<BridgeConfigBridge>,
pub listeners: DefaultBridgeConfigItem<Vec<BridgeConfigListener>>,
pub logging: DefaultBridgeConfigItem<BridgeConfigLogging>,
pub queue: DefaultBridgeConfigItem<BridgeConfigMessageQueue>,
}
impl DefaultBridgeConfig {
pub fn new() -> Self {
DefaultBridgeConfig {
bot: DefaultBridgeConfigItem {
comment: "foo".to_string(),
v: BridgeConfigBot {
displayname: Some("Hookshot Bot".to_string()),
avatar: Some("mxc://example.com/foobar".to_string())
}
},
bridge: DefaultBridgeConfigItem {
comment: "ffoo".to_string(),
v: BridgeConfigBridge {
}
},
listeners: DefaultBridgeConfigItem {
comment: "ffoo".to_string(),
v: vec![BridgeConfigListener { }]
},
logging: DefaultBridgeConfigItem {
comment: "fooo".to_string(),
v: BridgeConfigLogging {
level: "info".to_string(),
}
},
queue: DefaultBridgeConfigItem {
comment: "fooo".to_string(),
v: BridgeConfigMessageQueue {
monolithic: true,
port: Some(6379),
host: Some("localhost".to_string()),
}
}
}
}
}
impl From<DefaultBridgeConfig> for BridgeConfig {
fn from(d: DefaultBridgeConfig) -> Self {
BridgeConfig {
bot: Some(d.bot.v),
bridge: d.bridge.v,
logging: d.logging.v,
queue: Some(d.queue.v),
listeners: d.listeners.v,
}
}
}

View File

@ -2,9 +2,11 @@ use std::fs::File;
use std::collections::HashMap;
use napi::{Error, Status};
use self::config::{BridgeConfig, BridgeConfigLogging, BridgeConfigYAML, BridgeConfigLoggingYAML};
use self::config::{*};
pub mod config;
pub mod defaults;
#[napi]
impl BridgeConfig {
@ -30,7 +32,6 @@ impl BridgeConfig {
Ok(o) => BridgeConfig::new(o, env),
Err(e) => Err(e)
}
}
#[napi(constructor)]
@ -39,14 +40,27 @@ impl BridgeConfig {
level: Some("info".to_string())
});
let mut config = BridgeConfig {
bot: config_yaml.bot,
bridge: config_yaml.bridge,
logging: BridgeConfigLogging {
level: logging_yaml.level.unwrap_or("info".to_string())
}
level: logging_yaml.level.unwrap_or_else(|| "info".to_string())
},
queue: config_yaml.queue,
listeners: config_yaml.listeners.unwrap_or_else(|| Vec::default())
};
match env.get("LOG_LEVEL") {
Some(log_level) => config.logging.level = log_level.to_string(),
None => { }
}
// Handle legacy 'webhook' config
match config_yaml.webhook {
Some(webhook_cfg) => config.listeners.push( BridgeConfigListener {
bind_address: webhook_cfg.bind_address,
port: webhook_cfg.port,
resources: vec!("webhooks".to_string()),
}),
None => { }
}
Ok(config)
}
}
@ -54,12 +68,11 @@ impl BridgeConfig {
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use crate::config::config::{BridgeConfig};
use crate::config::{defaults::DefaultBridgeConfig, config::BridgeConfig};
#[test]
fn it_works() {
let cfg = BridgeConfig::parse_config("./config.sample.yml".to_string(), HashMap::new()).unwrap();
fn test_sample_config() {
let cfg: BridgeConfig = DefaultBridgeConfig::new().into();
assert_eq!(cfg.logging.level, "info");
}
}

View File

@ -762,6 +762,14 @@
resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.2.tgz#423c77877d0569db20e1fc80885ac4118314010e"
integrity sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==
"@turt2live/matrix-sdk-crypto-nodejs@^0.1.0-beta.7":
version "0.1.0-beta.7"
resolved "https://registry.yarnpkg.com/@turt2live/matrix-sdk-crypto-nodejs/-/matrix-sdk-crypto-nodejs-0.1.0-beta.7.tgz#624b8e407eebe0a3866c2092c35dcff1d8ce492b"
integrity sha512-V2VMXGzC3cLdyf8duMN2UJd4Y5m3KSyLgjuL9Xo3QZHjeqzIDDMQtsUAwhgMVwQz83tLHfo0QILXSr8N33AVAA==
dependencies:
"@napi-rs/cli" "^2.2.0"
shelljs "^0.8.4"
"@types/body-parser@*":
version "1.19.2"
resolved "https://registry.yarnpkg.com/@types/body-parser/-/body-parser-1.19.2.tgz#aea2059e28b7658639081347ac4fab3de166e6f0"
@ -836,7 +844,7 @@
"@types/qs" "*"
"@types/range-parser" "*"
"@types/express@^4.17.13", "@types/express@^4.17.7":
"@types/express@^4.17.13":
version "4.17.13"
resolved "https://registry.yarnpkg.com/@types/express/-/express-4.17.13.tgz#a76e2995728999bab51a33fabce1d705a3709034"
integrity sha512-6bSZTPaTIACxn48l50SR+axgrqm6qXFIxrdAKaG6PaJk3+zuUr35hBlgT7vOmJcum+OEaIBLtHV/qloEAFITeA==
@ -1144,6 +1152,11 @@ ajv@^6.10.0, ajv@^6.12.3, ajv@^6.12.4:
json-schema-traverse "^0.4.1"
uri-js "^4.2.2"
another-json@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/another-json/-/another-json-0.2.0.tgz#b5f4019c973b6dd5c6506a2d93469cb6d32aeedc"
integrity sha1-tfQBnJc7bdXGUGotk0acttMq7tw=
ansi-colors@4.1.1, ansi-colors@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/ansi-colors/-/ansi-colors-4.1.1.tgz#cbb9ae256bf750af1eab344f229aa27fe94ba348"
@ -2673,7 +2686,7 @@ glob@7.1.6:
once "^1.3.0"
path-is-absolute "^1.0.0"
glob@^7.1.3, glob@^7.1.4, glob@^7.1.6, glob@^7.1.7:
glob@^7.0.0, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6, glob@^7.1.7:
version "7.2.0"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.2.0.tgz#d15535af7732e02e948f4c41628bd910293f6023"
integrity sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==
@ -2985,6 +2998,11 @@ inherits@2.0.1:
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.1.tgz#b17d08d326b4423e568eff719f91b0b1cbdf69f1"
integrity sha1-sX0I0ya0Qj5Wjv9xn5GwscvfafE=
interpret@^1.0.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.4.0.tgz#665ab8bc4da27a774a40584e812e3e0fa45b1a1e"
integrity sha512-agE4QfB2Lkp9uICn7BAqoscw4SZP9kTE2hxiFI3jBPmXJfdqiahTbUuKGsMoN2GtqL9AxhYioAcVvgsb1HvRbA==
ioredis@^4.28.0:
version "4.28.2"
resolved "https://registry.yarnpkg.com/ioredis/-/ioredis-4.28.2.tgz#493ccd5d869fd0ec86c96498192718171f6c9203"
@ -3528,12 +3546,14 @@ markdown-it@^12.2.0:
mdurl "^1.0.1"
uc.micro "^1.0.5"
matrix-bot-sdk@^0.5.19:
version "0.5.19"
resolved "https://registry.yarnpkg.com/matrix-bot-sdk/-/matrix-bot-sdk-0.5.19.tgz#6ce13359ab53ea0af9dc3ebcbe288c5f6d9c02c6"
integrity sha512-RIPyvQPkOVp2yTKeDgp5rcn6z/DiKdHb6E8c69K+utai8ypRGtfDRj0PGqP+1XzqC9Wb1OFrESCUB5t0ffdC9g==
matrix-bot-sdk@^v0.6.0-beta.3:
version "0.6.0-beta.3"
resolved "https://registry.yarnpkg.com/matrix-bot-sdk/-/matrix-bot-sdk-0.6.0-beta.3.tgz#c9d5c6f12321b32a07779563486e76d499c17e95"
integrity sha512-HS++Djd2m57Dq0vCoHQngR433Z/hZ31kOzF/BGuIpgJmlyhjgdTnaR1KN+6FEj1HMx0usnKD97ZJ3sVo2RZTHg==
dependencies:
"@types/express" "^4.17.7"
"@turt2live/matrix-sdk-crypto-nodejs" "^0.1.0-beta.7"
"@types/express" "^4.17.13"
another-json "^0.2.0"
chalk "^4.1.0"
express "^4.17.1"
glob-to-regexp "^0.4.1"
@ -4523,6 +4543,13 @@ readdirp@~3.6.0:
dependencies:
picomatch "^2.2.1"
rechoir@^0.6.2:
version "0.6.2"
resolved "https://registry.yarnpkg.com/rechoir/-/rechoir-0.6.2.tgz#85204b54dba82d5742e28c96756ef43af50e3384"
integrity sha1-hSBLVNuoLVdC4oyWdW70OvUOM4Q=
dependencies:
resolve "^1.1.6"
redis-commands@1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/redis-commands/-/redis-commands-1.7.0.tgz#15a6fea2d58281e27b1cd1acfb4b293e278c3a89"
@ -4618,7 +4645,7 @@ resolve-from@^5.0.0:
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-5.0.0.tgz#c35225843df8f776df21c57557bc087e9dfdfc69"
integrity sha512-qYg9KP24dD5qka9J47d0aVky0N+b4fTU89LN9iDnjB5waksiC49rvMB0PrUJQGoTmH50XPiqOvAjDfaijGxYZw==
resolve@^1.17.0, resolve@^1.20.0:
resolve@^1.1.6, resolve@^1.17.0, resolve@^1.20.0:
version "1.20.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.20.0.tgz#629a013fb3f70755d6f0b7935cc1c2c5378b1975"
integrity sha512-wENBPt4ySzg4ybFQW2TT1zMQucPK95HSh/nq2CFTZVOGut2+pQvSsgtda4d26YrYcr067wjbmzOG8byDPBX63A==
@ -4785,6 +4812,15 @@ shebang-regex@^3.0.0:
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-3.0.0.tgz#ae16f1644d873ecad843b0307b143362d4c42172"
integrity sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==
shelljs@^0.8.4:
version "0.8.4"
resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.8.4.tgz#de7684feeb767f8716b326078a8a00875890e3c2"
integrity sha512-7gk3UZ9kOfPLIAbslLzyWeGiEqx9e3rxwZM0KE6EL8GlGwjym9Mrlx5/p33bWTu9YG6vcS4MBxYZDHYr5lr8BQ==
dependencies:
glob "^7.0.0"
interpret "^1.0.0"
rechoir "^0.6.2"
signal-exit@^3.0.0, signal-exit@^3.0.2, signal-exit@^3.0.3:
version "3.0.6"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.6.tgz#24e630c4b0f03fea446a2bd299e62b4a6ca8d0af"