Initial commit
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
[alias]
|
||||
check-all = "check --workspace"
|
||||
test-all = "test --workspace"
|
||||
Generated
+423
@@ -0,0 +1,423 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.104"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470"
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "1.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600"
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.191"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c"
|
||||
|
||||
[[package]]
|
||||
name = "matchers"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9"
|
||||
dependencies = [
|
||||
"regex-automata",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
||||
|
||||
[[package]]
|
||||
name = "mio"
|
||||
version = "1.2.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "20521a903f02b0a573cd6840e9d0a5a0cc043d361fe924092cd70f8556cc9c4a"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"wasi",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mm-bridge"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"mm-core",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mm-client"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"mm-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mm-core"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mm-store"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"mm-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mm-web"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"mm-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nu-ansi-term"
|
||||
version = "0.50.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
|
||||
dependencies = [
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.21.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.107"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.47"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.229"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_core"
|
||||
version = "1.0.229"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.229"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 3.0.10",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.154"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"memchr",
|
||||
"serde",
|
||||
"serde_core",
|
||||
"zmij",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sharded-slab"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.16.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "149309498a5f7334b12ddae0cad8aba81ce785fe91755068867e017cf98fee46"
|
||||
|
||||
[[package]]
|
||||
name = "socket2"
|
||||
version = "0.6.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.119"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "3.0.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.69"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.69"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.119",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "1.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1ad99c4c6d32803332c548b1af0540b357b3f5fc0be8f6c6bfe8b2e6ae784070"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio"
|
||||
version = "1.54.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b52fd3255258d26fac7264ab5a76b53c13367136dc011d3d6ad4f8ed070396a3"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"libc",
|
||||
"mio",
|
||||
"pin-project-lite",
|
||||
"socket2",
|
||||
"tokio-macros",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-macros"
|
||||
version = "2.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 3.0.10",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing"
|
||||
version = "0.1.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100"
|
||||
dependencies = [
|
||||
"pin-project-lite",
|
||||
"tracing-attributes",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-attributes"
|
||||
version = "0.1.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.119",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-core"
|
||||
version = "0.1.36"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"valuable",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-log"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3"
|
||||
dependencies = [
|
||||
"log",
|
||||
"once_cell",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-subscriber"
|
||||
version = "0.3.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319"
|
||||
dependencies = [
|
||||
"matchers",
|
||||
"nu-ansi-term",
|
||||
"once_cell",
|
||||
"regex-automata",
|
||||
"sharded-slab",
|
||||
"smallvec",
|
||||
"thread_local",
|
||||
"tracing",
|
||||
"tracing-core",
|
||||
"tracing-log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
|
||||
|
||||
[[package]]
|
||||
name = "valuable"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.1+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.61.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zmij"
|
||||
version = "1.0.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
|
||||
@@ -0,0 +1,28 @@
|
||||
[workspace]
|
||||
resolver = "2"
|
||||
members = [
|
||||
"crates/mm-core",
|
||||
"crates/mm-client",
|
||||
"crates/mm-store",
|
||||
"crates/mm-web",
|
||||
"crates/mm-bridge",
|
||||
]
|
||||
|
||||
[workspace.package]
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "GPL-3.0-or-later"
|
||||
|
||||
[workspace.dependencies]
|
||||
anyhow = "1.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
thiserror = "1.0"
|
||||
tokio = { version = "1.40", features = ["macros", "rt-multi-thread", "net", "io-util", "sync", "time"] }
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] }
|
||||
|
||||
mm-core = { path = "crates/mm-core" }
|
||||
mm-client = { path = "crates/mm-client" }
|
||||
mm-store = { path = "crates/mm-store" }
|
||||
mm-web = { path = "crates/mm-web" }
|
||||
@@ -0,0 +1,18 @@
|
||||
[package]
|
||||
name = "mm-bridge"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[[bin]]
|
||||
name = "mm-bridge"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
mm-core.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
tokio.workspace = true
|
||||
tracing.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
@@ -0,0 +1,67 @@
|
||||
use crate::{protocol, status};
|
||||
use mm_core::{BridgeError, ErrorKind};
|
||||
|
||||
pub fn handle_request(request: protocol::Request) -> protocol::Response {
|
||||
match request.method.as_str() {
|
||||
"ping" => protocol::Response::ok(request.id, protocol::pong_result()),
|
||||
"status" => protocol::Response::ok(request.id, status::status_result()),
|
||||
_ => protocol::Response::err(
|
||||
request.id,
|
||||
BridgeError::new(
|
||||
ErrorKind::UnknownMethod,
|
||||
format!("unknown method: {}", request.method),
|
||||
),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_line(line: &str) -> protocol::Response {
|
||||
match protocol::parse_request(line) {
|
||||
Ok(request) => handle_request(request),
|
||||
Err((id, error)) => protocol::Response::err(id, error),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn response_line(line: &str) -> String {
|
||||
let response = handle_line(line);
|
||||
match protocol::encode_response(&response) {
|
||||
Ok(encoded) => encoded,
|
||||
Err(error) => fallback_error_line(&response.id, error),
|
||||
}
|
||||
}
|
||||
|
||||
fn fallback_error_line(id: &str, error: BridgeError) -> String {
|
||||
let response = protocol::Response::err(id, error);
|
||||
let value = serde_json::to_value(response).unwrap_or_else(|_| {
|
||||
serde_json::json!({
|
||||
"id": id,
|
||||
"type": "response",
|
||||
"ok": false,
|
||||
"error": {
|
||||
"code": "internal_error",
|
||||
"message": "failed to encode error response",
|
||||
"retryable": false
|
||||
}
|
||||
})
|
||||
});
|
||||
format!("{value}\n")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn ping_line_returns_pong() {
|
||||
let line = response_line(r#"{"id":"1","type":"request","method":"ping","params":{}}"#);
|
||||
assert!(line.contains(r#""ok":true"#));
|
||||
assert!(line.contains(r#""pong":true"#));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_method_returns_error() {
|
||||
let line = response_line(r#"{"id":"2","type":"request","method":"nope","params":{}}"#);
|
||||
assert!(line.contains(r#""ok":false"#));
|
||||
assert!(line.contains("unknown_method"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
use std::env;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BridgeConfig {
|
||||
pub listen: String,
|
||||
}
|
||||
|
||||
impl BridgeConfig {
|
||||
pub fn from_env() -> anyhow::Result<Self> {
|
||||
let listen =
|
||||
env::var("MM_BRIDGE_LISTEN").unwrap_or_else(|_| "127.0.0.1:8765".to_string());
|
||||
|
||||
validate_listen(&listen)?;
|
||||
|
||||
Ok(Self { listen })
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_listen(listen: &str) -> anyhow::Result<()> {
|
||||
anyhow::ensure!(
|
||||
!listen.trim().is_empty(),
|
||||
"MM_BRIDGE_LISTEN must not be empty"
|
||||
);
|
||||
anyhow::ensure!(
|
||||
listen.contains(':'),
|
||||
"MM_BRIDGE_LISTEN must be host:port"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn rejects_empty_listen() {
|
||||
assert!(validate_listen("").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepts_host_port() {
|
||||
assert!(validate_listen("127.0.0.1:8765").is_ok());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
//! Main bridge library.
|
||||
|
||||
pub mod app;
|
||||
pub mod config;
|
||||
pub mod protocol;
|
||||
pub mod server;
|
||||
pub mod status;
|
||||
pub mod transport;
|
||||
@@ -0,0 +1,24 @@
|
||||
use anyhow::Context;
|
||||
use mm_bridge::{config::BridgeConfig, server};
|
||||
use tracing_subscriber::EnvFilter;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
init_tracing();
|
||||
|
||||
let config = BridgeConfig::from_env().context("failed to load bridge config")?;
|
||||
tracing::info!(listen = %config.listen, "starting emacs-mattermost bridge");
|
||||
|
||||
server::run(config).await
|
||||
}
|
||||
|
||||
fn init_tracing() {
|
||||
let filter = EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| EnvFilter::new("mm_bridge=info,warn"));
|
||||
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(filter)
|
||||
.with_target(true)
|
||||
.with_writer(std::io::stderr)
|
||||
.init();
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
use mm_core::{BridgeError, ErrorKind};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct Request {
|
||||
pub id: String,
|
||||
#[serde(rename = "type")]
|
||||
pub kind: String,
|
||||
pub method: String,
|
||||
#[serde(default)]
|
||||
pub params: Value,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct Response {
|
||||
pub id: String,
|
||||
#[serde(rename = "type")]
|
||||
pub kind: &'static str,
|
||||
pub ok: bool,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub result: Option<Value>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub error: Option<ProtocolError>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ProtocolError {
|
||||
pub code: String,
|
||||
pub message: String,
|
||||
pub retryable: bool,
|
||||
}
|
||||
|
||||
impl Response {
|
||||
pub fn ok(id: impl Into<String>, result: Value) -> Self {
|
||||
Self {
|
||||
id: id.into(),
|
||||
kind: "response",
|
||||
ok: true,
|
||||
result: Some(result),
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn err(id: impl Into<String>, error: BridgeError) -> Self {
|
||||
let kind = error.kind();
|
||||
Self {
|
||||
id: id.into(),
|
||||
kind: "response",
|
||||
ok: false,
|
||||
result: None,
|
||||
error: Some(ProtocolError {
|
||||
code: kind.as_str().to_string(),
|
||||
message: error.message().to_string(),
|
||||
retryable: kind.retryable(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_request(line: &str) -> Result<Request, (String, BridgeError)> {
|
||||
let value: Value = serde_json::from_str(line).map_err(|err| {
|
||||
(
|
||||
"unknown".to_string(),
|
||||
BridgeError::new(ErrorKind::InvalidJson, err.to_string()),
|
||||
)
|
||||
})?;
|
||||
|
||||
let id = value
|
||||
.get("id")
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or("unknown")
|
||||
.to_string();
|
||||
|
||||
let request: Request = serde_json::from_value(value).map_err(|err| {
|
||||
(
|
||||
id.clone(),
|
||||
BridgeError::new(ErrorKind::InvalidRequest, err.to_string()),
|
||||
)
|
||||
})?;
|
||||
|
||||
validate_request(&request).map_err(|err| (id, err))?;
|
||||
Ok(request)
|
||||
}
|
||||
|
||||
fn validate_request(request: &Request) -> Result<(), BridgeError> {
|
||||
if request.id.trim().is_empty() {
|
||||
return Err(BridgeError::new(
|
||||
ErrorKind::InvalidRequest,
|
||||
"id must not be empty",
|
||||
));
|
||||
}
|
||||
|
||||
if request.kind != "request" {
|
||||
return Err(BridgeError::new(
|
||||
ErrorKind::InvalidRequest,
|
||||
"type must be request",
|
||||
));
|
||||
}
|
||||
|
||||
if request.method.trim().is_empty() {
|
||||
return Err(BridgeError::new(
|
||||
ErrorKind::InvalidRequest,
|
||||
"method must not be empty",
|
||||
));
|
||||
}
|
||||
|
||||
if !request.params.is_object() && !request.params.is_null() {
|
||||
return Err(BridgeError::new(
|
||||
ErrorKind::InvalidRequest,
|
||||
"params must be an object",
|
||||
));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn encode_response(response: &Response) -> Result<String, BridgeError> {
|
||||
let mut line = serde_json::to_string(response)
|
||||
.map_err(|err| BridgeError::new(ErrorKind::InternalError, err.to_string()))?;
|
||||
line.push('\n');
|
||||
Ok(line)
|
||||
}
|
||||
|
||||
pub fn pong_result() -> Value {
|
||||
json!({ "pong": true })
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn parses_ping_request() {
|
||||
let req = parse_request(r#"{"id":"1","type":"request","method":"ping","params":{}}"#)
|
||||
.expect("valid request");
|
||||
assert_eq!(req.method, "ping");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encodes_response_with_newline() {
|
||||
let line = encode_response(&Response::ok("1", pong_result())).expect("encode");
|
||||
assert!(line.ends_with('\n'));
|
||||
assert!(line.contains(r#""pong":true"#));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
use crate::{config::BridgeConfig, transport};
|
||||
use anyhow::Context;
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
pub async fn run(config: BridgeConfig) -> anyhow::Result<()> {
|
||||
let listener = TcpListener::bind(&config.listen)
|
||||
.await
|
||||
.with_context(|| format!("failed to bind {}", config.listen))?;
|
||||
|
||||
tracing::info!(listen = %config.listen, "bridge listening");
|
||||
|
||||
loop {
|
||||
let (stream, peer) = listener.accept().await.context("accept failed")?;
|
||||
tracing::debug!(%peer, "accepted connection");
|
||||
|
||||
tokio::spawn(async move {
|
||||
if let Err(error) = transport::handle_client(stream).await {
|
||||
tracing::warn!(%error, "client handler failed");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
use mm_core::{BridgeState, ServerSummary};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
pub const BRIDGE_VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
pub fn status_result() -> Value {
|
||||
let servers: Vec<ServerSummary> = Vec::new();
|
||||
|
||||
json!({
|
||||
"bridge": {
|
||||
"version": BRIDGE_VERSION,
|
||||
"state": BridgeState::Running,
|
||||
},
|
||||
"servers": servers,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn status_has_empty_servers() {
|
||||
let value = status_result();
|
||||
assert_eq!(value["servers"].as_array().unwrap().len(), 0);
|
||||
assert_eq!(value["bridge"]["state"], "running");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
use crate::app;
|
||||
|
||||
pub async fn handle_client(stream: TcpStream) -> std::io::Result<()> {
|
||||
let peer = stream.peer_addr().ok();
|
||||
tracing::info!(?peer, "client connected");
|
||||
|
||||
let (read_half, mut write_half) = stream.into_split();
|
||||
let mut lines = BufReader::new(read_half).lines();
|
||||
|
||||
while let Some(line) = lines.next_line().await? {
|
||||
if line.trim().is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let response = app::response_line(&line);
|
||||
write_half.write_all(response.as_bytes()).await?;
|
||||
write_half.flush().await?;
|
||||
}
|
||||
|
||||
tracing::info!(?peer, "client disconnected");
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "mm-client"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
mm-core.workspace = true
|
||||
@@ -0,0 +1 @@
|
||||
//! auth module placeholder.
|
||||
@@ -0,0 +1,8 @@
|
||||
//! Mattermost REST/WebSocket client.
|
||||
//!
|
||||
//! Placeholder crate. Implementation starts after the bridge skeleton works.
|
||||
|
||||
pub mod auth;
|
||||
pub mod rest;
|
||||
pub mod tls;
|
||||
pub mod ws;
|
||||
@@ -0,0 +1 @@
|
||||
//! rest module placeholder.
|
||||
@@ -0,0 +1 @@
|
||||
//! tls module placeholder.
|
||||
@@ -0,0 +1 @@
|
||||
//! ws module placeholder.
|
||||
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "mm-core"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
serde.workspace = true
|
||||
thiserror.workspace = true
|
||||
@@ -0,0 +1,73 @@
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum ErrorKind {
|
||||
InvalidJson,
|
||||
InvalidRequest,
|
||||
UnknownMethod,
|
||||
Unauthorized,
|
||||
Forbidden,
|
||||
NotFound,
|
||||
RateLimited,
|
||||
NetworkError,
|
||||
TlsError,
|
||||
ServerError,
|
||||
WebsocketDisconnected,
|
||||
InternalError,
|
||||
}
|
||||
|
||||
impl ErrorKind {
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::InvalidJson => "invalid_json",
|
||||
Self::InvalidRequest => "invalid_request",
|
||||
Self::UnknownMethod => "unknown_method",
|
||||
Self::Unauthorized => "unauthorized",
|
||||
Self::Forbidden => "forbidden",
|
||||
Self::NotFound => "not_found",
|
||||
Self::RateLimited => "rate_limited",
|
||||
Self::NetworkError => "network_error",
|
||||
Self::TlsError => "tls_error",
|
||||
Self::ServerError => "server_error",
|
||||
Self::WebsocketDisconnected => "websocket_disconnected",
|
||||
Self::InternalError => "internal_error",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn retryable(self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::RateLimited
|
||||
| Self::NetworkError
|
||||
| Self::ServerError
|
||||
| Self::WebsocketDisconnected
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum BridgeError {
|
||||
#[error("{kind}: {message}", kind = kind.as_str())]
|
||||
Protocol { kind: ErrorKind, message: String },
|
||||
}
|
||||
|
||||
impl BridgeError {
|
||||
pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
|
||||
Self::Protocol {
|
||||
kind,
|
||||
message: message.into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn kind(&self) -> ErrorKind {
|
||||
match self {
|
||||
Self::Protocol { kind, .. } => *kind,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn message(&self) -> &str {
|
||||
match self {
|
||||
Self::Protocol { message, .. } => message,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
||||
macro_rules! string_id {
|
||||
($name:ident) => {
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct $name(pub String);
|
||||
|
||||
impl $name {
|
||||
pub fn new(value: impl Into<String>) -> Self {
|
||||
Self(value.into())
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for $name {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(self.as_str())
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
string_id!(ServerId);
|
||||
string_id!(TeamId);
|
||||
string_id!(ChannelId);
|
||||
string_id!(UserId);
|
||||
string_id!(PostId);
|
||||
|
||||
impl Default for ServerId {
|
||||
fn default() -> Self {
|
||||
Self::new("default")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
//! Shared domain types for emacs-mattermost.
|
||||
//!
|
||||
//! This crate contains no network code.
|
||||
|
||||
pub mod error;
|
||||
pub mod ids;
|
||||
pub mod model;
|
||||
|
||||
pub use error::{BridgeError, ErrorKind};
|
||||
pub use ids::{ChannelId, PostId, ServerId, TeamId, UserId};
|
||||
pub use model::{BridgeState, ServerSummary};
|
||||
@@ -0,0 +1,14 @@
|
||||
use crate::ids::ServerId;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum BridgeState {
|
||||
Running,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ServerSummary {
|
||||
pub server_id: ServerId,
|
||||
pub state: String,
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "mm-store"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
mm-core.workspace = true
|
||||
+1
@@ -0,0 +1 @@
|
||||
//! cache module placeholder.
|
||||
@@ -0,0 +1,6 @@
|
||||
//! Local cache and session storage.
|
||||
//!
|
||||
//! Placeholder crate. Implementation starts at the authentication step.
|
||||
|
||||
pub mod cache;
|
||||
pub mod session;
|
||||
@@ -0,0 +1 @@
|
||||
//! session module placeholder.
|
||||
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "mm-web"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
mm-core.workspace = true
|
||||
@@ -0,0 +1,7 @@
|
||||
//! Web status UI.
|
||||
//!
|
||||
//! Placeholder crate. Implementation starts at the Docker/status step.
|
||||
|
||||
pub mod routes;
|
||||
pub mod state;
|
||||
pub mod templates;
|
||||
@@ -0,0 +1 @@
|
||||
//! routes module placeholder.
|
||||
@@ -0,0 +1 @@
|
||||
//! state module placeholder.
|
||||
@@ -0,0 +1 @@
|
||||
//! templates module placeholder.
|
||||
@@ -0,0 +1,3 @@
|
||||
[toolchain]
|
||||
channel = "stable"
|
||||
components = ["rustfmt", "clippy"]
|
||||
Reference in New Issue
Block a user