Skip to content

Commit

Permalink
feat: secret wit interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslanti committed Sep 24, 2024
1 parent 4a049cc commit 6bff175
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[build]
target = "wasm32-wasi"
target = "wasm32-wasip1"
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions examples/secret/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "secret"
version = {workspace = true}
edition = "2021"
publish = false

[lib]
crate-type = ["cdylib"]

[dependencies]
fastedge = { path = "../../" }
anyhow = "1.0"
38 changes: 38 additions & 0 deletions examples/secret/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use anyhow::{Error, Result};

use fastedge::body::Body;
use fastedge::http::{Request, Response, StatusCode};
use fastedge::secret;

#[allow(dead_code)]
#[fastedge::http]
fn main(_req: Request<Body>) -> Result<Response<Body>> {

let value = match secret::get("SECRET") {
Ok(value) => value,
Err(secret::Error::AccessDenied) => {
return Response::builder()
.status(StatusCode::FORBIDDEN)
.body(Body::empty())
.map_err(Error::msg);
},
Err(secret::Error::Other(msg)) => {
return Response::builder()
.status(StatusCode::FORBIDDEN)
.body(Body::from(msg))
.map_err(Error::msg);
},
};

if value.is_none() {
return Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Body::empty())
.map_err(Error::msg);
}

Response::builder()
.status(StatusCode::OK)
.body(Body::empty())
.map_err(Error::msg)
}
4 changes: 0 additions & 4 deletions src/key_value.rs

This file was deleted.

3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub use http_client::send_request;
pub use crate::exports::gcore::fastedge::http_handler;
use crate::gcore::fastedge::http::{Error as HttpError, Method, Request, Response};
pub use crate::gcore::fastedge::key_value;
pub use crate::gcore::fastedge::secret;

/// Implementation of Outbound HTTP component
mod http_client;
Expand All @@ -25,7 +26,7 @@ pub mod wasi_nn {
}

wit_bindgen::generate!({
world: "reactor",
world: "http-reactor",
path: "wit",
pub_export_macro: true,
});
Expand Down
14 changes: 14 additions & 0 deletions wit/secret.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
interface secret {
/// Get the secret associated with the specified `key`
/// Returns `ok(none)` if the key does not exist.
get: func(key: string) -> result<option<string>, error>;

/// The set of errors which may be raised by functions in this interface
variant error {
/// The requesting component does not have access to the specified key
/// (which may or may not exist).
access-denied,
/// Some implementation-specific error has occurred (e.g. I/O)
other(string)
}
}
14 changes: 7 additions & 7 deletions wit/world.wit
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package gcore:fastedge;
package gcore: fastedge;

world http-reactor {
import http;
import http-client;
import http;
import http-client;

import dictionary;
import dictionary;
import key-value;
import secret;

import key-value;

export http-handler;
export http-handler;
}

0 comments on commit 6bff175

Please sign in to comment.