From 659ccb0859e4673ad5ff8cb0971a2ee905693a77 Mon Sep 17 00:00:00 2001 From: Raphael Titsworth-Morin Date: Fri, 20 Sep 2024 20:09:15 +0000 Subject: [PATCH 1/3] initial working version of rocket sample --- samples/rocket/.devcontainer/Dockerfile | 1 + .../rocket/.devcontainer/devcontainer.json | 18 ++++++++ samples/rocket/.github/workflows/deploy.yaml | 27 +++++++++++ samples/rocket/README.md | 45 +++++++++++++++++++ samples/rocket/app/.dockerignore | 4 ++ samples/rocket/app/.gitignore | 28 ++++++++++++ samples/rocket/app/Cargo.toml | 11 +++++ samples/rocket/app/Dockerfile | 35 +++++++++++++++ samples/rocket/app/src/main.rs | 16 +++++++ samples/rocket/app/templates/index.html.tera | 29 ++++++++++++ samples/rocket/compose.dev.yaml | 8 ++++ samples/rocket/compose.yaml | 15 +++++++ 12 files changed, 237 insertions(+) create mode 100644 samples/rocket/.devcontainer/Dockerfile create mode 100644 samples/rocket/.devcontainer/devcontainer.json create mode 100644 samples/rocket/.github/workflows/deploy.yaml create mode 100644 samples/rocket/README.md create mode 100644 samples/rocket/app/.dockerignore create mode 100644 samples/rocket/app/.gitignore create mode 100644 samples/rocket/app/Cargo.toml create mode 100644 samples/rocket/app/Dockerfile create mode 100644 samples/rocket/app/src/main.rs create mode 100644 samples/rocket/app/templates/index.html.tera create mode 100644 samples/rocket/compose.dev.yaml create mode 100644 samples/rocket/compose.yaml diff --git a/samples/rocket/.devcontainer/Dockerfile b/samples/rocket/.devcontainer/Dockerfile new file mode 100644 index 00000000..bb6abcf6 --- /dev/null +++ b/samples/rocket/.devcontainer/Dockerfile @@ -0,0 +1 @@ +FROM mcr.microsoft.com/devcontainers/rust:1-bookworm diff --git a/samples/rocket/.devcontainer/devcontainer.json b/samples/rocket/.devcontainer/devcontainer.json new file mode 100644 index 00000000..ec3da065 --- /dev/null +++ b/samples/rocket/.devcontainer/devcontainer.json @@ -0,0 +1,18 @@ +{ + "build": { + "dockerfile": "Dockerfile", + "context": ".." + }, + "features": { + "ghcr.io/defanglabs/devcontainer-feature/defang-cli:1.0.4": {}, + "ghcr.io/devcontainers/features/docker-in-docker:2": {} + }, + "customizations": { + "vscode": { + "extensions": [ + "karunamurti.tera", + "tamasfe.even-better-toml" + ] + } + } +} \ No newline at end of file diff --git a/samples/rocket/.github/workflows/deploy.yaml b/samples/rocket/.github/workflows/deploy.yaml new file mode 100644 index 00000000..72cd98c1 --- /dev/null +++ b/samples/rocket/.github/workflows/deploy.yaml @@ -0,0 +1,27 @@ +name: Deploy + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + permissions: + contents: read + id-token: write + + steps: + - name: Checkout Repo + uses: actions/checkout@v4 + + - name: Deploy + uses: DefangLabs/defang-github-action@v1.1.0 + #REMOVE_ME_AFTER_EDITING - Replace the following line with the list of environment variables you want to pass to the action + # or remove the lines if you don't need to pass any environment variables/secrets to the action + with: + config-env-vars: ENV1 ENV2 + env: + ENV1: ${{ secrets.ENV1 }} + ENV2: ${{ secrets.ENV2 }} \ No newline at end of file diff --git a/samples/rocket/README.md b/samples/rocket/README.md new file mode 100644 index 00000000..f76881a1 --- /dev/null +++ b/samples/rocket/README.md @@ -0,0 +1,45 @@ +# Rocket + +[1-click deploy](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-rocket-template%26template_owner%3DDefangSamples) + +This sample demonstrates how to deploy a very simple Rocket app. Rocket is a web framework for Rust. + +## Prerequisites + +1. [!NOTE] Download [Defang CLI](https://github.com/DefangLabs/defang) +2. (Optional) If you are using [Defang BYOC](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) authenticate against your AWS account +3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/) + +## Development + +To run the application locally, you can use the following command: + +```bash +docker compose up +``` + +## Deployment + +> [!NOTE] +> Download [Defang CLI](https://github.com/DefangLabs/defang) + +### Defang Playground + +Deploy your application to the defang playground by opening up your terminal and typing `defang up`. + +### BYOC (AWS) + +If you want to deploy to your own cloud account, you can use Defang BYOC: + +1. [Authenticate your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html), and that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`. +2. Run `defang up` in a terminal that has access to your AWS environment variables. + +--- + +Title: Rocket + +Short Description: A simple Rocket app. + +Tags: Rocket + +Languages: Rust diff --git a/samples/rocket/app/.dockerignore b/samples/rocket/app/.dockerignore new file mode 100644 index 00000000..ee724afc --- /dev/null +++ b/samples/rocket/app/.dockerignore @@ -0,0 +1,4 @@ +target +.cargo +**/*.sh +**/*.tar.gz \ No newline at end of file diff --git a/samples/rocket/app/.gitignore b/samples/rocket/app/.gitignore new file mode 100644 index 00000000..6f4e28eb --- /dev/null +++ b/samples/rocket/app/.gitignore @@ -0,0 +1,28 @@ +# Rust specific +target/ +**/*.rs.bk + +# Dependencies +/Cargo.lock + +# Build artifacts +/target/ + +# Generated by Cargo +**/*.rs.bk + +# Binaries +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Mac specific +.DS_Store + +# IDE specific +.idea/ +.vscode/ +*.swp +*.swo \ No newline at end of file diff --git a/samples/rocket/app/Cargo.toml b/samples/rocket/app/Cargo.toml new file mode 100644 index 00000000..5b937808 --- /dev/null +++ b/samples/rocket/app/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +rocket = "0.5.1" + +[dependencies.rocket_dyn_templates] +version = "0.2.0" +features = ["tera"] \ No newline at end of file diff --git a/samples/rocket/app/Dockerfile b/samples/rocket/app/Dockerfile new file mode 100644 index 00000000..66a02b61 --- /dev/null +++ b/samples/rocket/app/Dockerfile @@ -0,0 +1,35 @@ +FROM docker.io/rust:1-slim-bookworm AS build + +## cargo package name: customize here or provide via --build-arg +ARG pkg=app + +WORKDIR /build + +COPY . . + +RUN --mount=type=cache,target=/build/target \ + --mount=type=cache,target=/usr/local/cargo/registry \ + --mount=type=cache,target=/usr/local/cargo/git \ + set -eux; \ + cargo build --release; \ + objcopy --compress-debug-sections target/release/$pkg ./main + +################################################################################ + +FROM docker.io/debian:bookworm-slim + +WORKDIR /app + +## copy the main binary +COPY --from=build /build/main ./ + +## copy runtime assets which may or may not exist +COPY --from=build /build/Rocket.tom[l] ./static +COPY --from=build /build/stati[c] ./static +COPY --from=build /build/template[s] ./templates + +## ensure the container listens globally on port 8080 +ENV ROCKET_ADDRESS=0.0.0.0 +ENV ROCKET_PORT=8080 + +CMD ./main \ No newline at end of file diff --git a/samples/rocket/app/src/main.rs b/samples/rocket/app/src/main.rs new file mode 100644 index 00000000..1a591d94 --- /dev/null +++ b/samples/rocket/app/src/main.rs @@ -0,0 +1,16 @@ +#[macro_use] +extern crate rocket; + +use rocket_dyn_templates::{context, Template}; + +#[get("/")] +fn index() -> Template { + Template::render("index", context! {}) +} + +#[launch] +fn rocket() -> _ { + rocket::build() + .mount("/", routes![index]) + .attach(Template::fairing()) +} diff --git a/samples/rocket/app/templates/index.html.tera b/samples/rocket/app/templates/index.html.tera new file mode 100644 index 00000000..f05430fc --- /dev/null +++ b/samples/rocket/app/templates/index.html.tera @@ -0,0 +1,29 @@ + + + + + + Defang × Rocket × Rust + + + + +

