From 64fc1a1f318aeb9a11e6018dee4307ce2949c4e3 Mon Sep 17 00:00:00 2001 From: Nils Ponsard Date: Thu, 1 Jun 2023 16:43:45 +0200 Subject: [PATCH] feat: initialize dashboard module Signed-off-by: Nils Ponsard --- Cargo.lock | 9 +++++++++ aster/src/main.rs | 1 + dashboard/Cargo.toml | 7 +++++++ dashboard/src/lib.rs | 48 +++++++++++++++++++++++++++++++++++--------- 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1269413..3275265 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -554,6 +554,15 @@ dependencies = [ [[package]] name = "dashboard" version = "0.1.0" +dependencies = [ + "anyhow", + "async-trait", + "axum", + "common", + "log", + "sqlx", + "tokio", +] [[package]] name = "digest" diff --git a/aster/src/main.rs b/aster/src/main.rs index 0c2fa78..0bff962 100644 --- a/aster/src/main.rs +++ b/aster/src/main.rs @@ -15,6 +15,7 @@ async fn main() -> Result<(), anyhow::Error> { let mut services: Vec> = vec![ Box::::default(), Box::::default(), + Box::::default(), ]; info!("Init services"); diff --git a/dashboard/Cargo.toml b/dashboard/Cargo.toml index bdc704a..05302ce 100644 --- a/dashboard/Cargo.toml +++ b/dashboard/Cargo.toml @@ -6,3 +6,10 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +anyhow = "1.0.71" +async-trait = "0.1.68" +axum = "0.6.18" +log = "0.4.18" +sqlx = { version = "0.6.3", features = ["runtime-tokio-native-tls", "postgres", "chrono" ] } +tokio = { version = "1.12.0", features = ["full"] } +common = { path = "../common" } \ No newline at end of file diff --git a/dashboard/src/lib.rs b/dashboard/src/lib.rs index 7d12d9a..d6702cc 100644 --- a/dashboard/src/lib.rs +++ b/dashboard/src/lib.rs @@ -1,14 +1,44 @@ -pub fn add(left: usize, right: usize) -> usize { - left + right +use anyhow::Context; +use async_trait::async_trait; +use axum::{Router, Server}; +use common::{messaging::crossbeam::CrossbeamMessagingFactory, AsterService}; +use sqlx::{postgres::PgPoolOptions, PgPool}; +const SERVICE_PORT: u16 = 3036; + +#[derive(Debug, Default)] +pub struct DashboardServer { + pool: Option, } -#[cfg(test)] -mod tests { - use super::*; +#[async_trait] +impl AsterService for DashboardServer { + async fn init( + &mut self, + _messaging: &mut CrossbeamMessagingFactory, + ) -> Result<(), anyhow::Error> { + // read the database url from the environment + let database_url = std::env::var("DATABASE_URL").context("DATABASE_URL not set")?; + + // create a connection pool + + self.pool = Some( + PgPoolOptions::new() + .max_connections(5) + .connect(database_url.as_str()) + .await?, + ); + + Ok(()) + } + async fn run(&mut self) -> Result<(), anyhow::Error> { + let router = Router::new(); + + // run the server + + Server::bind(&format!("0.0.0.0:{}", SERVICE_PORT).parse().unwrap()) + .serve(router.into_make_service()) + .await?; - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); + Ok(()) } }