add accounts route in periphery

This commit is contained in:
beckerinj
2022-11-11 17:40:57 -05:00
parent aaa8d7e97d
commit b495593b59
6 changed files with 47 additions and 10 deletions

View File

@@ -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))
}

View File

@@ -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<GetAccountsPath>,
) -> Json<Vec<String>> {
match path.account_type {
AccountType::Github => {
let mut accounts: Vec<String> =
secrets.github_accounts.keys().map(|k| k.clone()).collect();
accounts.sort();
Json(accounts)
}
AccountType::Docker => {
let mut accounts: Vec<String> =
secrets.docker_accounts.keys().map(|k| k.clone()).collect();
accounts.sort();
Json(accounts)
}
}
}

View File

@@ -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))
}

View File

@@ -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 {

View File

@@ -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<Arc<PeripherySecrets>>;
#[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}");