Skip to content

Commit

Permalink
Allow getting http trigger metadata from (locked)app from spin-http
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <[email protected]>
  • Loading branch information
rylev committed Aug 25, 2023
1 parent ceffe50 commit 40defaf
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
4 changes: 2 additions & 2 deletions crates/app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl<'a, L: MaybeLoader> App<'a, L> {
&'this self,
key: MetadataKey<T>,
) -> Result<Option<T>> {
self.locked.metadata.get_typed(key)
self.locked.get_metadata(key)
}

/// Deserializes typed metadata for this app.
Expand All @@ -185,7 +185,7 @@ impl<'a, L: MaybeLoader> App<'a, L> {
&'this self,
key: MetadataKey<T>,
) -> Result<T> {
self.locked.metadata.require_typed(key)
self.locked.require_metadata(key)
}

/// Returns an iterator of custom config [`Variable`]s defined for this app.
Expand Down
25 changes: 24 additions & 1 deletion crates/app/src/locked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::values::ValuesMap;
use crate::{metadata::MetadataExt, values::ValuesMap};

/// A String-keyed map with deterministic serialization order.
pub type LockedMap<T> = std::collections::BTreeMap<String, T>;
Expand Down Expand Up @@ -37,6 +37,29 @@ impl LockedApp {
pub fn to_json(&self) -> serde_json::Result<Vec<u8>> {
serde_json::to_vec_pretty(&self)
}

/// Deserializes typed metadata for this app.
///
/// Returns `Ok(None)` if there is no metadata for the given `key` and an
/// `Err` only if there _is_ a value for the `key` but the typed
/// deserialization failed.
pub fn get_metadata<'this, T: Deserialize<'this>>(
&'this self,
key: crate::MetadataKey<T>,
) -> crate::Result<Option<T>> {
self.metadata.get_typed(key)
}

/// Deserializes typed metadata for this app.
///
/// Like [`LockedApp::get_metadata`], but returns an error if there is
/// no metadata for the given `key`.
pub fn require_metadata<'this, T: Deserialize<'this>>(
&'this self,
key: crate::MetadataKey<T>,
) -> crate::Result<T> {
self.metadata.require_typed(key)
}
}

/// A LockedComponent represents a "fully resolved" Spin component.
Expand Down
1 change: 1 addition & 0 deletions crates/http/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod app_info;
pub mod config;
pub mod routes;
pub mod trigger;
pub mod wagi;

pub const WELL_KNOWN_PREFIX: &str = "/.well-known/spin/";
14 changes: 14 additions & 0 deletions crates/http/src/trigger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use serde::{Deserialize, Serialize};
use spin_app::MetadataKey;

/// Http trigger metadata key
pub const METADATA_KEY: MetadataKey<Metadata> = MetadataKey::new("trigger");

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Metadata {
// The type of trigger which should always been "http" in this case
pub r#type: String,
// The based url
pub base: String,
}
18 changes: 6 additions & 12 deletions crates/trigger-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ use hyper::{
service::{make_service_fn, service_fn},
Body, Request, Response, Server,
};
use serde::{Deserialize, Serialize};
use spin_app::{AppComponent, MetadataKey};
use spin_app::AppComponent;
use spin_core::Engine;
use spin_http::{
app_info::AppInfo,
Expand All @@ -44,8 +43,6 @@ pub use tls::TlsConfig;
pub(crate) type RuntimeData = ();
pub(crate) type Store = spin_core::Store<RuntimeData>;

const TRIGGER_METADATA_KEY: MetadataKey<TriggerMetadata> = MetadataKey::new("trigger");

/// The Spin HTTP trigger.
pub struct HttpTrigger {
engine: TriggerAppEngine<Self>,
Expand Down Expand Up @@ -84,13 +81,6 @@ impl CliArgs {
}
}

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct TriggerMetadata {
r#type: String,
base: String,
}

#[async_trait]
impl TriggerExecutor for HttpTrigger {
const TRIGGER_TYPE: &'static str = "http";
Expand All @@ -99,7 +89,10 @@ impl TriggerExecutor for HttpTrigger {
type RunConfig = CliArgs;

async fn new(engine: TriggerAppEngine<Self>) -> Result<Self> {
let base = engine.app().require_metadata(TRIGGER_METADATA_KEY)?.base;
let base = engine
.app()
.require_metadata(spin_http::trigger::METADATA_KEY)?
.base;

let component_routes = engine
.trigger_configs()
Expand Down Expand Up @@ -459,6 +452,7 @@ mod tests {
use std::collections::BTreeMap;

use anyhow::Result;
use serde::Deserialize;
use spin_testing::test_socket_addr;

use super::*;
Expand Down

0 comments on commit 40defaf

Please sign in to comment.