Skip to content

Commit

Permalink
Merge pull request #78 from 4t145/refactor-config-and-init
Browse files Browse the repository at this point in the history
Refactor config, init and tracing
  • Loading branch information
gudaoxuri authored Sep 27, 2023
2 parents 4e9cb0f + 97eb572 commit 40af5e2
Show file tree
Hide file tree
Showing 81 changed files with 2,444 additions and 1,983 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ documentation = "https://docs.rs/tardis/"
repository = "https://github.com/ideal-world/tardis"
edition = "2021"
license = "MIT/Apache-2.0"
rust-version = "1.64"
rust-version = "1.72"
11 changes: 8 additions & 3 deletions examples/tracing-otlp/config/conf-default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ version = "1.0.0"
port = 8089
doc_urls = [["test env", "http://localhost:8089/"]]

[fw.db]
url = "postgres://postgres:ENC(5892ae51dbeedacdf10ba4c0d7af42a7)@localhost:5432/test"
# [fw.db]
# url = "postgres://postgres:ENC(5892ae51dbeedacdf10ba4c0d7af42a7)@localhost:5432/test"

[fw.log]
level = "info"
level = "debug"

[fw.log.tracing]
# https://www.jaegertracing.io/docs/1.49/getting-started/
# endpoint = "http://localhost:4318/"
endpoint = "http://localhost:4317"
# protocol = "http/protobuf"
protocol = "grpc"
server_name = "tracing示例"
3 changes: 1 addition & 2 deletions examples/tracing-otlp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ mod route;
///
#[tokio::main]
async fn main() -> TardisResult<()> {
env::set_var("RUST_LOG", "debug");
env::set_var("PROFILE", "default");

// Initial configuration
TardisFuns::init(Some("config")).await?;
// Register the processor and start the web service
TardisFuns::web_server().add_route(Api).await.start().await;
web_server.await;
TardisFuns::web_server().await;
Ok(())
}
2 changes: 1 addition & 1 deletion examples/tracing-otlp/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tardis::web::web_resp::{TardisApiResult, TardisResp};

use crate::processor::{self, TaskKind};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Api;

#[poem_openapi::OpenApi]
Expand Down
8 changes: 4 additions & 4 deletions examples/websocket/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Page {
#[oai(path = "/echo", method = "get")]
async fn echo(&self) -> Html<&'static str> {
Html(
r###"
r##"
<body>
<form id="loginForm">
Name: <input id="nameInput" type="text" />
Expand Down Expand Up @@ -58,14 +58,14 @@ impl Page {
msgInput.value = "";
});
</script>
"###,
"##,
)
}

#[oai(path = "/broadcast", method = "get")]
async fn broadcast(&self) -> Html<&'static str> {
Html(
r###"
r##"
<body>
<form id="loginForm">
Name: <input id="nameInput" type="text" />
Expand Down Expand Up @@ -108,7 +108,7 @@ impl Page {
msgInput.value = "";
});
</script>
"###,
"##,
)
}

Expand Down
46 changes: 37 additions & 9 deletions tardis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ name = "tardis"
path = "src/lib.rs"

[features]
default = ["tardis-macros"]
conf-remote = ["web-client", "async-trait", "rust-crypto"]
crypto = ["rust-crypto", "rsa"]
default = ["tardis-macros", "async-trait"]
conf-remote = ["web-client", "async-trait", "crypto"]
digest = ["sha1", "sha2", "hmac", "md-5", "sm3", "dep:digest"]
aead = ["aes-gcm-siv", "aes-gcm", "aes-siv", "dep:aead"]
block_modes = ["cbc", "ecb", "aes", "cipher"]
crypto = ["rsa", "digest", "aead", "block_modes"]
crypto-with-sm = ["crypto", "libsm", "num-bigint"]
future = ["futures", "async-stream", "futures-util", "async-trait"]
tls = ["native-tls"]
Expand Down Expand Up @@ -48,8 +51,10 @@ fs = ["tokio/fs"]
process = ["tokio/process"]
test = ["testcontainers"]
tracing = ["tracing-opentelemetry", "opentelemetry", "opentelemetry-otlp"]
tokio-console = ["console-subscriber"]
tracing-appender = ["dep:tracing-appender"]
web-server-grpc = ["web-server", "dep:poem-grpc"]
cluster = ["web-server", "ws-client"]
cluster = ["web-server", "ws-client", "cache"]

