Skip to content

Commit 3f847c3

Browse files
feat: cache port
Co-authored-by: Steve Nguyen <sonntuet1997@gmail.com>
1 parent 186af5e commit 3f847c3

26 files changed

+745
-350
lines changed

Cargo.lock

Lines changed: 233 additions & 288 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Dockerfile

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,30 @@ COPY . .
2020
RUN cargo build --release --all
2121
RUN mv target/${CARGO_BUILD_TARGET}/release /out
2222

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

29-
FROM alpine AS dev
31+
FROM alpine AS gpt-dev
3032
WORKDIR /user
31-
COPY src/public/config/00-default.toml 00-default.toml
32-
COPY --from=builder /out/cli /usr/local/bin/rust-server
33+
COPY src/gpt_answer_server/config/00-default.toml 00-default.toml
34+
COPY --from=builder /out/gpt_answer_server /usr/local/bin/rust-grpc-server
3335
COPY --from=bunyan /root/.cargo/bin/bunyan /usr/local/bin/
3436
ENTRYPOINT ["/bin/sh"]
35-
CMD ["-c", "/usr/local/bin/rust-server --config-path=*.toml | bunyan"]
37+
CMD ["-c", "/usr/local/bin/rust-grpc-server --config-path=*.toml | bunyan"]
38+
39+
FROM scratch AS public-prod
40+
WORKDIR /user
41+
COPY src/public/config/00-default.toml 00-default.toml
42+
COPY --from=builder /out/cli /usr/local/bin/rust-api-server
43+
ENTRYPOINT ["/usr/local/bin/rust-api-server", "--config-path=*.toml"]
44+
45+
FROM scratch AS gpt-prod
46+
WORKDIR /user
47+
COPY src/gpt_answer_server/config/00-default.toml 00-default.toml
48+
COPY --from=builder /out/gpt_answer_server /usr/local/bin/rust-grpc-server
49+
ENTRYPOINT ["/usr/local/bin/rust-grpc-server", "--config-path=*.toml"]

Makefile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ POSTGRES_DIR="./src/adapter/src/repositories/postgres"
22
DATABASE_URL="postgres://postgres:changeme@127.0.0.1:5432/postgres"
33

44
PKG_NAME=rust-api-server
5+
PKG_NAME_GRPC=rust-grpc-server
56
BUILD_VERSION=$(shell git describe --long)
67
BUILD_RELEASE=$(shell git describe --tags --abbrev=0)
78

8-
99
lint:
1010
cargo clippy --fix --allow-dirty --allow-staged
1111

@@ -26,14 +26,20 @@ migrate-redo:
2626

2727
build:
2828
PKG_NAME=rust-api-server
29+
PKG_NAME_GRPC=rust-grpc-server
2930
BUILD_VERSION=$(shell git describe --long)
3031
BUILD_RELEASE=$(shell git describe --tags --abbrev=0)
3132
BUILDKIT_PROGRESS=plain
3233
DOCKER_BUILDKIT=1
33-
docker build --ssh default -t $(PKG_NAME):$(BUILD_VERSION) --target=prod .
34+
docker build -t $(PKG_NAME):$(BUILD_VERSION) --target=public-prod .
35+
docker build -t $(PKG_NAME_GRPC):$(BUILD_VERSION) --target=gpt-prod .
3436

3537
build-dev:
36-
BUILDKIT_PROGRESS=plain DOCKER_BUILDKIT=1 docker build --ssh default -t $(PKG_NAME):$(BUILD_VERSION) --target=dev .
38+
BUILDKIT_PROGRESS=plain DOCKER_BUILDKIT=1 docker build -t $(PKG_NAME):$(BUILD_VERSION) --target=public-dev .
39+
BUILDKIT_PROGRESS=plain DOCKER_BUILDKIT=1 docker build -t $(PKG_NAME_GRPC):$(BUILD_VERSION) --target=gpt-dev .
3740

