From 131dd5ab9df295b0bad320675b2307afcbf7d076 Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Sun, 10 Jul 2022 07:43:40 -0400 Subject: colonies: List users First work towards cloud-init data being stored in homeworld --- src/handlers/colonies.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/handlers/mod.rs | 1 + src/main.rs | 1 + 3 files changed, 48 insertions(+) create mode 100644 src/handlers/colonies.rs (limited to 'src') diff --git a/src/handlers/colonies.rs b/src/handlers/colonies.rs new file mode 100644 index 0000000..37ec65a --- /dev/null +++ b/src/handlers/colonies.rs @@ -0,0 +1,46 @@ +use std::sync::Arc; + +use axum::{Extension, Json}; +use sqlx::query_as; + +use crate::{ + errors::{JsonResult, ServiceError, StringResult}, + State, +}; + +use solarlib::colony::{User, UserData}; + +#[derive(Debug, Clone)] +pub struct DbUser { + pub id: i32, + pub name: String, + pub groups: Vec, + pub ssh_authorized_keys: Vec, +} + +impl From for User { + fn from(o: DbUser) -> Self { + Self { + name: o.name, + groups: o.groups, + ssh_authorized_keys: o.ssh_authorized_keys, + } + } +} + +pub async fn list_users(state: Extension>) -> JsonResult> { + let mut conn = state.conn.acquire().await?; + + let db_users = query_as!(DbUser, "SELECT * FROM seed_users") + .fetch_all(&mut conn) + .await?; + + let users = db_users + .into_iter() + .map(|u| u.into()) + .collect::>(); + + let data = UserData { users }; + + Ok(Json(data)) +} diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 2b5bfaf..a643df3 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -1 +1,2 @@ +pub mod colonies; pub mod ships; diff --git a/src/main.rs b/src/main.rs index 4a02f95..67b54ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -54,6 +54,7 @@ async fn main() { .route("/ships/new", post(handlers::ships::new)) .route("/ships/delete/:shasum", delete(handlers::ships::delete)) .route("/ships/get/:shasum", get(handlers::ships::get)) + .route("/users/list", get(handlers::colonies::list_users)) .layer( ServiceBuilder::new() .layer(HandleErrorLayer::new(|error: BoxError| async move { -- cgit v1.2.3