Defang × Rocket × Rust

+

Check the docs at https://docs.defang.io/docs/intro

+ + \ No newline at end of file diff --git a/samples/rocket/compose.dev.yaml b/samples/rocket/compose.dev.yaml new file mode 100644 index 00000000..5247d26d --- /dev/null +++ b/samples/rocket/compose.dev.yaml @@ -0,0 +1,8 @@ +services: + app: + extends: + file: compose.yaml + service: app + healthcheck: {} + volumes: + - "./app/templates:/app/templates" \ No newline at end of file diff --git a/samples/rocket/compose.yaml b/samples/rocket/compose.yaml new file mode 100644 index 00000000..b75086cc --- /dev/null +++ b/samples/rocket/compose.yaml @@ -0,0 +1,15 @@ +services: + app: + # uncomment to add your own domain + # domainname: example.com + build: + context: ./app + dockerfile: Dockerfile + ports: + - target: 8080 + published: 8080 + mode: ingress + healthcheck: + # wget or curl required for healthchecks on services with a published port + # this gets parsed by Defang and provided to the load balancers as well + test: ["CMD", "curl", "-f", "http://localhost:8080" ] \ No newline at end of file From a1f7e2bf176cf3578cb54118cb685c5bbceee33b Mon Sep 17 00:00:00 2001 From: Raphael Titsworth-Morin Date: Fri, 20 Sep 2024 20:39:08 +0000 Subject: [PATCH 2/3] fix healthcheck --- samples/rocket/app/Dockerfile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/samples/rocket/app/Dockerfile b/samples/rocket/app/Dockerfile index 66a02b61..794b6885 100644 --- a/samples/rocket/app/Dockerfile +++ b/samples/rocket/app/Dockerfile @@ -18,6 +18,11 @@ RUN --mount=type=cache,target=/build/target \ FROM docker.io/debian:bookworm-slim +RUN apt-get update -qq \ + && apt-get install -y curl \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + WORKDIR /app ## copy the main binary From 71f8f2f7ca8c8e40d6104a720a0d6bc4893fabf6 Mon Sep 17 00:00:00 2001 From: Raphael Titsworth-Morin Date: Fri, 20 Sep 2024 20:55:48 +0000 Subject: [PATCH 3/3] rm dev compose (rocket doesn't do hot reloading out of the box anyway), remove placeholders in deploy.yaml, and fix sample file checker --- samples/rocket/.github/workflows/deploy.yaml | 9 +-------- samples/rocket/compose.dev.yaml | 8 -------- scripts/check-sample-files.sh | 2 +- 3 files changed, 2 insertions(+), 17 deletions(-) delete mode 100644 samples/rocket/compose.dev.yaml diff --git a/samples/rocket/.github/workflows/deploy.yaml b/samples/rocket/.github/workflows/deploy.yaml index 72cd98c1..03ea61cd 100644 --- a/samples/rocket/.github/workflows/deploy.yaml +++ b/samples/rocket/.github/workflows/deploy.yaml @@ -17,11 +17,4 @@ jobs: uses: actions/checkout@v4 - name: Deploy - uses: DefangLabs/defang-github-action@v1.1.0 - #REMOVE_ME_AFTER_EDITING - Replace the following line with the list of environment variables you want to pass to the action - # or remove the lines if you don't need to pass any environment variables/secrets to the action - with: - config-env-vars: ENV1 ENV2 - env: - ENV1: ${{ secrets.ENV1 }} - ENV2: ${{ secrets.ENV2 }} \ No newline at end of file + uses: DefangLabs/defang-github-action@v1.1.0 \ No newline at end of file diff --git a/samples/rocket/compose.dev.yaml b/samples/rocket/compose.dev.yaml deleted file mode 100644 index 5247d26d..00000000 --- a/samples/rocket/compose.dev.yaml +++ /dev/null @@ -1,8 +0,0 @@ -services: - app: - extends: - file: compose.yaml - service: app - healthcheck: {} - volumes: - - "./app/templates:/app/templates" \ No newline at end of file diff --git a/scripts/check-sample-files.sh b/scripts/check-sample-files.sh index 8312fa73..a47533d6 100755 --- a/scripts/check-sample-files.sh +++ b/scripts/check-sample-files.sh @@ -47,7 +47,7 @@ for dir in ./samples/*/; do fi # Check for #REMOVE_ME_AFTER_EDITING - matches=$(grep -rnH "#REMOVE_ME_AFTER_EDITING" $dir* | cut -d: -f1,2) + matches=$(grep -rnH "#REMOVE_ME_AFTER_EDITING" "$dir" | cut -d: -f1,2) if [ -n "$matches" ]; then echo "$matches" | while read -r line; do