forked from shuttle-hq/shuttle-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add opendal hello world example (shuttle-hq#142)
* 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
Showing
5 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
name = "rocket-opendal-memory-example-app" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |