|
| 1 | +--- |
| 2 | +title: What's Next? |
| 3 | +slug: whats-next |
| 4 | +--- |
| 5 | + |
| 6 | +## Improve Development Environment |
| 7 | + |
| 8 | +> [!assignment] |
| 9 | +> Add missing files like `.dockerignore`, tests, validations for pagination, search filters for the list endpoint, and maybe add a new binary application to import books from a CSV file... |
| 10 | +
|
| 11 | +### Development `Dockerfile` |
| 12 | + |
| 13 | +Add a new file `book_service/Dockerfile`. |
| 14 | + |
| 15 | +```dockerfile |
| 16 | +FROM rust:1.97-slim |
| 17 | +WORKDIR /build/crates/book_service |
| 18 | + |
| 19 | +ENV CARGO_TARGET_DIR=/build/target |
| 20 | + |
| 21 | +RUN mkdir -p /service |
| 22 | + |
| 23 | +RUN --mount=type=bind,source=src,target=src \ |
| 24 | + --mount=type=bind,source=Cargo.toml,target=Cargo.toml \ |
| 25 | + --mount=type=bind,from=workspace_root,source=Cargo.lock,target=/build/Cargo.lock \ |
| 26 | + --mount=type=bind,from=workspace_root,source=Cargo.toml,target=/build/Cargo.toml \ |
| 27 | + --mount=type=bind,source=toasty,target=toasty \ |
| 28 | + --mount=type=bind,source=Toasty.toml,target=Toasty.toml \ |
| 29 | + --mount=type=cache,target=/build/target/ \ |
| 30 | + --mount=type=cache,target=/usr/local/cargo/registry/ \ |
| 31 | + <<EOF |
| 32 | +set -e |
| 33 | +cargo build --locked --release |
| 34 | +cp /build/target/release/app /service/app |
| 35 | +cp /build/target/release/migration /service/migration |
| 36 | +cp -r ./toasty /service/toasty/ |
| 37 | +cp ./Toasty.toml /service/Toasty.toml |
| 38 | +EOF |
| 39 | + |
| 40 | +WORKDIR /service |
| 41 | +CMD ["/service/app"] |
| 42 | + |
| 43 | +EXPOSE 3000 |
| 44 | +``` |
| 45 | + |
| 46 | +### Update `compose.yml` |
| 47 | + |
| 48 | +Update `book_service/compose.yml`. |
| 49 | + |
| 50 | +```yaml |
| 51 | +services: |
| 52 | + app: |
| 53 | + image: book-service-app |
| 54 | + env_file: .env |
| 55 | + build: |
| 56 | + context: . |
| 57 | + dockerfile: Dockerfile |
| 58 | + additional_contexts: |
| 59 | + workspace_root: ../.. |
| 60 | + depends_on: |
| 61 | + db: |
| 62 | + condition: service_healthy |
| 63 | + ports: |
| 64 | + - "3000:3000" |
| 65 | + command: [ "sh", "-c", "/service/migration migration apply && /service/app" ] |
| 66 | + restart: always |
| 67 | + |
| 68 | +# db |
| 69 | +``` |
| 70 | + |
| 71 | +## Improve for Production |
| 72 | + |
| 73 | +### Production `Dockerfile` |
| 74 | + |
| 75 | +Add a new file `book_service/prod.Dockerfile`. |
| 76 | + |
| 77 | +```dockerfile |
| 78 | +FROM rust:1.97-slim AS builder |
| 79 | + |
| 80 | +ARG CARGO_BUILD_TARGET=aarch64-unknown-linux-musl |
| 81 | + |
| 82 | +RUN apt-get update && apt-get install -y --no-install-recommends \ |
| 83 | + build-essential musl-tools && rm -rf /var/lib/apt/lists/* |
| 84 | + |
| 85 | +RUN rustup target add ${CARGO_BUILD_TARGET} |
| 86 | + |
| 87 | +WORKDIR /build/crates/book_service |
| 88 | + |
| 89 | +ENV CARGO_TARGET_DIR=/build/target \ |
| 90 | + CARGO_BUILD_TARGET=${CARGO_BUILD_TARGET} |
| 91 | + |
| 92 | +RUN mkdir -p /service |
| 93 | + |
| 94 | +RUN --mount=type=bind,source=src,target=src \ |
| 95 | + --mount=type=bind,source=Cargo.toml,target=Cargo.toml \ |
| 96 | + --mount=type=bind,from=workspace_root,source=Cargo.lock,target=/build/Cargo.lock \ |
| 97 | + --mount=type=bind,from=workspace_root,source=Cargo.toml,target=/build/Cargo.toml \ |
| 98 | + --mount=type=bind,source=toasty,target=toasty \ |
| 99 | + --mount=type=bind,source=Toasty.toml,target=Toasty.toml \ |
| 100 | + --mount=type=cache,target=/build/target/ \ |
| 101 | + --mount=type=cache,target=/usr/local/cargo/registry/ \ |
| 102 | + <<EOF |
| 103 | +set -e |
| 104 | +cargo build --locked --release |
| 105 | +cp /build/target/${CARGO_BUILD_TARGET}/release/app /service/app |
| 106 | +cp /build/target/${CARGO_BUILD_TARGET}/release/migration /service/migration |
| 107 | +cp -r ./toasty /service/toasty/ |
| 108 | +cp ./Toasty.toml /service/Toasty.toml |
| 109 | +EOF |
| 110 | + |
| 111 | +# ============================================================================== |
| 112 | +FROM gcr.io/distroless/static-debian13:nonroot |
| 113 | +WORKDIR /service |
| 114 | + |
| 115 | +COPY --from=builder /service/app /service/app |
| 116 | +COPY --from=builder /service/migration /service/migration |
| 117 | +COPY --from=builder /service/Toasty.toml /service/Toasty.toml |
| 118 | +COPY --from=builder /service/toasty /service/toasty/ |
| 119 | + |
| 120 | +CMD ["/service/app"] |
| 121 | +``` |
| 122 | + |
| 123 | +### Add `mimalloc` |
| 124 | + |
| 125 | +Run `cargo add mimalloc` from the book_service folder. Then update `book_service/src/bin/app.rs`. |
| 126 | + |
| 127 | +```rust |
| 128 | +use book_service::{AppConf, AppState, models::*, routes}; |
| 129 | +use mimalloc::MiMalloc; |
| 130 | +use tracing::info; |
| 131 | +use tracing_subscriber::{EnvFilter, layer::SubscriberExt, util::SubscriberInitExt}; |
| 132 | + |
| 133 | +#[global_allocator] |
| 134 | +static GLOBAL: MiMalloc = MiMalloc; |
| 135 | + |
| 136 | +#[tokio::main] |
| 137 | +``` |
0 commit comments