[dependencies]
# Basic
Expand All @@ -65,8 +70,10 @@ rand_core = { version = "0.6" }
chrono = { version = "0.4", features = ["serde"] }
config = { version = "0.13" }
regex = { version = "1.5" }
url = { version = "2.2" }
url = { version = "2.2", features = ["serde"] }
lru = { version = "0.11.0" }
typed-builder = { version = "0.16" }
paste = { version = "1.0" }
# Tokio
tokio = { version = "1", features = [
"macros",
Expand All @@ -80,7 +87,8 @@ tardis-macros = { version = "0.1.0-beta.11", path = "../tardis-macros", optional
# Log
tracing = { version = "0.1" }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

tracing-appender = { version = "0.2", optional = true }
console-subscriber = { version = "0.1", optional = true }
# Tracing
tracing-opentelemetry = { version = "0.21", optional = true }
opentelemetry = { version = "0.20", default-features = false, features = [
Expand All @@ -93,16 +101,37 @@ opentelemetry-otlp = { version = "0.13", features = [
"http-proto",
"tls",
], optional = true }

# TLS
native-tls = { version = "0.2", optional = true }

# Crypto
rust-crypto = { version = "0.2", optional = true }
cipher = { version = "0.4", optional = true, features = ["block-padding", "alloc"]}
## Digest
## see https://github.com/RustCrypto/hashes
sha1 = { version = "0.10", optional = true }
sha2 = { version = "0.10", optional = true }
# md5 is no longer considered secure
md-5 = { version = "0.10", optional = true }
# this sm3 lib support digest trait
sm3 = { version = "0.4", optional = true }
hmac = { version = "0.12", optional = true }
digest = { version = "0.10", optional = true }
# RSA
rsa = { version = "0.9", features = ["pem"], optional = true }
# AES
aead = { version = "0.5", optional = true }
aes ={ version = "0.8", optional = true }
aes-gcm-siv = { version = "0.11", optional = true }
aes-gcm = { version = "0.10", optional = true }
aes-siv = { version = "0.7", optional = true }
## block-mode
cbc = { version = "0.1", optional = true }
ecb = { version = "0.1", optional = true }
#
libsm = { version = "0.5", optional = true }
num-bigint = { version = "0.4.3", optional = true }


# Future
futures = { version = "0.3", optional = true }
async-stream = { version = "0.3", optional = true }
Expand Down Expand Up @@ -180,7 +209,6 @@ testcontainers = { version = "0.14", optional = true }
# Common
tokio = { version = "1", features = ["time", "rt", "macros", "sync"] }
criterion = { version = "0.5" }
console-subscriber = "0.1.10"
poem-grpc-build = "0.2.21"
prost = "0.11.9"
strip-ansi-escapes = "0.2.0"
Expand Down
21 changes: 11 additions & 10 deletions tardis/benches/crypto_benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use aes::Aes128;
use criterion::{criterion_group, criterion_main, Criterion};

use tardis::TardisFuns;
Expand All @@ -24,17 +25,17 @@ Rust 拥有出色的文档、友好的编译器和清晰的错误提示信息,

// AES

let key = TardisFuns::crypto.key.rand_16_hex().unwrap();
let iv = TardisFuns::crypto.key.rand_16_hex().unwrap();
let encrypted_data = TardisFuns::crypto.aes.encrypt_cbc(large_text, &key, &iv).unwrap();
let key = TardisFuns::crypto.key.rand_16_bytes();
let iv = TardisFuns::crypto.key.rand_16_bytes();
let encrypted_data = TardisFuns::crypto.aead.encrypt_cbc::<Aes128>(large_text, &key, &iv).unwrap();
c.bench_function("CRYPTO: aes_encrypt_cbc", |b| {
b.iter(|| {
TardisFuns::crypto.aes.encrypt_cbc(large_text, &key, &iv).unwrap();
TardisFuns::crypto.aead.encrypt_cbc::<Aes128>(large_text, &key, &iv).unwrap();
})
});
c.bench_function("CRYPTO: aes_decrypt_cbc", |b| {
b.iter(|| {
TardisFuns::crypto.aes.decrypt_cbc(&encrypted_data, &key, &iv).unwrap();
TardisFuns::crypto.aead.decrypt_cbc::<Aes128>(&encrypted_data, &key, &iv).unwrap();
})
});

Expand Down Expand Up @@ -77,17 +78,17 @@ Rust 拥有出色的文档、友好的编译器和清晰的错误提示信息,
});

// SM4
let key = TardisFuns::crypto.key.rand_16_hex().unwrap();
let iv = TardisFuns::crypto.key.rand_16_hex().unwrap();
let encrypted_data = TardisFuns::crypto.sm4.encrypt_cbc(large_text, &key, &iv).unwrap();
let key = TardisFuns::crypto.key.rand_16_bytes();
let iv = TardisFuns::crypto.key.rand_16_bytes();
let encrypted_data = TardisFuns::crypto.sm4.encrypt_cbc(large_text, key, iv).unwrap();
c.bench_function("CRYPTO: sm4_encrypt_cbc", |b| {
b.iter(|| {
TardisFuns::crypto.sm4.encrypt_cbc(large_text, &key, &iv).unwrap();
TardisFuns::crypto.sm4.encrypt_cbc(large_text, key, iv).unwrap();
})
});
c.bench_function("CRYPTO: sm4_decrypt_cbc", |b| {
b.iter(|| {
TardisFuns::crypto.sm4.decrypt_cbc(&encrypted_data, &key, &iv).unwrap();
TardisFuns::crypto.sm4.decrypt_cbc(&encrypted_data, key, iv).unwrap();
})
});

Expand Down
12 changes: 11 additions & 1 deletion tardis/src/basic/dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{collections::HashMap, fmt, pin::Pin, sync::Arc};
use tokio::sync::{Mutex, RwLock};
use tracing::error;

use crate::serde::{Deserialize, Serialize};
use crate::{serde::{Deserialize, Serialize}, TardisFuns};

use super::result::TardisResult;

Expand Down Expand Up @@ -96,6 +96,16 @@ impl Default for TardisContext {
}

impl TardisContext {
pub fn to_json(&self) -> TardisResult<String> {
TardisFuns::json.obj_to_string(self)
}

pub fn to_base64(&self) -> TardisResult<String> {
let ctx = TardisContext::default();
let ctx = TardisFuns::json.obj_to_string(&ctx)?;
Ok(TardisFuns::crypto.base64.encode(ctx))
}

pub async fn add_ext(&self, key: &str, value: &str) -> TardisResult<()> {
self.ext.write().await.insert(key.to_string(), value.to_string());
Ok(())
Expand Down
9 changes: 9 additions & 0 deletions tardis/src/basic/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ impl TardisError {
}
}

impl std::error::Error for TardisError {}

pub struct TardisErrorWithExt {
pub ext: String,
/// https://www.andiamo.co.uk/resources/iso-language-codes/
Expand Down Expand Up @@ -147,6 +149,13 @@ impl TardisErrorWithExt {
}
}

// dynamic cast any error into TardisError
impl From<&dyn std::error::Error> for TardisError {
fn from(error: &dyn std::error::Error) -> Self {
TardisError::format_error(&format!("[Tardis.Basic] {error}"), "")
}
}

impl From<std::io::Error> for TardisError {
fn from(error: std::io::Error) -> Self {
TardisError::io_error(&format!("[Tardis.Basic] {error}"), "")
Expand Down
Loading

0 comments on commit 40af5e2

Please sign in to comment.