WIP: Initial dropshot server
Some checks failed
ci/woodpecker/pr/ci Pipeline failed

This commit is contained in:
Daniel Lundin 2024-11-25 20:37:41 +01:00
parent af28c60984
commit 00128e39ce
Signed by: dln
SSH key fingerprint: SHA256:dQy1Xj3UiqJYpKR5ggQ2bxgz4jCH8IF+k3AB8o0kmdI
4 changed files with 1310 additions and 11 deletions

1247
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -20,9 +20,16 @@ clap = { version = "4.5.21", features = [
"wrap_help",
"string",
] }
dropshot = "0.13.0"
http = "1.1.0"
schemars = "0.8.21"
serde = "1.0.215"
slog = "2.7.0"
slog-async = "2.8.0"
tokio = { version = "1.41.1", features = ["full"] }
tracing = "0.1.40"
tracing-chrome = "0.7.2"
tracing-slog = { git = "https://github.com/oxidecomputer/tracing-slog", default-features = false }
tracing-subscriber = { version = "0.3.18", default-features = false, features = [
"std",
"ansi",

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,17 +1,61 @@
use anyhow::Result;
use clap::Parser;
use tokio::time::{sleep, Duration};
use dropshot::endpoint;
use dropshot::ApiDescription;
use dropshot::ConfigDropshot;
use dropshot::HttpError;
use dropshot::HttpResponseOk;
use dropshot::RequestContext;
use dropshot::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 of the project.
name: String,
}
/// Fetch version info.
#[endpoint {
method = GET,
path = "/version",
}]
async fn api_version(rqctx: RequestContext<Arc<()>>) -> Result<HttpResponseOk<VersionInfo>, HttpError> {
let ver = VersionInfo {
name: String::from("patagia-controller"),
};
Ok(HttpResponseOk(ver))
}
#[tokio::main]
async fn main() -> Result<()> {
async fn main() -> Result<(), String> {
let _args = Cli::parse();
let fmt_layer = tracing_subscriber::fmt::layer();
let mut config_dropshot = ConfigDropshot::default();
config_dropshot.bind_address = SocketAddr::from_str("0.0.0.0:9474").unwrap();
config_dropshot.request_body_max_bytes = 1024 * 1024;
// Adapt the Dropshot logger to tracing
let dropshot_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!())
};
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::from_default_env())
.with(fmt_layer)
@ -19,6 +63,12 @@ async fn main() -> Result<()> {
tracing::info!("Patagia Controller");
sleep(Duration::from_secs(3)).await;
Ok(())
let mut api = ApiDescription::new();
api.register(api_version).map_err(|e| e.to_string())?;
let server = ServerBuilder::new(api, Arc::new(()), dropshot_logger)
.config(config_dropshot)
.start()
.map_err(|e| e.to_string())?;
server.await
}