Skip to content

Commit

Permalink
neat rust
Browse files Browse the repository at this point in the history
  • Loading branch information
feuyeux committed Oct 1, 2024
1 parent 0cd0c03 commit 743f019
Show file tree
Hide file tree
Showing 20 changed files with 209 additions and 110 deletions.
4 changes: 2 additions & 2 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
14 changes: 14 additions & 0 deletions docker/cross_run_client.sh
Original file line number Diff line number Diff line change
@@ -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
18 changes: 9 additions & 9 deletions docker/grpc-cpp.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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/
Expand All @@ -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/
Expand Down
2 changes: 1 addition & 1 deletion docker/grpc-dart.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 25 additions & 4 deletions docker/java_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,35 @@ 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
mvn clean install -DskipTests -f server_pom.xml
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
}
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions docker/java_grpc.dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
4 changes: 3 additions & 1 deletion docker/kotlin_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions docker/build_rust.sh → docker/rust_build.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 8 additions & 8 deletions docker/grpc-rust.dockerfile → docker/rust_grpc.dockerfile
Original file line number Diff line number Diff line change
@@ -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 \
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions docker/rust_push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
docker push feuyeux/grpc_server_rust:1.0.0
docker push feuyeux/grpc_client_rust:1.0.0
13 changes: 13 additions & 0 deletions docker/rust_run_client.sh
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions docker/rust_run_server.sh
Original file line number Diff line number Diff line change
@@ -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
49 changes: 36 additions & 13 deletions docker/tls/copy_certs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions docker/tools/clean_world.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 14 additions & 15 deletions hello-grpc-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,42 @@ 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",
"macros",
"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"
5 changes: 2 additions & 3 deletions hello-grpc-rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ rustup show
```

```bash
# brew install protobuf
# brew link --overwrite protobuf
# protoc --version
sh ../docker/tls/copy_certs.sh
cargo build
```
Expand All @@ -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
```

Expand Down
Loading

0 comments on commit 743f019

Please sign in to comment.