Add OpenAPI generation xtask

This commit is contained in:
Daniel Lundin 2024-12-11 21:12:24 +01:00
parent ac6a53fac9
commit a4097b7cc3
Signed by: dln
SSH key fingerprint: SHA256:dQy1Xj3UiqJYpKR5ggQ2bxgz4jCH8IF+k3AB8o0kmdI
14 changed files with 268 additions and 78 deletions

15
controller/src/api.rs Normal file
View file

@ -0,0 +1,15 @@
use anyhow::Result;
use dropshot::ApiDescription;
use std::sync::Arc;
use crate::context::ControllerContext;
use crate::version;
type ControllerApiDescription = ApiDescription<Arc<ControllerContext>>;
pub fn api() -> Result<ControllerApiDescription> {
let mut api = ControllerApiDescription::new();
api.register(version::api_version)?;
Ok(api)
}

View file

@ -1,67 +1,22 @@
use anyhow::{anyhow, Result};
use clap::Parser;
use dropshot::{
endpoint, ApiDescription, ConfigDropshot, HttpError, HttpResponseOk, RequestContext,
ServerBuilder,
};
use schemars::JsonSchema;
use serde::Serialize;
use dropshot::{ConfigDropshot, ServerBuilder};
use slog::Drain;
use tracing::Instrument;
use tracing_slog::TracingSlogDrain;
use std::net::SocketAddr;
use std::str::FromStr;
use std::sync::Arc;
use patagia_controller::api;
use patagia_controller::context::ControllerContext;
use patagia_controller::instrumentation;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Cli {}
/// Represents a project in our API.
#[derive(Serialize, JsonSchema)]
struct VersionInfo {
name: String,
version: String,
}
/// Fetch version info.
#[endpoint {
method = GET,
path = "/version",
}]
#[tracing::instrument(
skip(rqctx),
fields(
http.method=rqctx.request.method().as_str(),
http.path=rqctx.request.uri().path(),
http.remote_ip=rqctx.request.remote_addr().ip().to_string(),
request_id = rqctx.request_id,
),
err(Debug),
)]
async fn api_version(
rqctx: RequestContext<Arc<()>>,
) -> Result<HttpResponseOk<VersionInfo>, HttpError> {
let ver = VersionInfo {
name: env!("CARGO_PKG_NAME").to_string(),
version: env!("CARGO_PKG_VERSION").to_string(),
};
tracing::info_span!("Hello, span!");
async move {
tracing::info!("Someone made a request to /version");
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
}
.instrument(tracing::info_span!("Let's do the thing...."))
.await;
Ok(HttpResponseOk(ver))
}
#[tokio::main]
async fn main() -> Result<()> {
let _args = Cli::parse();
@ -69,15 +24,6 @@ async fn main() -> Result<()> {
tracing::info!("Patagia Controller");
tracing::debug!("Starting server");
tracing::error!(name: "my-event-name", target: "my-system", event_id = 20, user_name = "otel", user_email = "otel@opentelemetry.io", message = "This is an example message");
foo().await;
let mut api = ApiDescription::new();
api.register(api_version).unwrap();
let config = ConfigDropshot {
bind_address: SocketAddr::from_str("0.0.0.0:9474").unwrap(),
..Default::default()
@ -90,22 +36,12 @@ async fn main() -> Result<()> {
slog::Logger::root(async_drain, slog::o!())
};
ServerBuilder::new(api, Arc::new(()), logger)
let ctx = ControllerContext::new();
let api = api::api()?;
ServerBuilder::new(api, Arc::new(ctx), logger)
.config(config)
.start()
.map_err(|e| anyhow!("Error starting server: {:?}", e))?
.await
.map_err(|e| anyhow!(e))
}
#[tracing::instrument]
async fn foo() {
tracing::info!(
monotonic_counter.foo = 1_u64,
key_1 = "bar",
key_2 = 10,
"handle foo",
);
tracing::info!(histogram.baz = 10, "histogram example",);
}

13
controller/src/context.rs Normal file
View file

@ -0,0 +1,13 @@
pub struct ControllerContext {}
impl ControllerContext {
pub fn new() -> ControllerContext {
ControllerContext {}
}
}
impl Default for ControllerContext {
fn default() -> Self {
Self::new()
}
}

View file

@ -1 +1,5 @@
pub mod api;
pub mod context;
pub mod instrumentation;
mod version;

49
controller/src/version.rs Normal file
View file

@ -0,0 +1,49 @@
use dropshot::{endpoint, HttpError, HttpResponseOk, RequestContext};
use schemars::JsonSchema;
use serde::Serialize;
use tracing::Instrument;
use std::sync::Arc;
use crate::context::ControllerContext;
/// Version and build information
#[derive(Serialize, JsonSchema)]
struct VersionInfo {
name: String,
version: String,
}
/// Fetch version info.
#[endpoint {
method = GET,
path = "/version",
}]
#[tracing::instrument(
skip(rqctx),
fields(
http.method=rqctx.request.method().as_str(),
http.path=rqctx.request.uri().path(),
http.remote_ip=rqctx.request.remote_addr().ip().to_string(),
request_id = rqctx.request_id,
),
err(Debug),
)]
pub async fn api_version(
rqctx: RequestContext<Arc<ControllerContext>>,
) -> Result<HttpResponseOk<VersionInfo>, HttpError> {
let ver = VersionInfo {
name: env!("CARGO_PKG_NAME").to_string(),
version: env!("CARGO_PKG_VERSION").to_string(),
};
tracing::info_span!("Hello, span!");
async move {
tracing::info!("Someone made a request to /version");
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
}
.instrument(tracing::info_span!("Let's do the thing...."))
.await;
Ok(HttpResponseOk(ver))
}