Skip to content

Commit

Permalink
Merge pull request #1634 from fibonacci1729/share-app-info
Browse files Browse the repository at this point in the history
Move AppInfo into spin-http
  • Loading branch information
fibonacci1729 authored Jul 7, 2023
2 parents 81efab9 + 1a516da commit 173e866
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 18 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

9 changes: 9 additions & 0 deletions crates/app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ pub use host_component::DynamicHostComponent;
pub use locked::Variable;
pub use metadata::MetadataKey;

/// MetadataKey for extracting the application name.
pub const APP_NAME_KEY: MetadataKey = MetadataKey::new("name");
/// MetadataKey for extracting the application version.
pub const APP_VERSION_KEY: MetadataKey = MetadataKey::new("version");
/// MetadataKey for extracting the bindle version.
pub const BINDLE_VERSION_KEY: MetadataKey = MetadataKey::new("bindle_version");
/// MetadataKey for extracting the OCI image digest.
pub const OCI_IMAGE_DIGEST_KEY: MetadataKey = MetadataKey::new("oci_image_digest");

/// A trait for implementing the low-level operations needed to load an [`App`].
// TODO(lann): Should this migrate to spin-loader?
#[async_trait]
Expand Down
1 change: 1 addition & 0 deletions crates/http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ indexmap = "1"
percent-encoding = "2"
serde = { version = "1.0", features = ["derive"] }
tracing = { workspace = true }
spin-app = { path = "../app" }

[dev-dependencies]
spin-testing = { path = "../testing" }
31 changes: 31 additions & 0 deletions crates/http/src/app_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use serde::{Deserialize, Serialize};
use spin_app::{App, APP_NAME_KEY, APP_VERSION_KEY, BINDLE_VERSION_KEY, OCI_IMAGE_DIGEST_KEY};

#[derive(Debug, Serialize, Deserialize)]
pub struct AppInfo {
pub name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bindle_version: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub oci_image_digest: Option<String>,
}

impl AppInfo {
pub fn new(app: &App) -> Self {
let name = app
.get_metadata(APP_NAME_KEY)
.unwrap_or_default()
.unwrap_or_default();
let version = app.get_metadata(APP_VERSION_KEY).unwrap_or_default();
let bindle_version = app.get_metadata(BINDLE_VERSION_KEY).unwrap_or_default();
let oci_image_digest = app.get_metadata(OCI_IMAGE_DIGEST_KEY).unwrap_or_default();
Self {
name,
version,
bindle_version,
oci_image_digest,
}
}
}
1 change: 1 addition & 0 deletions crates/http/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod app_info;
pub mod config;
pub mod routes;
pub mod wagi;
Expand Down
21 changes: 3 additions & 18 deletions crates/trigger-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ use serde::{Deserialize, Serialize};
use spin_app::{AppComponent, MetadataKey};
use spin_core::Engine;
use spin_http::{
app_info::AppInfo,
config::{HttpExecutorType, HttpTriggerConfig},
routes::{RoutePattern, Router},
};
use spin_trigger::{
locked::{BINDLE_VERSION_KEY, DESCRIPTION_KEY, VERSION_KEY},
EitherInstancePre, TriggerAppEngine, TriggerExecutor,
};
use spin_trigger::{locked::DESCRIPTION_KEY, EitherInstancePre, TriggerAppEngine, TriggerExecutor};
use tls_listener::TlsListener;
use tokio::net::{TcpListener, TcpStream};
use tokio_rustls::server::TlsStream;
Expand Down Expand Up @@ -269,11 +267,7 @@ impl HttpTrigger {

/// Returns spin status information.
fn app_info(&self) -> Result<Response<Body>> {
let info = AppInfo {
name: self.engine.app_name.clone(),
version: self.engine.app().get_metadata(VERSION_KEY)?,
bindle_version: self.engine.app().get_metadata(BINDLE_VERSION_KEY)?,
};
let info = AppInfo::new(self.engine.app());
let body = serde_json::to_vec_pretty(&info)?;
Ok(Response::builder()
.header("content-type", "application/json")
Expand Down Expand Up @@ -366,15 +360,6 @@ impl HttpTrigger {
}
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AppInfo {
pub name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bindle_version: Option<String>,
}

fn parse_listen_addr(addr: &str) -> anyhow::Result<SocketAddr> {
let addrs: Vec<SocketAddr> = addr.to_socket_addrs()?.collect();
// Prefer 127.0.0.1 over e.g. [::1] because CHANGE IS HARD
Expand Down

0 comments on commit 173e866

Please sign in to comment.