Skip to content

Commit

Permalink
Merge pull request #204 from DefangLabs/rocket
Browse files Browse the repository at this point in the history
Rocket sample
  • Loading branch information
raphaeltm committed Sep 20, 2024
2 parents 6c4ebc0 + 71f8f2f commit f3c35e4
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 1 deletion.
1 change: 1 addition & 0 deletions samples/rocket/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM mcr.microsoft.com/devcontainers/rust:1-bookworm
18 changes: 18 additions & 0 deletions samples/rocket/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
}
20 changes: 20 additions & 0 deletions samples/rocket/.github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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/[email protected]
45 changes: 45 additions & 0 deletions samples/rocket/README.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions samples/rocket/app/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
.cargo
**/*.sh
**/*.tar.gz
28 changes: 28 additions & 0 deletions samples/rocket/app/.gitignore
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions samples/rocket/app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"]
40 changes: 40 additions & 0 deletions samples/rocket/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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

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
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
16 changes: 16 additions & 0 deletions samples/rocket/app/src/main.rs
Original file line number Diff line number Diff line change
@@ -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())
}
29 changes: 29 additions & 0 deletions samples/rocket/app/templates/index.html.tera
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Defang × Rocket × Rust</title>
<link href="https://fonts.googleapis.com/css2?family=Exo+2:wght@400;700&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Exo 2', sans-serif;
min-width: 100vw;
min-height: 100vh;
color: #ffffff;
background-image: linear-gradient(311deg, rgba(63, 178, 175, 0.67), rgba(80, 54, 163, 0.67) 53%, rgba(9, 23, 76, 0.85)), linear-gradient(54deg, rgba(255, 131, 122, 0.25), rgba(255, 131, 122, 0) 28%), linear-gradient(241deg, rgba(228, 122, 255, 0.32), rgb(212, 240, 248) 36%);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
a {
color: #ffffff;
}
</style>
</head>
<body>
<h1>Defang × Rocket × Rust</h1>
<p>Check the docs at <a href="https://docs.defang.io/docs/intro">https://docs.defang.io/docs/intro</a></p>
</body>
</html>
15 changes: 15 additions & 0 deletions samples/rocket/compose.yaml
Original file line number Diff line number Diff line change
@@ -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" ]
2 changes: 1 addition & 1 deletion scripts/check-sample-files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f3c35e4

Please sign in to comment.