summaryrefslogtreecommitdiff
path: root/src/handlers/colonies.rs
diff options
context:
space:
mode:
authorCara Salter <cara@devcara.com>2022-07-10 07:43:40 -0400
committerCara Salter <cara@devcara.com>2022-07-10 07:43:40 -0400
commit131dd5ab9df295b0bad320675b2307afcbf7d076 (patch)
treea971b3506198935d98e87282deaa2297c4241d84 /src/handlers/colonies.rs
parent90823a79ef80f6a589a414fb8a3006c8ff17a95d (diff)
downloadhomeworld-131dd5ab9df295b0bad320675b2307afcbf7d076.tar.gz
homeworld-131dd5ab9df295b0bad320675b2307afcbf7d076.zip
colonies: List users
First work towards cloud-init data being stored in homeworld
Diffstat (limited to 'src/handlers/colonies.rs')
-rw-r--r--src/handlers/colonies.rs46
1 files changed, 46 insertions, 0 deletions
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<String>,
+ pub ssh_authorized_keys: Vec<String>,
+}
+
+impl From<DbUser> 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<Arc<State>>) -> JsonResult<Json<UserData>> {
+ 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::<Vec<User>>();
+
+ let data = UserData { users };
+
+ Ok(Json(data))
+}