WIP: Initial dropshot server

This commit is contained in:
Daniel Lundin 2024-11-25 20:37:41 +01:00
parent af28c60984
commit 1317c3b721
Signed by: dln
SSH key fingerprint: SHA256:dQy1Xj3UiqJYpKR5ggQ2bxgz4jCH8IF+k3AB8o0kmdI
5 changed files with 1316 additions and 19 deletions
controller

View file

@ -7,10 +7,17 @@ license = "MPL-2.0"
[dependencies]
anyhow.workspace = true
clap.workspace = true
dropshot.workspace = true
http.workspace = true
schemars.workspace = true
serde.workspace = true
slog.workspace = true
slog-async.workspace = true
tokio.workspace = true
tracing.workspace = true
tracing-chrome.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true
tracing-slog.workspace = true
[[bin]]
name = "patagia-controller"

View file

@ -1,12 +1,43 @@
use anyhow::Result;
use anyhow::{anyhow, Result};
use clap::Parser;
use tokio::time::{sleep, Duration};
use dropshot::{
endpoint, ApiDescription, ConfigDropshot, HttpError, HttpResponseOk, RequestContext,
ServerBuilder,
};
use schemars::JsonSchema;
use serde::Serialize;
use slog::Drain;
use tracing_slog::TracingSlogDrain;
use tracing_subscriber::prelude::*;
use std::net::SocketAddr;
use std::str::FromStr;
use std::sync::Arc;
#[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",
}]
async fn api_version(_: RequestContext<Arc<()>>) -> Result<HttpResponseOk<VersionInfo>, HttpError> {
let ver = VersionInfo {
name: env!("CARGO_PKG_NAME").to_string(),
version: env!("CARGO_PKG_VERSION").to_string(),
};
Ok(HttpResponseOk(ver))
}
#[tokio::main]
async fn main() -> Result<()> {
let _args = Cli::parse();
@ -19,6 +50,25 @@ async fn main() -> Result<()> {
tracing::info!("Patagia Controller");
sleep(Duration::from_secs(3)).await;
Ok(())
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()
};
// Adapt the Dropshot logger to tracing
let logger = {
let level_drain = slog::LevelFilter(TracingSlogDrain, slog::Level::Debug).fuse();
let async_drain = slog_async::Async::new(level_drain).build().fuse();
slog::Logger::root(async_drain, slog::o!())
};
ServerBuilder::new(api, Arc::new(()), logger)
.config(config)
.start()
.map_err(|e| anyhow!("Error starting server: {:?}", e))?
.await
.map_err(|e| anyhow!(e))
}