3841
profiling-public:
39-
CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph --root -- -c ./src/public/config/* -c ./deploy/local/custom.toml
42+
CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph --root -- -c ./src/public/config/* -c ./deploy/local/custom.toml
43+
44+
profiling-gpt:
45+
CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph --root -- -c ./src/gpt_answer_server/config/* -c ./deploy/local/gpt_answer_server_custom.toml

README.md

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ tags [here](https://hub.docker.com/r/thuan2172001/rust-server/tags).
1919

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

2525
**Contributions and PRs:**
@@ -98,23 +98,26 @@ Hierarchical child config via env, separated by using `__`. Specify list values
9898
| ENV | DEFAULT VALUE | NOTE |
9999
|------------------------------------------------------------------------|---------------|-----------|
100100
| [RUST_LOG](https://docs.rs/env_logger/latest/env_logger/) > LOG__LEVEL | "INFO" | Log level |
101-
| SERVER__URL | | |
102-
| SERVER__PORT | | |
101+
| SERVER\_\_URL | | |
102+
| SERVER\_\_PORT | | |
103103
| SERVICE_NAME | | |
104104
| EXPORTER_ENDPOINT | | |
105-
| DB__PG__URL | | |
106-
| DB__PG__MAX_SIZE | | |
105+
| DB\_\_PG\_\_URL | "localhost" | |
106+
| DB\_\_PG\_\_MAX_SIZE | 5432 | |
107+
| REDIS\_\_HOST | "localhost" | |
108+
| REDIS\_\_PORT | 6379 | |
107109

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

110112
## GitHub Flow CI Configuration
111113

112114
1. **Set Docker Hub Secrets:**
113-
- Go to repository Settings > Secrets.
114-
- Add `DOCKER_USERNAME` and `DOCKERHUB_TOKEN`.
115+
116+
- Go to repository Settings > Secrets.
117+
- Add `DOCKER_USERNAME` and `DOCKERHUB_TOKEN`.
115118

116119
2. **Enable Dependabot Alerts:**
117-
- In repository Insights, enable "Dependabot alerts" and "Security & Analysis."
120+
- In repository Insights, enable "Dependabot alerts" and "Security & Analysis."
118121

119122
## Checklist
120123

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

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

163166
For load testing and profiling your Rust API server, refer to
164167
the [Load Testing and Profiling with K6 and Flamegraph](./load-tests/README.md) guide. This document provides
165-
detailed instructions on using K6 and Flamegraph for load testing and profiling purposes.
168+
detailed instructions on using K6 and Flamegraph for load testing and profiling purposes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
service_name = "rust-grpc-server"
2+
exporter_endpoint = "http://localhost:7281"
3+
server_endpoint = "0.0.0.0:50051"
4+
5+
[redis]
6+
host = "redis"
7+
port = 6379

deploy/docker/01-custom.toml renamed to deploy/docker/01_public_custom.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ level = "info"
1010

1111
[db.pg]
1212
url = "postgres://postgres:changeme@postgres:5432/postgres"
13-
max_size = 10
13+
max_size = 10

deploy/docker/docker-compose.yaml

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
version: "3.8"
1+
version: '3.8'
22
networks:
33
rust-api-server:
4-
name: "rust-api-server-docker"
4+
name: 'rust-api-server-docker'
55
services:
66
quickwit:
77
image: quickwit/quickwit:latest
88
command: run
99
restart: always
1010
environment:
11-
QW_ENABLE_OTLP_ENDPOINT: "true"
12-
QW_ENABLE_JAEGER_ENDPOINT: "true"
11+
QW_ENABLE_OTLP_ENDPOINT: 'true'
12+
QW_ENABLE_JAEGER_ENDPOINT: 'true'
1313
ports:
14-
- "7280:7280"
15-
- "7281:7281"
14+
- '7280:7280'
15+
- '7281:7281'
1616
jaeger:
1717
image: jaegertracing/jaeger-query:latest
1818
restart: always
1919
depends_on:
2020
- quickwit
2121
environment:
22-
SPAN_STORAGE_TYPE: "grpc-plugin"
23-
GRPC_STORAGE_SERVER: "quickwit:7281"
22+
SPAN_STORAGE_TYPE: 'grpc-plugin'
23+
GRPC_STORAGE_SERVER: 'quickwit:7281'
2424
ports:
25-
- "16686:16686"
25+
- '16686:16686'
2626
postgres:
2727
image: postgres
2828
environment:
@@ -32,15 +32,36 @@ services:
3232
volumes:
3333
- postgres:/data/postgres
3434
ports:
35-
- "5432:5432"
36-
server:
37-
image: rust-server
35+
- '5432:5432'
36+
redis:
37+
image: redis:latest
38+
restart: always
39+
volumes:
40+
- redis:/data
41+
ports:
42+
- '6379:6379'
43+
public_server:
44+
image: rust-api-server
45+
build:
46+
context: ../..
47+
target: public-dev
48+
ports:
49+
- '8888:8888'
50+
volumes:
51+
- ./01_public_custom.toml:/user/01_custom.toml:ro
52+
depends_on:
53+
- postgres
54+
gpt_answer_server:
55+
image: rust-grpc-server
3856
build:
3957
context: ../..
40-
target: dev
58+
target: gpt-dev
4159
ports:
42-
- "8888:8888"
60+
- '9999:9999'
4361
volumes:
44-
- ./01-custom.toml:/user/01-custom.toml:ro
62+
- ./01_gpt_answer_server_custom.toml:/user/01_custom.toml:ro
63+
depends_on:
64+
- redis
4565
volumes:
46-
postgres:
66+
postgres:
67+
redis:

deploy/local/docker-compose.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,13 @@ services:
3333
- postgres:/data/postgres
3434
ports:
3535
- "5432:5432"
36+
redis:
37+
image: redis:latest
38+
restart: always
39+
volumes:
40+
- redis:/data
41+
ports:
42+
- "6379:6379"
3643
volumes:
37-
postgres:
44+
postgres:
45+
redis:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
service_name = "rust-grpc-server-local"
2+
exporter_endpoint = "http://localhost:7281"
3+
server_endpoint = "0.0.0.0:50051"
4+
5+
[redis]
6+
host = "redis"
7+
port = 6379

src/adapter/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ features = ["derive"]
3838

3939
[dependencies.testcontainers-modules]
4040
version = "0.4.3"
41-
features = ["postgres"]
41+
features = ["postgres", "redis"]
4242

4343
[dependencies.tokio]
4444
version = "1.36.0"
@@ -49,3 +49,6 @@ version = "1.0.83"
4949

5050
[dependencies.tonic]
5151
version = "0.11.0"
52+
53+
[dependencies.redis-async]
54+
version = "0.17.1"

0 commit comments

Comments
 (0)