generated from Patagia/template-nix
Compare commits
2 commits
6e38eaddad
...
d3460ecbab
Author | SHA1 | Date | |
---|---|---|---|
d3460ecbab | |||
3b04b82998 |
17 changed files with 1011 additions and 38 deletions
8
.cargo/audit.toml
Normal file
8
.cargo/audit.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[advisories]
|
||||
ignore = [
|
||||
# Advisory about a vulnerability in rsa, which we don't use, but comes via sqlx due
|
||||
# to a bug in cargo. For context, see:
|
||||
# https://github.com/launchbadge/sqlx/issues/2911
|
||||
# and https://github.com/rust-lang/cargo/issues/10801
|
||||
"RUSTSEC-2023-0071"
|
||||
]
|
686
Cargo.lock
generated
686
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -14,6 +14,7 @@ schemars.workspace = true
|
|||
serde.workspace = true
|
||||
tokio.workspace = true
|
||||
tracing.workspace = true
|
||||
uuid.workspace = true
|
||||
|
||||
[package.metadata.cargo-machete]
|
||||
ignored = ["reqwest", "serde"]
|
||||
|
|
62
api.json
62
api.json
|
@ -5,6 +5,44 @@
|
|||
"version": "1.0.0"
|
||||
},
|
||||
"paths": {
|
||||
"/users/{userId}": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"user"
|
||||
],
|
||||
"summary": "Fetch user info.",
|
||||
"operationId": "get_user_by_id",
|
||||
"parameters": [
|
||||
{
|
||||
"in": "path",
|
||||
"name": "userId",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"format": "uuid"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "successful operation",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/User"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"4XX": {
|
||||
"$ref": "#/components/responses/Error"
|
||||
},
|
||||
"5XX": {
|
||||
"$ref": "#/components/responses/Error"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/version": {
|
||||
"get": {
|
||||
"summary": "Fetch version info.",
|
||||
|
@ -51,6 +89,23 @@
|
|||
"request_id"
|
||||
]
|
||||
},
|
||||
"User": {
|
||||
"description": "User",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "string",
|
||||
"format": "uuid"
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"id",
|
||||
"name"
|
||||
]
|
||||
},
|
||||
"VersionInfo": {
|
||||
"description": "Version and build information",
|
||||
"type": "object",
|
||||
|
@ -80,5 +135,10 @@
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"tags": [
|
||||
{
|
||||
"name": "user"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "SELECT * FROM users WHERE id = $1",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "id",
|
||||
"type_info": "Uuid"
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "name",
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"name": "time_deleted",
|
||||
"type_info": "Timestamptz"
|
||||
},
|
||||
{
|
||||
"ordinal": 3,
|
||||
"name": "time_created",
|
||||
"type_info": "Timestamptz"
|
||||
},
|
||||
{
|
||||
"ordinal": 4,
|
||||
"name": "time_modified",
|
||||
"type_info": "Timestamptz"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "843923b9a0257cf80f1dff554e7dc8fdfc05f489328e8376513124dfb42996e3"
|
||||
}
|
|
@ -15,10 +15,14 @@ schemars.workspace = true
|
|||
serde.workspace = true
|
||||
slog-async.workspace = true
|
||||
slog.workspace = true
|
||||
sqlx = { version = "0.8.3", default-features = false, features = [
|
||||
"macros", "migrate", "postgres", "runtime-tokio", "tls-rustls", "time", "uuid"
|
||||
] }
|
||||
tokio.workspace = true
|
||||
trace-request = { path = "../trace-request" }
|
||||
tracing-slog.workspace = true
|
||||
tracing.workspace = true
|
||||
uuid.workspace = true
|
||||
|
||||
[package.metadata.cargo-machete]
|
||||
ignored = ["http"]
|
||||
|
|
5
controller/build.rs
Normal file
5
controller/build.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
// generated by `sqlx migrate build-script`
|
||||
fn main() {
|
||||
// trigger recompilation when a new migration is added
|
||||
println!("cargo:rerun-if-changed=migrations");
|
||||
}
|
7
controller/migrations/20250108132540_users.sql
Normal file
7
controller/migrations/20250108132540_users.sql
Normal file
|
@ -0,0 +1,7 @@
|
|||
CREATE TABLE IF NOT EXISTS patagia.public.Users(
|
||||
id UUID PRIMARY KEY,
|
||||
name VARCHAR(63) NOT NULL,
|
||||
time_deleted TIMESTAMP WITH TIME ZONE, -- non-NULL if deleted
|
||||
time_created TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
time_modified TIMESTAMP WITH TIME ZONE NOT NULL
|
||||
);
|
|
@ -4,12 +4,14 @@ use dropshot::ApiDescription;
|
|||
use std::sync::Arc;
|
||||
|
||||
use crate::context::ControllerContext;
|
||||
use crate::user;
|
||||
use crate::version;
|
||||
|
||||
type ControllerApiDescription = ApiDescription<Arc<ControllerContext>>;
|
||||
|
||||
pub fn api() -> Result<ControllerApiDescription> {
|
||||
let mut api = ControllerApiDescription::new();
|
||||
user::register_api(&mut api)?;
|
||||
api.register(version::version)?;
|
||||
Ok(api)
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use anyhow::{anyhow, Result};
|
||||
use clap::Parser;
|
||||
use dropshot::{ConfigDropshot, ServerBuilder};
|
||||
|
||||
use slog::Drain;
|
||||
use sqlx::postgres::PgPool;
|
||||
use tracing_slog::TracingSlogDrain;
|
||||
|
||||
use std::net::SocketAddr;
|
||||
|
@ -36,6 +36,13 @@ struct Cli {
|
|||
env = "LISTEN_ADDRESS"
|
||||
)]
|
||||
listen_address: String,
|
||||
|
||||
#[arg(
|
||||
long = "database-url",
|
||||
default_value = "postgresql://localhost/patagia",
|
||||
env = "DATABASE_URL"
|
||||
)]
|
||||
database_url: Option<String>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
|
@ -57,7 +64,19 @@ async fn main() -> Result<()> {
|
|||
slog::Logger::root(async_drain, slog::o!())
|
||||
};
|
||||
|
||||
let ctx = ControllerContext::new();
|
||||
let database_url = args.database_url.unwrap();
|
||||
|
||||
tracing::info!(
|
||||
database_url,
|
||||
listen_address = args.listen_address,
|
||||
"Starting server"
|
||||
);
|
||||
|
||||
let pg = PgPool::connect(&database_url).await?;
|
||||
|
||||
sqlx::migrate!().run(&pg).await?;
|
||||
|
||||
let ctx = ControllerContext::new(pg);
|
||||
let api = api::api()?;
|
||||
ServerBuilder::new(api, Arc::new(ctx), logger)
|
||||
.config(config)
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
pub struct ControllerContext {}
|
||||
use sqlx::postgres::PgPool;
|
||||
|
||||
pub struct ControllerContext {
|
||||
pub pg_pool: PgPool,
|
||||
}
|
||||
|
||||
impl ControllerContext {
|
||||
pub fn new() -> ControllerContext {
|
||||
ControllerContext {}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ControllerContext {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
pub fn new(pg_pool: PgPool) -> ControllerContext {
|
||||
ControllerContext { pg_pool }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
pub mod api;
|
||||
pub mod context;
|
||||
|
||||
mod user;
|
||||
mod version;
|
||||
|
|
126
controller/src/user/api.rs
Normal file
126
controller/src/user/api.rs
Normal file
|
@ -0,0 +1,126 @@
|
|||
use dropshot::{
|
||||
endpoint, EmptyScanParams, HttpError, HttpResponseOk, PaginationParams, Path, Query,
|
||||
RequestContext, ResultsPage, WhichPage,
|
||||
};
|
||||
use dropshot::{ApiDescription, ApiDescriptionRegisterError};
|
||||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use trace_request::trace_request;
|
||||
use uuid::Uuid;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::User;
|
||||
use crate::context::ControllerContext;
|
||||
|
||||
#[derive(Deserialize, JsonSchema, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct UsersPathParams {
|
||||
user_id: Uuid,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, JsonSchema, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct UserPage {
|
||||
user_id: Uuid,
|
||||
}
|
||||
|
||||
pub fn register_api(
|
||||
api: &mut ApiDescription<Arc<ControllerContext>>,
|
||||
) -> Result<(), ApiDescriptionRegisterError> {
|
||||
api.register(get_user_by_id)?;
|
||||
api.register(list_users)
|
||||
}
|
||||
|
||||
/// Fetch user info.
|
||||
#[endpoint {
|
||||
method = GET,
|
||||
path = "/users/{userId}",
|
||||
tags = [ "user" ],
|
||||
}]
|
||||
#[trace_request]
|
||||
async fn get_user_by_id(
|
||||
rqctx: RequestContext<Arc<ControllerContext>>,
|
||||
params: Path<UsersPathParams>,
|
||||
) -> Result<HttpResponseOk<User>, HttpError> {
|
||||
let id = params.into_inner().user_id;
|
||||
tracing::debug!(id = id.to_string(), "Getting user by id");
|
||||
|
||||
let pg = rqctx.context().pg_pool.to_owned();
|
||||
|
||||
let rec = sqlx::query!(r#"SELECT * FROM users WHERE id = $1"#, id)
|
||||
.fetch_one(&pg)
|
||||
.await
|
||||
.map_err(|e| match e {
|
||||
sqlx::Error::RowNotFound => {
|
||||
HttpError::for_not_found(None, format!("User not found by id: {:?}", id))
|
||||
}
|
||||
err => HttpError::for_internal_error(format!("Error: {}", err)),
|
||||
})?;
|
||||
|
||||
let user = User {
|
||||
id: rec.id,
|
||||
name: rec.name,
|
||||
};
|
||||
|
||||
Ok(HttpResponseOk(user))
|
||||
}
|
||||
|
||||
/// List users
|
||||
#[endpoint {
|
||||
method = GET,
|
||||
path = "/users",
|
||||
tags = [ "user" ],
|
||||
}]
|
||||
#[trace_request]
|
||||
async fn list_users(
|
||||
rqctx: RequestContext<Arc<ControllerContext>>,
|
||||
query: Query<PaginationParams<EmptyScanParams, UserPage>>,
|
||||
) -> Result<HttpResponseOk<ResultsPage<User>>, HttpError> {
|
||||
let pag_params = query.into_inner();
|
||||
let limit = rqctx.page_limit(&pag_params)?.get() as usize;
|
||||
let pg = rqctx.context().pg_pool.to_owned();
|
||||
|
||||
let users = match &pag_params.page {
|
||||
WhichPage::First(..) => {
|
||||
tracing::debug!("Listing users. First page");
|
||||
sqlx::query!(r#"SELECT * FROM users ORDER BY id LIMIT $1"#, limit as i64)
|
||||
.fetch_all(&pg)
|
||||
.await
|
||||
.map_err(|e| HttpError::for_internal_error(format!("Error: {}", e)))?
|
||||
.into_iter()
|
||||
.map(|rec| User {
|
||||
id: rec.id,
|
||||
name: rec.name,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
WhichPage::Next(UserPage { user_id: last_seen }) => {
|
||||
tracing::debug!(
|
||||
last_seen = last_seen.to_string(),
|
||||
"Listing users. Next page"
|
||||
);
|
||||
sqlx::query!(
|
||||
r#"SELECT * FROM users WHERE id > $1 ORDER BY id LIMIT $2"#,
|
||||
last_seen,
|
||||
limit as i64
|
||||
)
|
||||
.fetch_all(&pg)
|
||||
.await
|
||||
.map_err(|e| HttpError::for_internal_error(format!("Error: {}", e)))?
|
||||
.into_iter()
|
||||
.map(|rec| User {
|
||||
id: rec.id,
|
||||
name: rec.name,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
};
|
||||
|
||||
Ok(HttpResponseOk(ResultsPage::new(
|
||||
users,
|
||||
&EmptyScanParams {},
|
||||
|u: &User, _| UserPage { user_id: u.id },
|
||||
)?))
|
||||
}
|
14
controller/src/user/mod.rs
Normal file
14
controller/src/user/mod.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
use schemars::JsonSchema;
|
||||
use serde::Serialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
mod api;
|
||||
|
||||
pub use self::api::register_api;
|
||||
|
||||
/// User
|
||||
#[derive(Serialize, JsonSchema)]
|
||||
struct User {
|
||||
id: Uuid,
|
||||
name: String,
|
||||
}
|
|
@ -49,6 +49,7 @@
|
|||
root = ./.;
|
||||
fileset = pkgs.lib.fileset.unions [
|
||||
./api.json
|
||||
./controller/.sqlx
|
||||
(craneLib.fileset.commonCargoSources ./.)
|
||||
];
|
||||
};
|
||||
|
@ -116,6 +117,7 @@
|
|||
formatter =
|
||||
(treefmt-nix.lib.evalModule pkgs {
|
||||
projectRootFile = "flake.nix";
|
||||
|
||||
programs = {
|
||||
nixfmt.enable = true;
|
||||
nixfmt.package = pkgs.nixfmt-rfc-style;
|
||||
|
@ -141,6 +143,8 @@
|
|||
just
|
||||
nixfmt-rfc-style
|
||||
rust-dev-toolchain
|
||||
sqls
|
||||
sqlx-cli
|
||||
watchexec
|
||||
]
|
||||
++ commonArgs.buildInputs;
|
||||
|
|
|
@ -96,20 +96,18 @@ pub fn init_tracing(otel_endpoint: Option<&String>, log_stderr: bool) -> Result<
|
|||
None => None,
|
||||
};
|
||||
|
||||
let metrics_layer = match meter_provider {
|
||||
Some(ref p) => Some(MetricsLayer::new(p.to_owned())),
|
||||
None => None,
|
||||
};
|
||||
let metrics_layer = meter_provider
|
||||
.as_ref()
|
||||
.map(|p| MetricsLayer::new(p.to_owned()));
|
||||
|
||||
let tracer_provider = match otel_endpoint {
|
||||
Some(endpoint) => Some(init_tracer_provider(endpoint, resource)?),
|
||||
None => None,
|
||||
};
|
||||
|
||||
let trace_layer = match tracer_provider {
|
||||
Some(ref p) => Some(OpenTelemetryLayer::new(p.tracer("tracing-otel-subscriber"))),
|
||||
None => None,
|
||||
};
|
||||
let trace_layer = tracer_provider
|
||||
.as_ref()
|
||||
.map(|p| OpenTelemetryLayer::new(p.tracer("tracing-otel-subscriber")));
|
||||
|
||||
opentelemetry::global::set_text_map_propagator(TraceContextPropagator::new());
|
||||
|
||||
|
|
30
justfile
30
justfile
|
@ -5,11 +5,13 @@ default:
|
|||
@just --choose
|
||||
|
||||
# Run controller
|
||||
[group('controller')]
|
||||
run-controller $RUST_LOG="debug,h2=info,hyper_util=info,tower=info":
|
||||
cargo run --package patagia-controller -- --log-stderr
|
||||
|
||||
# Run controller local development
|
||||
dev-controller:
|
||||
[group('controller')]
|
||||
dev-controller: dev-controller-db-migrate
|
||||
watchexec --clear --restart --stop-signal INT --debounce 300ms -- just run-controller
|
||||
|
||||
# Run agent
|
||||
|
@ -48,6 +50,10 @@ machete:
|
|||
open-api:
|
||||
cargo xtask open-api
|
||||
|
||||
# Update OpenAPI spec
|
||||
gen-open-api:
|
||||
cargo xtask open-api > api.json
|
||||
|
||||
# Run all tests
|
||||
check: check-nix
|
||||
|
||||
|
@ -56,6 +62,7 @@ check-nix:
|
|||
nix flake check
|
||||
|
||||
# Run PostgreSQL for development and testing
|
||||
[group('controller')]
|
||||
dev-postgres:
|
||||
mkdir -p "${XDG_RUNTIME_DIR}/patagia-postgres"
|
||||
podman volume exists patagia-postgres || podman volume create patagia-postgres
|
||||
|
@ -69,12 +76,33 @@ dev-postgres:
|
|||
--volume patagia-postgres:/var/lib/postgresql/data \
|
||||
--volume "${XDG_RUNTIME_DIR}/patagia-postgres:/var/run/postgresql" \
|
||||
docker.io/postgres:17
|
||||
sleep 0.3
|
||||
|
||||
# Clean up PostgreSQL data
|
||||
[group('controller')]
|
||||
dev-postgres-clean:
|
||||
podman rm -f patagia-postgres || true
|
||||
podman volume rm patagia-postgres || true
|
||||
|
||||
# Connect to PostgreSQL with psql
|
||||
[group('controller')]
|
||||
dev-postgres-psql:
|
||||
podman exec -it patagia-postgres psql -U patagia
|
||||
|
||||
[group('controller')]
|
||||
[working-directory: 'controller']
|
||||
dev-controller-db-migrate: dev-postgres
|
||||
cargo sqlx migrate run
|
||||
|
||||
[group('controller')]
|
||||
[working-directory: 'controller']
|
||||
dev-controller-db-reset:
|
||||
cargo sqlx db reset -y
|
||||
|
||||
[group('controller')]
|
||||
[working-directory: 'controller']
|
||||
gen-controller-sqlx-prepare:
|
||||
cargo sqlx prepare
|
||||
|
||||
gen: gen-open-api gen-controller-sqlx-prepare fmt
|
||||
|
||||
|
|
Loading…
Reference in a new issue