Skip to content

Commit

Permalink
Move AppInfo into spin-http
Browse files Browse the repository at this point in the history
Signed-off-by: Brian H <brian.hardock@fermyon.com>
  • Loading branch information
fibonacci1729 committed Jul 6, 2023
1 parent 6aa2e1a commit 5bce096
Show file tree
Hide file tree
Showing 5 changed files with 42 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.

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" }
36 changes: 36 additions & 0 deletions crates/http/src/app_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use serde::{Deserialize, Serialize};
use spin_app::{App, MetadataKey};

pub const APP_NAME_KEY: MetadataKey<String> = MetadataKey::new("name");
pub const APP_VERSION_KEY: MetadataKey = MetadataKey::new("version");
pub const BINDLE_VERSION_KEY: MetadataKey = MetadataKey::new("bindle_version");
pub const OCI_IMAGE_DIGEST_KEY: MetadataKey = MetadataKey::new("oci_image_digest");

#[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 5bce096

Please sign in to comment.