#1 Initial dropshot server
All checks were successful
ci/woodpecker/push/ci Pipeline was successful

Reviewed-on: Patagia/patagia-run#1
This commit is contained in:
Daniel Lundin 2024-11-26 22:45:55 +01:00
commit a832a28faf
5 changed files with 1316 additions and 19 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,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))
}

View file

@ -1,18 +1,16 @@
set shell := ["/usr/bin/env", "bash", "-euo", "pipefail", "-c"]
export OTEL_SERVICE_NAME := "my-program"
[private]
default:
@just --choose
# Run
run $RUST_LOG="debug":
nix run
# Run controller
run-controller $RUST_LOG="debug":
cargo run --package patagia-controller
# Run local development
dev:
watchexec --clear --restart --stop-signal INT --debounce 300ms -- just run
# Run controller local development
dev-controller:
watchexec --clear --restart --stop-signal INT --debounce 300ms -- just run-controller
# Run all tests
check: