Skip to content

Commit

Permalink
Add rofl-appd REST API server
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Dec 19, 2024
1 parent b890760 commit 7ad7fa8
Show file tree
Hide file tree
Showing 10 changed files with 735 additions and 9 deletions.
650 changes: 643 additions & 7 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ members = [

# ROFL.
"rofl-utils",
"rofl-appd",
"rofl-containers",

# Test runtimes.
Expand Down
12 changes: 12 additions & 0 deletions rofl-appd/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "rofl-appd"
version = "0.1.0"
edition = "2021"

[dependencies]
# Oasis SDK.
oasis-runtime-sdk = { path = "../runtime-sdk", features = ["tdx"] }

# Third party.
rocket = { version = "0.5.1", features = ["json"] }

28 changes: 28 additions & 0 deletions rofl-appd/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! REST API daemon accessible by ROFL apps.
mod routes;
mod state;

use std::sync::Arc;

use rocket::{figment::Figment, routes};

use oasis_runtime_sdk::modules::rofl::app::{App, Environment};

/// Start the REST API server.
pub async fn start<A>(address: &str, env: Environment<A>) -> Result<(), rocket::Error>
where
A: App,
{
let env: Arc<dyn state::Env> = Arc::new(state::EnvImpl::new(env));
let cfg = Figment::new().join(("address", address));

rocket::custom(cfg)
.manage(env)
.mount("/rofl/v1/app", routes![routes::app::id,])
.mount("/rofl/v1/keys", routes![routes::keys::generate,])
.launch()
.await?;

Ok(())
}
10 changes: 10 additions & 0 deletions rofl-appd/src/routes/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::sync::Arc;

use rocket::State;

use crate::state::Env;

#[rocket::get("/id")]
pub fn id(env: &State<Arc<dyn Env>>) -> String {
env.app_id().to_bech32()
}
11 changes: 11 additions & 0 deletions rofl-appd/src/routes/keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::sync::Arc;

use rocket::State;

use crate::state::Env;

/// Key generation endpoint.
#[rocket::post("/generate")]
pub fn generate(env: &State<Arc<dyn Env>>) -> &'static str {
"TODO"
}
2 changes: 2 additions & 0 deletions rofl-appd/src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod app;
pub mod keys;
21 changes: 21 additions & 0 deletions rofl-appd/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use oasis_runtime_sdk::modules::rofl::app::{App, AppId, Environment};

pub trait Env: Send + Sync {
fn app_id(&self) -> AppId;
}

pub(crate) struct EnvImpl<A: App> {
env: Environment<A>,
}

impl<A: App> EnvImpl<A> {
pub fn new(env: Environment<A>) -> Self {
Self { env }
}
}

impl<A: App> Env for EnvImpl<A> {
fn app_id(&self) -> AppId {
A::id()
}
}
1 change: 1 addition & 0 deletions rofl-containers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
# Oasis SDK.
oasis-runtime-sdk = { path = "../runtime-sdk", features = ["tdx"] }
rofl-appd = { path = "../rofl-appd" }

# Third party.
base64 = "0.22.1"
8 changes: 6 additions & 2 deletions rofl-containers/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use std::env;
use base64::prelude::*;
use oasis_runtime_sdk::{cbor, modules::rofl::app::prelude::*};

/// UNIX socket address where the REST API server will listen on.
const ROFL_APPD_ADDRESS: &str = "unix:/run/rofl-appd.sock";

/// A generic container-based ROFL application.
struct ContainersApp;

Expand Down Expand Up @@ -30,8 +33,9 @@ impl App for ContainersApp {
.expect("Corrupted ROFL_CONSENSUS_TRUST_ROOT (must be Base64-encoded CBOR).")
}

async fn run(self: Arc<Self>, _env: Environment<Self>) {
// TODO: Start the REST API server.
async fn run(self: Arc<Self>, env: Environment<Self>) {
// Start the REST API server.
let _ = rofl_appd::start(ROFL_APPD_ADDRESS, env).await;
}
}

Expand Down

0 comments on commit 7ad7fa8

Please sign in to comment.