From f5928b90748b0bb4c0c498ccc77ebde4eaec8841 Mon Sep 17 00:00:00 2001 From: fx Date: Sat, 21 Oct 2023 20:46:31 +0200 Subject: add init function for db tables --- src/auth.rs | 3 ++- src/db.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++++- src/error.rs | 1 + src/main.rs | 25 +++---------------------- 4 files changed, 52 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/auth.rs b/src/auth.rs index 81e798f..0fffa60 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -25,6 +25,7 @@ pub fn auth(secret: Option<&HeaderValue>) -> Result { } } +#[derive(Debug)] pub enum AuthError { WrongSecret, MissingSecret, @@ -42,4 +43,4 @@ impl AuthError { }, } } -} \ No newline at end of file +} diff --git a/src/db.rs b/src/db.rs index 87943ca..e9d001f 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,8 +1,53 @@ +use std::env; + use serde::Serialize; +use sqlx::{PgPool, postgres::PgPoolOptions}; +use tracing::{debug, info}; + +use crate::error::WebolError; #[derive(Serialize)] pub struct Device { pub id: String, pub mac: String, pub broadcast_addr: String -} \ No newline at end of file +} + +impl Device { + async fn init(db: &PgPool) -> Result<(), WebolError> { + sqlx::query!(r#" + CREATE TABLE IF NOT EXISTS "devices" + ( + "id" TEXT PRIMARY KEY NOT NULL, + "mac" TEXT NOT NULL, + "broadcast_addr" TEXT NOT NULL + );"# + ).execute(db).await.map_err(|err| WebolError::Server(Box::new(err)))?; + + Ok(()) + } +} + +pub async fn setup_db(db: &PgPool) -> Result<(), WebolError> { + Device::init(db).await +} + +pub async fn init_db_pool() -> PgPool { + #[cfg(not(debug_assertions))] + let db_url = SETTINGS.get_string("database.url").unwrap(); + + #[cfg(debug_assertions)] + let db_url = env::var("DATABASE_URL").unwrap(); + + debug!("attempt to connect dbPool to '{}'", db_url); + + let pool = PgPoolOptions::new() + .max_connections(5) + .connect(&db_url) + .await + .unwrap(); + + info!("dbPool successfully connected to '{}'", db_url); + + pool +} diff --git a/src/error.rs b/src/error.rs index afed111..db2fc86 100644 --- a/src/error.rs +++ b/src/error.rs @@ -6,6 +6,7 @@ use serde_json::json; use tracing::error; use crate::auth::AuthError; +#[derive(Debug)] pub enum WebolError { Auth(AuthError), Generic, diff --git a/src/main.rs b/src/main.rs index b7306ea..8b4e9eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,11 +3,11 @@ use std::sync::Arc; use axum::{Router, routing::post}; use axum::routing::{get, put}; use sqlx::PgPool; -use sqlx::postgres::PgPoolOptions; use time::util::local_offset; -use tracing::{debug, info, level_filters::LevelFilter}; +use tracing::{info, level_filters::LevelFilter}; use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; use crate::config::SETTINGS; +use crate::db::{init_db_pool, setup_db}; use crate::routes::device::{get_device, post_device, put_device}; use crate::routes::start::start; @@ -41,6 +41,7 @@ async fn main() { info!("start webol v{}", version); let db = init_db_pool().await; + setup_db(&db).await.unwrap(); let shared_state = Arc::new(AppState { db }); @@ -62,23 +63,3 @@ async fn main() { pub struct AppState { db: PgPool } - -async fn init_db_pool() -> PgPool { - #[cfg(not(debug_assertions))] - let db_url = SETTINGS.get_string("database.url").unwrap(); - - #[cfg(debug_assertions)] - let db_url = env::var("DATABASE_URL").unwrap(); - - debug!("attempt to connect dbPool to '{}'", db_url); - - let pool = PgPoolOptions::new() - .max_connections(5) - .connect(&db_url) - .await - .unwrap(); - - info!("dbPool successfully connected to '{}'", db_url); - - pool -} -- cgit v1.2.3