diff --git a/bin/mock-server/src/lib/lib.rs b/bin/mock-server/src/lib/lib.rs index 0c14738e7..9603fb882 100644 --- a/bin/mock-server/src/lib/lib.rs +++ b/bin/mock-server/src/lib/lib.rs @@ -14,7 +14,7 @@ use dropshot::{ TypedBody, WebsocketConnection, }; use futures::SinkExt; -use slog::{error, info, Logger}; +use slog::{error, info, o, Logger}; use thiserror::Error; use tokio::sync::{watch, Mutex}; use tokio_tungstenite::tungstenite::protocol::{Role, WebSocketConfig}; @@ -654,6 +654,10 @@ mod serial { /// Returns a Dropshot [`ApiDescription`] object to launch a mock Propolis /// server. +/// +/// This function should be avoided in favor of `start()` because using this +/// function requires that the consumer and Propolis update Dropshot +/// dependencies in lockstep due to the sharing of various types. pub fn api() -> ApiDescription> { let mut api = ApiDescription::new(); api.register(instance_ensure).unwrap(); @@ -664,3 +668,29 @@ pub fn api() -> ApiDescription> { api.register(instance_serial_history_get).unwrap(); api } + +// These types need to be exposed so that consumers have names for them without +// having to maintain a dropshot dependency in lockstep with their dependency on +// this crate. + +/// configuration for the dropshot server +pub type Config = dropshot::ConfigDropshot; +/// the dropshot server itself +pub type Server = dropshot::HttpServer>; +/// errors returned from attempting to start a dropshot server +// Dropshot should expose this, but it's going to be removed anyway. +pub type ServerStartError = Box; + +/// Starts a Propolis mock server +pub fn start(config: Config, log: Logger) -> Result { + let propolis_log = log.new(o!("component" => "propolis-server-mock")); + let dropshot_log = log.new(o!("component" => "dropshot")); + let private = Arc::new(Context::new(propolis_log)); + let starter = dropshot::HttpServerStarter::new( + &config, + api(), + private, + &dropshot_log, + )?; + Ok(starter.start()) +}