Skip to content

Commit

Permalink
feat: cache port
Browse files Browse the repository at this point in the history
Co-authored-by: Steve Nguyen <[email protected]>
  • Loading branch information
thuan2172001 and zk-steve committed May 20, 2024
1 parent 186af5e commit a98641a
Show file tree
Hide file tree
Showing 26 changed files with 751 additions and 355 deletions.
521 changes: 233 additions & 288 deletions Cargo.lock

Large diffs are not rendered by default.

28 changes: 21 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,30 @@ COPY . .
RUN cargo build --release --all
RUN mv target/${CARGO_BUILD_TARGET}/release /out

FROM scratch AS prod
FROM alpine AS public-dev
WORKDIR /user
COPY src/public/config/00-default.toml 00-default.toml
COPY --from=builder /out/cli /usr/local/bin/rust-server
ENTRYPOINT ["/usr/local/bin/rust-server", "--config-path=*.toml"]
COPY --from=builder /out/cli /usr/local/bin/rust-api-server
COPY --from=bunyan /root/.cargo/bin/bunyan /usr/local/bin/
ENTRYPOINT ["/bin/sh"]
CMD ["-c", "/usr/local/bin/rust-api-server --config-path=*.toml | bunyan"]

FROM alpine AS dev
FROM alpine AS gpt-dev
WORKDIR /user
COPY src/public/config/00-default.toml 00-default.toml
COPY --from=builder /out/cli /usr/local/bin/rust-server
COPY src/gpt_answer_server/config/00-default.toml 00-default.toml
COPY --from=builder /out/gpt_answer_server /usr/local/bin/rust-grpc-server
COPY --from=bunyan /root/.cargo/bin/bunyan /usr/local/bin/
ENTRYPOINT ["/bin/sh"]
CMD ["-c", "/usr/local/bin/rust-server --config-path=*.toml | bunyan"]
CMD ["-c", "/usr/local/bin/rust-grpc-server --config-path=*.toml | bunyan"]

FROM scratch AS public-prod
WORKDIR /user
COPY src/public/config/00-default.toml 00-default.toml
COPY --from=builder /out/cli /usr/local/bin/rust-api-server
ENTRYPOINT ["/usr/local/bin/rust-api-server", "--config-path=*.toml"]

FROM scratch AS gpt-prod
WORKDIR /user
COPY src/gpt_answer_server/config/00-default.toml 00-default.toml
COPY --from=builder /out/gpt_answer_server /usr/local/bin/rust-grpc-server
ENTRYPOINT ["/usr/local/bin/rust-grpc-server", "--config-path=*.toml"]
14 changes: 10 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ POSTGRES_DIR="./src/adapter/src/repositories/postgres"
DATABASE_URL="postgres://postgres:[email protected]:5432/postgres"

PKG_NAME=rust-api-server
PKG_NAME_GRPC=rust-grpc-server
BUILD_VERSION=$(shell git describe --long)
BUILD_RELEASE=$(shell git describe --tags --abbrev=0)


lint:
cargo clippy --fix --allow-dirty --allow-staged

Expand All @@ -26,14 +26,20 @@ migrate-redo:

build:
PKG_NAME=rust-api-server
PKG_NAME_GRPC=rust-grpc-server
BUILD_VERSION=$(shell git describe --long)
BUILD_RELEASE=$(shell git describe --tags --abbrev=0)
BUILDKIT_PROGRESS=plain
DOCKER_BUILDKIT=1
docker build --ssh default -t $(PKG_NAME):$(BUILD_VERSION) --target=prod .
docker build -t $(PKG_NAME):$(BUILD_VERSION) --target=public-prod .
docker build -t $(PKG_NAME_GRPC):$(BUILD_VERSION) --target=gpt-prod .

build-dev:
BUILDKIT_PROGRESS=plain DOCKER_BUILDKIT=1 docker build --ssh default -t $(PKG_NAME):$(BUILD_VERSION) --target=dev .
BUILDKIT_PROGRESS=plain DOCKER_BUILDKIT=1 docker build -t $(PKG_NAME):$(BUILD_VERSION) --target=public-dev .
BUILDKIT_PROGRESS=plain DOCKER_BUILDKIT=1 docker build -t $(PKG_NAME_GRPC):$(BUILD_VERSION) --target=gpt-dev .

