diff --git a/docker/README.md b/docker/README.md index eef67a8..3567309 100644 --- a/docker/README.md +++ b/docker/README.md @@ -5,8 +5,8 @@ | No. | Lang | Base Image | Server | Client | |:----|:-----------------------------|:---------------------------------------------------------------|:---------------------------------|:---------------------------------| | 1 | [C++](hello-grpc-cpp) | | feuyeux/grpc_server_cpp:1.0.0 | feuyeux/grpc_client_cpp:1.0.0 | -| 2 | [Rust](hello-grpc-rust) | | feuyeux/grpc_server_rust:1.0.0 | feuyeux/grpc_client_rust:1.0.0 | -| 3 | [Java](hello-grpc-java) | [openjdk:21-jdk-slim](java_grpc.dockerfile) | feuyeux/grpc_server_java:1.0.0 | feuyeux/grpc_client_java:1.0.0 | +| 2 | [Rust](hello-grpc-rust) | [rust:1.81-alpine3.20](rust_grpc.dockerfile) | feuyeux/grpc_server_rust:1.0.0 | feuyeux/grpc_client_rust:1.0.0 | +| 3 | [Java](hello-grpc-java) | [openjdk:23-jdk-slim](java_grpc.dockerfile) | feuyeux/grpc_server_java:1.0.0 | feuyeux/grpc_client_java:1.0.0 | | 4 | [Go](hello-grpc-go) | [golang:1.23-alpine](go_grpc.dockerfile) | feuyeux/grpc_server_go:1.0.0 | feuyeux/grpc_client_go:1.0.0 | | 5 | [C#](hello-grpc-csharp) | [mcr.microsoft.com/dotnet/runtime:8.0](csharp_grpc.dockerfile) | feuyeux/grpc_server_csharp:1.0.0 | feuyeux/grpc_client_csharp:1.0.0 | | 6 | [Python](hello-grpc-python) | [python:3.11-slim](python_grpc.dockerfile) | feuyeux/grpc_server_python:1.0.0 | feuyeux/grpc_client_python:1.0.0 | diff --git a/docker/cross_run_client.sh b/docker/cross_run_client.sh new file mode 100644 index 0000000..82065be --- /dev/null +++ b/docker/cross_run_client.sh @@ -0,0 +1,14 @@ +#!/bin/bash +cd "$( + cd "$(dirname "$0")" >/dev/null 2>&1 + pwd -P +)/" || exit +set -e + +langs=(rust java go csharp python) +# TODO: cpp rust java go csharp python nodejs dart kotlin swift php ts +for lang in "${langs[@]}"; do + echo "~~~ run grpc $lang client ~~~" + # pass the first argument to the script + sh "${lang}_run_client.sh" "$1" +done diff --git a/docker/grpc-cpp.dockerfile b/docker/grpc-cpp.dockerfile index f1c4755..0025494 100644 --- a/docker/grpc-cpp.dockerfile +++ b/docker/grpc-cpp.dockerfile @@ -21,10 +21,10 @@ RUN apt-get update && apt-get install -y \ libssl-dev \ && apt-get clean -ENV GRPC_SRC_PATH /source/grpc -ENV HELLO_BUILD_PATH /source/build -ENV GRPC_INSTALL_PATH /var/grpc/install -ENV PATH "$GRPC_INSTALL_PATH/bin:$PATH" +ENV GRPC_SRC_PATH=/source/grpc +ENV HELLO_BUILD_PATH=/source/build +ENV GRPC_INSTALL_PATH=/var/grpc/install +ENV PATH="$GRPC_INSTALL_PATH/bin:$PATH" # To solve the issue: CMake 3.22 or higher is required # https://cmake.org/download/ @@ -55,9 +55,9 @@ RUN cd ${GRPC_SRC_PATH} && \ ## build hello-grpc-cpp denpendencies ## -ENV GLOG_SRC_PATH /source/glog -ENV GFLAGS_SRC_PATH /source/gflags -ENV CACHE2_SRC_PATH /source/Cache2 +ENV GLOG_SRC_PATH=/source/glog +ENV GFLAGS_SRC_PATH=/source/gflags +ENV CACHE2_SRC_PATH=/source/Cache2 COPY glog $GLOG_SRC_PATH COPY gflags $GFLAGS_SRC_PATH @@ -87,7 +87,7 @@ RUN cd /source/hello-grpc-cpp && mkdir build && cd build && \ # docker run --rm -it feuyeux/grpc_cpp:1.0.0 bash FROM debian:12-slim AS server -ENV PATH "/var/grpc/install/bin:$PATH" +ENV PATH="/var/grpc/install/bin:$PATH" COPY --from=build /var/grpc/install /var/grpc/install WORKDIR /opt/hello-grpc/ COPY --from=build /source/glog/build/libglog.so.1 /usr/local/lib/ @@ -99,7 +99,7 @@ RUN /sbin/ldconfig -v CMD ["./proto_server"] FROM debian:12-slim AS client -ENV PATH "/var/grpc/install/bin:$PATH" +ENV PATH="/var/grpc/install/bin:$PATH" COPY --from=build /source/glog/build/libglog.so.1 /usr/local/lib/ COPY --from=build /var/grpc/install /var/grpc/install WORKDIR /opt/hello-grpc/ diff --git a/docker/grpc-dart.dockerfile b/docker/grpc-dart.dockerfile index ebe92ec..24c301c 100644 --- a/docker/grpc-dart.dockerfile +++ b/docker/grpc-dart.dockerfile @@ -2,7 +2,7 @@ FROM dart:3.3.0 AS build COPY hello-grpc-dart /hello-grpc WORKDIR /hello-grpc -ENV PUB_HOSTED_URL "https://pub.flutter-io.cn" +ENV PUB_HOSTED_URL="https://pub.flutter-io.cn" RUN dart pub get RUN dart compile exe server.dart -o bin/server RUN dart compile exe client.dart -o bin/client diff --git a/docker/java_build.sh b/docker/java_build.sh index d802b0b..ebe0624 100755 --- a/docker/java_build.sh +++ b/docker/java_build.sh @@ -5,6 +5,27 @@ cd "$( )/" || exit set -e +docker images | grep openjdk + +# +if [ "$(uname)" == "Darwin" ]; then + echo "macOS detected" +elif [ "$(expr substr "$(uname -s)" 1 5)" == "Linux" ]; then + echo "Linux detected" +else + export JAVA_HOME_8=/d/zoo/java-se-8u44/ + export JAVA_HOME_11=/d/zoo/jdk-11.0.23/ + export JAVA_HOME_21=/d/zoo/jdk-21.0.3/ + export JAVA_HOME_23=/d/zoo/jdk-23/ +fi +# if the first input parameter is empty, use JAVA_HOME_21, otherwise use it. +if [ -z "$1" ]; then + export JAVA_HOME=$JAVA_HOME_21 +else + export JAVA_HOME="JAVA_HOME_$1" +fi +echo "JAVA_HOME=$JAVA_HOME" + build_server() { echo "~~~ build grpc server java ~~~" cd ../hello-grpc-java @@ -12,7 +33,7 @@ build_server() { cp target/hello-grpc-java-server.jar ../docker/ cd ../docker - docker build -f java_grpc.dockerfile --target server -t feuyeux/grpc_server_java:1.0.0 --pull . + docker build -f java_grpc.dockerfile --target server -t feuyeux/grpc_server_java:1.0.0 . rm -rf hello-grpc-java-server.jar echo } @@ -24,14 +45,14 @@ build_client() { cp target/hello-grpc-java-client.jar ../docker/ cd ../docker - docker build -f java_grpc.dockerfile --target client -t feuyeux/grpc_client_java:1.0.0 --pull . + docker build -f java_grpc.dockerfile --target client -t feuyeux/grpc_client_java:1.0.0 . rm -rf hello-grpc-java-client.jar echo } -if [ "$1" == "server" ]; then +if [ "$2" == "server" ]; then build_server -elif [ "$1" == "client" ]; then +elif [ "$2" == "client" ]; then build_client else build_server diff --git a/docker/java_grpc.dockerfile b/docker/java_grpc.dockerfile index 1640810..2667e1d 100644 --- a/docker/java_grpc.dockerfile +++ b/docker/java_grpc.dockerfile @@ -1,10 +1,11 @@ -FROM openjdk:21-jdk-slim AS server +FROM openjdk:23-jdk-slim AS server +# https://hub.docker.com/_/openjdk COPY hello-grpc-java-server.jar grpc-server.jar COPY tls/server_certs /var/hello_grpc/server_certs COPY tls/client_certs /var/hello_grpc/client_certs ENTRYPOINT ["java","-jar","/grpc-server.jar"] -FROM openjdk:21-jdk-slim AS client +FROM openjdk:23-jdk-slim AS client COPY hello-grpc-java-client.jar grpc-client.jar COPY tls/client_certs /var/hello_grpc/client_certs CMD ["java","-jar","/grpc-client.jar"] diff --git a/docker/kotlin_build.sh b/docker/kotlin_build.sh index b1de5c7..19522aa 100755 --- a/docker/kotlin_build.sh +++ b/docker/kotlin_build.sh @@ -6,7 +6,9 @@ cd "$( pwd -P )/" || exit -# if os is windows, echo ok, otherwise echo alert. +docker images | grep openjdk + +# if [ "$(uname)" == "Darwin" ]; then echo "macOS detected" elif [ "$(expr substr "$(uname -s)" 1 5)" == "Linux" ]; then diff --git a/docker/build_rust.sh b/docker/rust_build.sh old mode 100755 new mode 100644 similarity index 62% rename from docker/build_rust.sh rename to docker/rust_build.sh index d188d4a..3cbf467 --- a/docker/build_rust.sh +++ b/docker/rust_build.sh @@ -6,13 +6,15 @@ cd "$( pwd -P )/" || exit +docker images | grep rust + echo "~~~ build grpc rust ~~~" cd .. cp -r hello-grpc-rust docker/ cd docker -docker build --cpu-shares=5120 --memory=4g -f grpc-rust.dockerfile --target build -t feuyeux/grpc_rust:1.0.0 . -docker build -f grpc-rust.dockerfile --target server -t feuyeux/grpc_server_rust:1.0.0 . -docker build -f grpc-rust.dockerfile --target client -t feuyeux/grpc_client_rust:1.0.0 . +docker build --cpu-shares=5120 --memory=4g -f rust_grpc.dockerfile --target build -t feuyeux/grpc_rust:1.0.0 . +docker build -f rust_grpc.dockerfile --target server -t feuyeux/grpc_server_rust:1.0.0 . +docker build -f rust_grpc.dockerfile --target client -t feuyeux/grpc_client_rust:1.0.0 . rm -rf hello-grpc-rust echo diff --git a/docker/grpc-rust.dockerfile b/docker/rust_grpc.dockerfile similarity index 77% rename from docker/grpc-rust.dockerfile rename to docker/rust_grpc.dockerfile index 8d6043d..5c7b4cc 100644 --- a/docker/grpc-rust.dockerfile +++ b/docker/rust_grpc.dockerfile @@ -1,8 +1,8 @@ # https://hub.docker.com/_/rust?tab=tags&page=1&ordering=last_updated&name=alpine -# FROM rust:1.55.0-alpine3.14 AS build -FROM rust:alpine AS build -ENV RUSTUP_DIST_SERVER https://mirrors.ustc.edu.cn/rust-static -ENV RUSTUP_UPDATE_ROOT https://mirrors.ustc.edu.cn/rust-static/rustup +FROM rust:1.81-alpine3.20 AS build +# https://mirrors.ustc.edu.cn/help/rust-static.html +# ENV RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static +# ENV RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup RUN apk add --update \ protobuf \ musl-dev \ @@ -16,18 +16,18 @@ COPY tls/client_certs /var/hello_grpc/client_certs RUN RUSTFLAGS="-C target-cpu=native" cargo build --release --bin proto-server RUN RUSTFLAGS="-C target-cpu=native" cargo build --release --bin proto-client -FROM alpine:3 AS server +FROM rust:1.81-alpine3.20 AS server WORKDIR /app -ENV RUST_BACKTRACE 1 +ENV RUST_BACKTRACE=1 COPY --from=feuyeux/grpc_rust:1.0.0 /source/target/release/proto-server . COPY tls/server_certs /var/hello_grpc/server_certs COPY tls/client_certs /var/hello_grpc/client_certs COPY hello-grpc-rust/config/log4rs.yml config/log4rs.yml ENTRYPOINT ["./proto-server"] -FROM alpine:3 AS client +FROM rust:1.81-alpine3.20 AS client WORKDIR /app -ENV RUST_BACKTRACE 1 +ENV RUST_BACKTRACE=1 COPY --from=feuyeux/grpc_rust:1.0.0 /source/target/release/proto-client . COPY tls/client_certs /var/hello_grpc/client_certs COPY hello-grpc-rust/config/log4rs.yml config/log4rs.yml diff --git a/docker/rust_push.sh b/docker/rust_push.sh new file mode 100644 index 0000000..06e0720 --- /dev/null +++ b/docker/rust_push.sh @@ -0,0 +1,3 @@ +#!/bin/bash +docker push feuyeux/grpc_server_rust:1.0.0 +docker push feuyeux/grpc_client_rust:1.0.0 diff --git a/docker/rust_run_client.sh b/docker/rust_run_client.sh new file mode 100644 index 0000000..8fd9a4e --- /dev/null +++ b/docker/rust_run_client.sh @@ -0,0 +1,13 @@ +#!/bin/bash +cd "$( + cd "$(dirname "$0")" >/dev/null 2>&1 + pwd -P +)/" || exit +export CLIENT_NAME=grpc_client_rust +export CLIENT_IMG=feuyeux/$CLIENT_NAME:1.0.0 +# if there's first argument, it's secure, otherwise insecure +if [ "$1" = "secure" ]; then + sh run_tls_client.sh +else + sh run_insecure_client.sh +fi diff --git a/docker/rust_run_server.sh b/docker/rust_run_server.sh new file mode 100644 index 0000000..e590782 --- /dev/null +++ b/docker/rust_run_server.sh @@ -0,0 +1,13 @@ +#!/bin/bash +cd "$( + cd "$(dirname "$0")" >/dev/null 2>&1 + pwd -P +)/" || exit +export SERVER_NAME=grpc_server_rust +export SERVER_IMG=feuyeux/$SERVER_NAME:1.0.0 +# if there's first argument, it's secure, otherwise insecure +if [ "$1" = "secure" ]; then + sh run_tls_server.sh +else + sh run_insecure_server.sh +fi diff --git a/docker/tls/copy_certs.sh b/docker/tls/copy_certs.sh index a9346a7..7a13c36 100755 --- a/docker/tls/copy_certs.sh +++ b/docker/tls/copy_certs.sh @@ -5,19 +5,42 @@ cd "$( )/" || exit set -e -if [ -d /var/hello_grpc/ ]; then - echo "folder existed" - # sudo chown -R "$(whoami)" /var/hello_grpc/ -else - sudo mkdir -p /var/hello_grpc/ - sudo chown -R "$(whoami)" /var/hello_grpc/ -fi -ls -l /var/hello_grpc/ +if [[ "$OSTYPE" == "darwin"* ]]; then + # macOS implementation + if [ -d /var/hello_grpc/ ]; then + echo "folder existed" + # sudo chown -R "$(whoami)" /var/hello_grpc/ + else + sudo mkdir -p /var/hello_grpc/ + sudo chown -R "$(whoami)" /var/hello_grpc/ + fi + ls -l /var/hello_grpc/ + + if [ -f /var/hello_grpc/server_certs/myssl_root.cer ]; then + echo "certs existed" + else + cp -r server_certs /var/hello_grpc/ + cp -r client_certs /var/hello_grpc/ + fi + ls -l /var/hello_grpc/server_certs + +elif [[ "$OSTYPE" == "msys" ]]; then + # Windows implementation + if [ -d /d/garden/var/hello_grpc/ ]; then + echo "folder existed" + else + mkdir -p /d/garden/var/hello_grpc/ + fi + ls -l /d/garden/var/hello_grpc/ -if [ -f /var/hello_grpc/server_certs/myssl_root.cer ]; then - echo "certs existed" + if [ -f /d/garden/var/hello_grpc/server_certs/myssl_root.cer ]; then + echo "certs existed" + else + cp -r server_certs /d/garden/var/hello_grpc/ + cp -r client_certs /d/garden/var/hello_grpc/ + fi + ls -l /d/garden/var/hello_grpc/server_certs else - cp -r server_certs /var/hello_grpc/ - cp -r client_certs /var/hello_grpc/ + echo "Unsupported OS" + exit 1 fi -ls -l /var/hello_grpc/server_certs diff --git a/docker/tools/clean_world.sh b/docker/tools/clean_world.sh index eee5851..41a81fc 100755 --- a/docker/tools/clean_world.sh +++ b/docker/tools/clean_world.sh @@ -8,3 +8,4 @@ docker rm "$(docker ps -a -q)" >/dev/null 2>&1 docker rmi "$(docker images | grep none | awk "{print $3}")" >/dev/null 2>&1 bash kill_port.sh >/dev/null 2>&1 # docker images +docker system prune diff --git a/hello-grpc-rust/Cargo.toml b/hello-grpc-rust/Cargo.toml index a892063..75efad6 100644 --- a/hello-grpc-rust/Cargo.toml +++ b/hello-grpc-rust/Cargo.toml @@ -12,14 +12,13 @@ path = "src/landing/server.rs" name = "proto-client" path = "src/landing/client.rs" - [dependencies] #grpc https://lib.rs/crates/tonic -tonic = { version = "0.12.0", features = ["tls"] } +tonic = { version = "0.12.3", features = ["tls"] } #gRPC Codec https://lib.rs/crates/prost -prost = "0.13.0" +prost = "0.13.3" # https://lib.rs/crates/tokio -tokio = { version = "1.32.0", features = [ +tokio = { version = "1.40.0", features = [ "rt-multi-thread", "time", "fs", @@ -27,28 +26,28 @@ tokio = { version = "1.32.0", features = [ "net", ] } #https://lib.rs/crates/tokio-stream -tokio-stream = { version = "0.1.14", features = ["net"] } +tokio-stream = { version = "0.1.16", features = ["net"] } #https://lib.rs/crates/async-stream -async-stream = "0.3.4" +async-stream = "0.3.5" #https://lib.rs/crates/futures -futures = { version = "0.3.28", default-features = false, features = ["alloc"] } +futures = { version = "0.3.30", default-features = false, features = ["alloc"] } #json https://lib.rs/crates/serde -serde = { version = "1.0.188", features = ["derive"] } +serde = { version = "1.0.210", features = ["derive"] } #https://lib.rs/crates/serde_json -serde_json = "1.0.107" +serde_json = "1.0.128" #https://lib.rs/crates/rand rand = "0.8.5" #https://lib.rs/crates/chrono -chrono = "0.4.30" +chrono = "0.4.38" #https://lib.rs/crates/uuid -uuid = { version = "1.4.1", features = ["serde", "v4"] } +uuid = { version = "1.10.0", features = ["serde", "v4"] } #log https://lib.rs/crates/log -log = "0.4.20" +log = "0.4.22" #log impl https://lib.rs/crates/log4rs -log4rs = "1.2.0" +log4rs = "1.3.0" #static https://crates.io/crates/once_cell -once_cell = "1.18.0" +once_cell = "1.20.1" [build-dependencies] #https://lib.rs/crates/tonic-build -tonic-build = "0.12.0" +tonic-build = "0.12.3" diff --git a/hello-grpc-rust/README.md b/hello-grpc-rust/README.md index 89d8adb..b6eca85 100644 --- a/hello-grpc-rust/README.md +++ b/hello-grpc-rust/README.md @@ -10,9 +10,6 @@ rustup show ``` ```bash -# brew install protobuf -# brew link --overwrite protobuf -# protoc --version sh ../docker/tls/copy_certs.sh cargo build ``` @@ -35,10 +32,12 @@ $ find . -name "*.rs" ## 2 Run ```bash +export GRPC_HELLO_SECURE=Y cargo run --bin proto-server ``` ```bash +export GRPC_HELLO_SECURE=Y cargo run --bin proto-client ``` diff --git a/hello-grpc-rust/src/common/conn.rs b/hello-grpc-rust/src/common/conn.rs index c901983..2c5f50c 100644 --- a/hello-grpc-rust/src/common/conn.rs +++ b/hello-grpc-rust/src/common/conn.rs @@ -12,28 +12,32 @@ const DOMAIN_NAME: &str = "hello.grpc.io"; pub const CONFIG_PATH: &str = "config/log4rs.yml"; pub async fn build_client() -> LandingServiceClient { - let is_tls = match env::var("GRPC_HELLO_SECURE") { - Ok(val) => val, - Err(_e) => String::default(), - }; - - if cfg!(windows) { - println!("this is windows"); - } + let is_tls = env::var("GRPC_HELLO_SECURE").unwrap_or_else(|_e| String::default()); if is_tls.eq("Y") { - let address = format!("http://{}:{}", grpc_backend_host(), grpc_backend_port()); - + let address = format!("https://{}:{}", grpc_backend_host(), grpc_backend_port()); //https://myssl.com/create_test_cert.html + #[cfg(target_os = "windows")] + let cert = include_str!("d:\\garden\\var\\hello_grpc\\client_certs\\full_chain.pem"); + #[cfg(target_os = "windows")] + let key = include_str!("d:\\garden\\var\\hello_grpc\\client_certs\\private.key"); + #[cfg(target_os = "windows")] + let ca = include_str!("d:\\garden\\var\\hello_grpc\\client_certs\\full_chain.pem"); - // let cert = include_str!("D:\\ssh\\client_certs\\full_chain.pem"); - // let key = include_str!("D:\\ssh\\client_certs\\private.key"); - // let ca = include_str!("D:\\ssh\\client_certs\\full_chain.pem"); - + #[cfg(target_os = "linux")] let cert = include_str!("/var/hello_grpc/client_certs/full_chain.pem"); + #[cfg(target_os = "linux")] let key = include_str!("/var/hello_grpc/client_certs/private.key"); + #[cfg(target_os = "linux")] let ca = include_str!("/var/hello_grpc/client_certs/full_chain.pem"); + #[cfg(target_os = "macos")] + let cert = include_str!("/usr/local/hello_grpc/client_certs/full_chain.pem"); + #[cfg(target_os = "macos")] + let key = include_str!("/usr/local/hello_grpc/client_certs/private.key"); + #[cfg(target_os = "macos")] + let ca = include_str!("/usr/local/hello_grpc/client_certs/full_chain.pem"); + // creating identify from key and certificate let id = Identity::from_pem(cert.as_bytes(), key.as_bytes()); let ca = Certificate::from_pem(ca.as_bytes()); @@ -57,19 +61,13 @@ pub async fn build_client() -> LandingServiceClient { let address = format!("http://{}:{}", grpc_backend_host(), grpc_backend_port()); info!("Connect With InSecure(:{})", grpc_backend_port()); info!("Connect With InSecure(:{})", address); - return match LandingServiceClient::connect(address).await { - Ok(client) => client, - Err(error) => { - panic!("Problem opening the file: {:?}", error) - } - }; + LandingServiceClient::connect(address).await.unwrap_or_else(|error| { + panic!("Problem opening the file: {:?}", error) + }) } fn grpc_server() -> String { - match env::var("GRPC_SERVER") { - Ok(val) => val, - Err(_e) => "[::1]".to_string(), - } + env::var("GRPC_SERVER").unwrap_or_else(|_e| "[::1]".to_string()) } pub fn has_backend() -> bool { @@ -80,18 +78,12 @@ pub fn has_backend() -> bool { } pub fn grpc_backend_host() -> String { - match env::var("GRPC_HELLO_BACKEND") { - Ok(val) => val, - Err(_e) => grpc_server(), - } + env::var("GRPC_HELLO_BACKEND").unwrap_or_else(|_e| grpc_server()) } fn grpc_backend_port() -> String { - match env::var("GRPC_HELLO_BACKEND_PORT") { + env::var("GRPC_HELLO_BACKEND_PORT").unwrap_or_else(|_e| match env::var("GRPC_SERVER_PORT") { Ok(val) => val, - Err(_e) => match env::var("GRPC_SERVER_PORT") { - Ok(val) => val, - Err(_e) => "9996".to_string(), - }, - } + Err(_e) => "9996".to_string(), + }) } diff --git a/hello-grpc-rust/src/common/trans.rs b/hello-grpc-rust/src/common/trans.rs index 3526d83..4436f21 100644 --- a/hello-grpc-rust/src/common/trans.rs +++ b/hello-grpc-rust/src/common/trans.rs @@ -10,9 +10,32 @@ pub static TRACING_KEYS: [&'static str; 7] = [ "x-b3-flags", "x-ot-span-context", ]; - -//https://myssl.com/create_test_cert.html +// 对于 Linux 和类 Unix 系统 +#[cfg(unix)] pub const CERT: &str = "/var/hello_grpc/server_certs/cert.pem"; +#[cfg(unix)] pub const CERT_KEY: &str = "/var/hello_grpc/server_certs/private.key"; +#[cfg(unix)] pub const CERT_CHAIN: &str = "/var/hello_grpc/server_certs/full_chain.pem"; -pub const ROOT_CERT: &str = "/var/hello_grpc/server_certs/myssl_root.cer"; \ No newline at end of file +#[cfg(unix)] +pub const ROOT_CERT: &str = "/var/hello_grpc/server_certs/myssl_root.cer"; + +// 对于 Windows +#[cfg(target_os = "windows")] +pub const CERT: &str = "d:\\garden\\var\\hello_grpc\\server_certs\\cert.pem"; +#[cfg(target_os = "windows")] +pub const CERT_KEY: &str = "d:\\garden\\var\\hello_grpc\\server_certs\\private.key"; +#[cfg(target_os = "windows")] +pub const CERT_CHAIN: &str = "d:\\garden\\var\\hello_grpc\\server_certs\\full_chain.pem"; +#[cfg(target_os = "windows")] +pub const ROOT_CERT: &str = "d:\\garden\\var\\hello_grpc\\server_certs\\myssl_root.cer"; + +// 对于 macOS +#[cfg(target_os = "macos")] +pub const CERT: &str = "/usr/local/hello_grpc/server_certs/cert.pem"; +#[cfg(target_os = "macos")] +pub const CERT_KEY: &str = "/usr/local/hello_grpc/server_certs/private.key"; +#[cfg(target_os = "macos")] +pub const CERT_CHAIN: &str = "/usr/local/hello_grpc/server_certs/full_chain.pem"; +#[cfg(target_os = "macos")] +pub const ROOT_CERT: &str = "/usr/local/hello_grpc/server_certs/myssl_root.cer"; \ No newline at end of file diff --git a/hello-grpc-rust/src/landing/client.rs b/hello-grpc-rust/src/landing/client.rs index f55a695..ed7c3c3 100644 --- a/hello-grpc-rust/src/landing/client.rs +++ b/hello-grpc-rust/src/landing/client.rs @@ -14,7 +14,7 @@ use hello_grpc_rust::common::utils::{build_link_requests, random_id}; #[tokio::main] async fn main() -> Result<(), Box> { - log4rs::init_file(CONFIG_PATH, Default::default()).unwrap(); + log4rs::init_file(CONFIG_PATH, Default::default())?; let mut client = build_client().await; diff --git a/hello-grpc-rust/src/landing/server.rs b/hello-grpc-rust/src/landing/server.rs index fef4ea4..6dc2835 100644 --- a/hello-grpc-rust/src/landing/server.rs +++ b/hello-grpc-rust/src/landing/server.rs @@ -2,7 +2,6 @@ use std::collections::HashMap; use std::env; use std::error::Error; use std::pin::Pin; - use chrono::prelude::*; use futures::stream; use futures::{Stream, StreamExt}; @@ -26,13 +25,10 @@ use hello_grpc_rust::common::utils::{thanks, HELLOS}; #[tokio::main] async fn main() -> Result<(), Box> { - log4rs::init_file(CONFIG_PATH, Default::default()).unwrap(); + log4rs::init_file(CONFIG_PATH, Default::default())?; let address = format!("[::0]:{}", get_server_port()).parse().unwrap(); - let is_tls = match env::var("GRPC_HELLO_SECURE") { - Ok(val) => val, - Err(_e) => String::default(), - }; + let is_tls = env::var("GRPC_HELLO_SECURE").unwrap_or_else(|_e| String::default()); let mut server = if is_tls.eq("Y") { let cert = tokio::fs::read(CERT_CHAIN).await?; @@ -278,7 +274,7 @@ fn build_result(id: String) -> TalkResult { r#type: ok, kv: map, }; - return result; + result } fn print_metadata(header: &MetadataMap) { @@ -309,8 +305,5 @@ fn propaganda_headers(request: &mut Request) -> MetadataMap { } fn get_server_port() -> String { - return match env::var("GRPC_SERVER_PORT") { - Ok(val) => val, - Err(_e) => "9996".to_string(), - }; + env::var("GRPC_SERVER_PORT").unwrap_or_else(|_e| "9996".to_string()) }