diff --git a/builder/src/api/get_accounts.rs b/builder/src/api/accounts.rs similarity index 100% rename from builder/src/api/get_accounts.rs rename to builder/src/api/accounts.rs diff --git a/builder/src/api/mod.rs b/builder/src/api/mod.rs index 64f71bbb9..4493194b1 100644 --- a/builder/src/api/mod.rs +++ b/builder/src/api/mod.rs @@ -1,10 +1,7 @@ use axum::{routing::get, Router}; -mod get_accounts; +mod accounts; pub fn router() -> Router { - Router::new().route( - "/accounts/:account_type", - get(get_accounts::get_accounts), - ) + Router::new().route("/accounts/:account_type", get(accounts::get_accounts)) } diff --git a/periphery/src/api/accounts.rs b/periphery/src/api/accounts.rs new file mode 100644 index 000000000..6b03eafa8 --- /dev/null +++ b/periphery/src/api/accounts.rs @@ -0,0 +1,30 @@ +use axum::{extract::Path, Extension, Json}; +use serde::Deserialize; +use types::AccountType; + +use crate::PeripherySecretsExtension; + +#[derive(Deserialize, Debug)] +pub struct GetAccountsPath { + account_type: AccountType, +} + +pub async fn get_accounts( + Extension(secrets): PeripherySecretsExtension, + Path(path): Path, +) -> Json> { + match path.account_type { + AccountType::Github => { + let mut accounts: Vec = + secrets.github_accounts.keys().map(|k| k.clone()).collect(); + accounts.sort(); + Json(accounts) + } + AccountType::Docker => { + let mut accounts: Vec = + secrets.docker_accounts.keys().map(|k| k.clone()).collect(); + accounts.sort(); + Json(accounts) + } + } +} diff --git a/periphery/src/api/mod.rs b/periphery/src/api/mod.rs index 54a63b8af..43ab0ab37 100644 --- a/periphery/src/api/mod.rs +++ b/periphery/src/api/mod.rs @@ -1,5 +1,6 @@ -use axum::Router; +use axum::{routing::get, Router}; +mod accounts; mod container; mod stats; @@ -7,4 +8,5 @@ pub fn router() -> Router { Router::new() .nest("/container", container::router()) .nest("/stats", stats::router()) + .route("/accounts/:account_type", get(accounts::get_accounts)) } diff --git a/periphery/src/config.rs b/periphery/src/config.rs index a5a31c1b4..43d97333d 100644 --- a/periphery/src/config.rs +++ b/periphery/src/config.rs @@ -1,10 +1,13 @@ -use std::{fs::File, io::Read}; +use std::{fs::File, io::Read, sync::Arc}; +use axum::Extension; use dotenv::dotenv; use helpers::parse_config_file; use serde::Deserialize; use types::PeripherySecrets; +use crate::PeripherySecretsExtension; + #[derive(Deserialize)] struct Env { #[serde(default = "default_port")] @@ -13,11 +16,11 @@ struct Env { secrets_path: String, } -pub fn load() -> (u16, PeripherySecrets) { +pub fn load() -> (u16, PeripherySecretsExtension) { dotenv().ok(); let env: Env = envy::from_env().expect("failed to parse env"); let secrets = parse_config_file(&env.secrets_path).expect("failed to parse secrets file"); - (env.port, secrets) + (env.port, Extension(Arc::new(secrets))) } fn default_port() -> u16 { diff --git a/periphery/src/main.rs b/periphery/src/main.rs index e16062475..3525761f6 100644 --- a/periphery/src/main.rs +++ b/periphery/src/main.rs @@ -1,7 +1,10 @@ #![allow(unused)] +use std::sync::Arc; + use ::helpers::get_socket_addr; use axum::{extract::Path, http::StatusCode, routing::get, Extension, Json, Router}; +use types::PeripherySecrets; mod api; mod config; @@ -9,11 +12,13 @@ mod helpers; use crate::api::*; +type PeripherySecretsExtension = Extension>; + #[tokio::main] async fn main() { let (port, secrets) = config::load(); - let app = api::router(); + let app = api::router().layer(secrets); println!("starting montior periphery on port {port}");