profiling-public:
CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph --root -- -c ./src/public/config/* -c ./deploy/local/custom.toml
CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph --root -- -c ./src/public/config/* -c ./deploy/local/custom.toml

profiling-gpt:
CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph --root -- -c ./src/gpt_answer_server/config/* -c ./deploy/local/gpt_answer_server_custom.toml
48 changes: 26 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ tags [here](https://hub.docker.com/r/thuan2172001/rust-server/tags).

**Release Binaries:**
For every release, separate `cli` binaries are built. These binaries can be downloaded
from [here](https://github.com/sonntuet1997/rust-web-api-microservice-template/releases) and are available for various operating
from [here](https://github.com/sonntuet1997/rust-web-api-microservice-template/releases) and are available for various operating
systems and architectures. You are welcome to use the automated binaries or build your own.

**Contributions and PRs:**
Expand Down Expand Up @@ -95,26 +95,30 @@ options:

Hierarchical child config via env, separated by using `__`. Specify list values by using `,` separator

| ENV | DEFAULT VALUE | NOTE |
|------------------------------------------------------------------------|---------------|-----------|
| [RUST_LOG](https://docs.rs/env_logger/latest/env_logger/) > LOG__LEVEL | "INFO" | Log level |
| SERVER__URL | | |
| SERVER__PORT | | |
| SERVICE_NAME | | |
| EXPORTER_ENDPOINT | | |
| DB__PG__URL | | |
| DB__PG__MAX_SIZE | | |
| ENV | DEFAULT VALUE | NOTE |
| ------------------------------------------------------------------------ | ------------- | --------- |
| [RUST_LOG](https://docs.rs/env_logger/latest/env_logger/) > LOG\_\_LEVEL | "INFO" | Log level |
| SERVER\_\_URL | | |
| SERVER\_\_PORT | | |
| SERVICE_NAME | | |
| EXPORTER_ENDPOINT | | |
| DB\_\_PG\_\_URL | "localhost" | |
| DB\_\_PG\_\_MAX_SIZE | 5432 | |
| REDIS\_\_HOST | "localhost" | |
| REDIS\_\_PORT | 6379 | |

Make sure to set these environment variables according to your needs before running the server.

## GitHub Flow CI Configuration

1. **Set Docker Hub Secrets:**
- Go to repository Settings > Secrets.
- Add `DOCKER_USERNAME` and `DOCKERHUB_TOKEN`.

- Go to repository Settings > Secrets.
- Add `DOCKER_USERNAME` and `DOCKERHUB_TOKEN`.

2. **Enable Dependabot Alerts:**
- In repository Insights, enable "Dependabot alerts" and "Security & Analysis."

- In repository Insights, enable "Dependabot alerts" and "Security & Analysis".

## Checklist

Expand All @@ -126,7 +130,7 @@ include:
1. [x] Incorporating descriptive comments to enhance code readability.
2. [x] Implementing tracing mechanisms for effective debugging.
3. [x] Writing comprehensive test cases to validate functionality.
1. [x] Using https://testcontainers.com for integration tests.
1. [x] Using https://testcontainers.com for integration tests.
4. [x] Utilizing version control with [Git](https://git-scm.com/) for code management.
5. [x] Structuring code in a logical and maintainable manner.
6. [x] Containerizing the application using [Docker](https://www.docker.com/) for portability and scalability.
Expand All @@ -136,19 +140,19 @@ include:
Demonstrate proficiency in advanced development practices including:

1. [x] CLI Interface.
1. [x] Embed Git Info, Config Tool.
1. [x] Embed Git Info, Config Tool.
2. [x] Load Configuration from a File.
3. [x] Multiple Implementations.
4. [x] Advanced Tracing.
5. [x] CI/CD.
1. [x] Publish binary artifacts in [Github](https://github.com/).
2. [x] Push Docker images.
3. [x] Build pipeline on amd arch.
4. [ ] Build pipeline on arm arch.
1. [x] Publish binary artifacts in [Github](https://github.com/).
2. [x] Push Docker images.
3. [x] Build pipeline on amd arch.
4. [ ] Build pipeline on arm arch.
6. [x] Docker Image Optimization.
7. [x] Load test using [K6](https://k6.io/).
1. [x] Use [Flamegraph](https://github.com/flamegraph-rs/flamegraph) for profiling.
2. [ ] [Better UI](https://medium.com/swlh/beautiful-load-testing-with-k6-and-docker-compose-4454edb3a2e3).
1. [x] Use [Flamegraph](https://github.com/flamegraph-rs/flamegraph) for profiling.
2. [ ] [Better UI](https://medium.com/swlh/beautiful-load-testing-with-k6-and-docker-compose-4454edb3a2e3).
8. [ ] Comprehensive DB query filter for list().
9. [ ] Optimize release binary performance.
10. [ ] Docs on how to use this repo, the design behind the scene.
Expand All @@ -162,4 +166,4 @@ Feel free to explore and expand upon these functionalities as needed for your pr

For load testing and profiling your Rust API server, refer to
the [Load Testing and Profiling with K6 and Flamegraph](./load-tests/README.md) guide. This document provides
detailed instructions on using K6 and Flamegraph for load testing and profiling purposes.
detailed instructions on using K6 and Flamegraph for load testing and profiling purposes.
7 changes: 7 additions & 0 deletions deploy/docker/01_gpt_answer_server_custom.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
service_name = "rust-grpc-server"
exporter_endpoint = "http://localhost:7281"
server_endpoint = "0.0.0.0:50051"

[redis]
host = "redis"
port = 6379
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ level = "info"

[db.pg]
url = "postgres://postgres:changeme@postgres:5432/postgres"
max_size = 10
max_size = 10
53 changes: 37 additions & 16 deletions deploy/docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
version: "3.8"
version: '3.8'
networks:
rust-api-server:
name: "rust-api-server-docker"
name: 'rust-api-server-docker'
services:
quickwit:
image: quickwit/quickwit:latest
command: run
restart: always
environment:
QW_ENABLE_OTLP_ENDPOINT: "true"
QW_ENABLE_JAEGER_ENDPOINT: "true"
QW_ENABLE_OTLP_ENDPOINT: 'true'
QW_ENABLE_JAEGER_ENDPOINT: 'true'
ports:
- "7280:7280"
- "7281:7281"
- '7280:7280'
- '7281:7281'
jaeger:
image: jaegertracing/jaeger-query:latest
restart: always
depends_on:
- quickwit
environment:
SPAN_STORAGE_TYPE: "grpc-plugin"
GRPC_STORAGE_SERVER: "quickwit:7281"
SPAN_STORAGE_TYPE: 'grpc-plugin'
GRPC_STORAGE_SERVER: 'quickwit:7281'
ports:
- "16686:16686"
- '16686:16686'
postgres:
image: postgres
environment:
Expand All @@ -32,15 +32,36 @@ services:
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
server:
image: rust-server
- '5432:5432'
redis:
image: redis:latest
restart: always
volumes:
- redis:/data
ports:
- '6379:6379'
public_server:
image: rust-api-server
build:
context: ../..
target: public-dev
ports:
- '8888:8888'
volumes:
- ./01_public_custom.toml:/user/01_custom.toml:ro
depends_on:
- postgres
gpt_answer_server:
image: rust-grpc-server
build:
context: ../..
target: dev
target: gpt-dev
ports:
- "8888:8888"
- '50051:50051'
volumes:
- ./01-custom.toml:/user/01-custom.toml:ro
- ./01_gpt_answer_server_custom.toml:/user/01_custom.toml:ro
depends_on:
- redis
volumes:
postgres:
postgres:
redis:
10 changes: 9 additions & 1 deletion deploy/local/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,13 @@ services:
- postgres:/data/postgres
ports:
- "5432:5432"
redis:
image: redis:latest
restart: always
volumes:
- redis:/data
ports:
- "6379:6379"
volumes:
postgres:
postgres:
redis:
7 changes: 7 additions & 0 deletions deploy/local/gpt_answer_server_custom.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
service_name = "rust-grpc-server-local"
exporter_endpoint = "http://localhost:7281"
server_endpoint = "0.0.0.0:50051"

[redis]
host = "redis"
port = 6379
5 changes: 4 additions & 1 deletion src/adapter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ features = ["derive"]

[dependencies.testcontainers-modules]
version = "0.4.3"
features = ["postgres"]
features = ["postgres", "redis"]

[dependencies.tokio]
version = "1.36.0"
Expand All @@ -49,3 +49,6 @@ version = "1.0.83"

[dependencies.tonic]
version = "0.11.0"

[dependencies.redis-async]
version = "0.17.1"
Loading

0 comments on commit a98641a

Please sign in to comment.