Skip to content

Commit

Permalink
feat: Add opendal hello world example (shuttle-hq#142)
Browse files Browse the repository at this point in the history
* feat: Add opendal hello world example

Signed-off-by: Xuanwo <github@xuanwo.io>

* Move into rocket

Signed-off-by: Xuanwo <github@xuanwo.io>

* Update rocket/opendal-memory/README.md

---------

Signed-off-by: Xuanwo <github@xuanwo.io>
  • Loading branch information
Xuanwo authored Feb 16, 2024
1 parent 60543a6 commit 7385b51
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Rocket | [secrets](./rocket/secrets/) | Extract secrets from your Secrets.toml f
Rocket | [static-files](./rocket/static-files/) | Hello World page that serves static HTML and JS files | `cargo shuttle init --from shuttle-hq/shuttle-examples --subfolder rocket/static-files`
Rocket | [url-shortener](./rocket/url-shortener/) | URL shortener with Postgres storage | `cargo shuttle init --from shuttle-hq/shuttle-examples --subfolder rocket/url-shortener`
Rocket | [workspace](./rocket/workspace/) | A cargo workspace where one crate is a Shuttle service | `cargo shuttle init --from shuttle-hq/shuttle-examples --subfolder rocket/workspace`
Rocket | [opendal-memory](./rocket/opendal-memory/) | app with an OpenDAL memory storage | `cargo shuttle init --from shuttle-hq/shuttle-examples --subfolder rocket/opendal-memory`
Salvo | [hello-world](./salvo/hello-world/) | Hello World | `cargo shuttle init --template salvo`
Salvo | [image-rescaler](./salvo/image-rescaler/) | Hello World | `cargo shuttle init --from shuttle-hq/shuttle-examples --subfolder salvo/image-rescaler`
Serenity | [hello-world](./serenity/hello-world/) | Hello World Discord bot | `cargo shuttle init --template serenity`
Expand Down
13 changes: 13 additions & 0 deletions rocket/opendal-memory/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "opendal-memory"
version = "0.1.0"
edition = "2021"

[dependencies]
shuttle-runtime = "0.39.0"
shuttle-opendal = "0.39.0"
tokio = "1.26.0"
shuttle-rocket = "0.39.0"
opendal = "0.45.0"
rocket = { version = "0.5.0", features = ["json"] }
serde = { version = "1.0.148", features = ["derive"] }
10 changes: 10 additions & 0 deletions rocket/opendal-memory/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# OpenDAL Memory Storage

This example shows that how to connect to an in-memory storage using OpenDAL.

## Project structure

The project consists of the following files

- `Shuttle.toml` contains the name of the app
- `src/main.rs` is where all the magic happens - it creates a Shuttle service with two endpoints: one for adding new data and one for retrieving it back.
1 change: 1 addition & 0 deletions rocket/opendal-memory/Shuttle.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name = "rocket-opendal-memory-example-app"
51 changes: 51 additions & 0 deletions rocket/opendal-memory/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#[macro_use]
extern crate rocket;

use rocket::response::status::BadRequest;
use rocket::State;

use opendal::Operator;

struct MyState {
storage: Operator,
}

#[post("/<path>", data = "<data>")]
async fn add(
path: String,
data: String,
state: &State<MyState>,
) -> Result<String, BadRequest<String>> {
let bs = data.into_bytes();
let length = bs.len();

state
.storage
.write(&path, bs)
.await
.map_err(|e| BadRequest(e.to_string()))?;
Ok(format!("path {path} written {}B data", length))
}

#[get("/<path>")]
async fn retrieve(path: String, state: &State<MyState>) -> Result<String, BadRequest<String>> {
let bs = state
.storage
.read(&path)
.await
.map_err(|e| BadRequest(e.to_string()))?;
let data = String::from_utf8_lossy(&bs).to_string();
Ok(data)
}

#[shuttle_runtime::main]
async fn rocket(
#[shuttle_opendal::Opendal(scheme = "memory")] storage: Operator,
) -> shuttle_rocket::ShuttleRocket {
let state = MyState { storage };
let rocket = rocket::build()
.mount("/", routes![retrieve, add])
.manage(state);

Ok(rocket.into())
}

0 comments on commit 7385b51

Please sign in to comment.