aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfx <[email protected]>2023-10-21 20:46:31 +0200
committerfx <[email protected]>2023-10-21 20:46:31 +0200
commitf5928b90748b0bb4c0c498ccc77ebde4eaec8841 (patch)
treee17f41b5722b1e6d36b4ae55332eb89fdf57b4f0 /src
parent48943e0de28b99d7f108e96b7d250bbf1a6c5a5b (diff)
downloadwebol-f5928b90748b0bb4c0c498ccc77ebde4eaec8841.tar
webol-f5928b90748b0bb4c0c498ccc77ebde4eaec8841.tar.gz
webol-f5928b90748b0bb4c0c498ccc77ebde4eaec8841.zip
add init function for db tables
Diffstat (limited to 'src')
-rw-r--r--src/auth.rs3
-rw-r--r--src/db.rs47
-rw-r--r--src/error.rs1
-rw-r--r--src/main.rs25
4 files changed, 52 insertions, 24 deletions
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<bool, AuthError> {
25 } 25 }
26} 26}
27 27
28#[derive(Debug)]
28pub enum AuthError { 29pub enum AuthError {
29 WrongSecret, 30 WrongSecret,
30 MissingSecret, 31 MissingSecret,
@@ -42,4 +43,4 @@ impl AuthError {
42 }, 43 },
43 } 44 }
44 } 45 }
45} \ No newline at end of file 46}
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 @@
1use std::env;
2
1use serde::Serialize; 3use serde::Serialize;
4use sqlx::{PgPool, postgres::PgPoolOptions};
5use tracing::{debug, info};
6
7use crate::error::WebolError;
2 8
3#[derive(Serialize)] 9#[derive(Serialize)]
4pub struct Device { 10pub struct Device {
5 pub id: String, 11 pub id: String,
6 pub mac: String, 12 pub mac: String,
7 pub broadcast_addr: String 13 pub broadcast_addr: String
8} \ No newline at end of file 14}
15
16impl Device {
17 async fn init(db: &PgPool) -> Result<(), WebolError> {
18 sqlx::query!(r#"
19 CREATE TABLE IF NOT EXISTS "devices"
20 (
21 "id" TEXT PRIMARY KEY NOT NULL,
22 "mac" TEXT NOT NULL,
23 "broadcast_addr" TEXT NOT NULL
24 );"#
25 ).execute(db).await.map_err(|err| WebolError::Server(Box::new(err)))?;
26
27 Ok(())
28 }
29}
30
31pub async fn setup_db(db: &PgPool) -> Result<(), WebolError> {
32 Device::init(db).await
33}
34
35pub async fn init_db_pool() -> PgPool {
36 #[cfg(not(debug_assertions))]
37 let db_url = SETTINGS.get_string("database.url").unwrap();
38
39 #[cfg(debug_assertions)]
40 let db_url = env::var("DATABASE_URL").unwrap();
41
42 debug!("attempt to connect dbPool to '{}'", db_url);
43
44 let pool = PgPoolOptions::new()
45 .max_connections(5)
46 .connect(&db_url)
47 .await
48 .unwrap();
49
50 info!("dbPool successfully connected to '{}'", db_url);
51
52 pool
53}
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;
6use tracing::error; 6use tracing::error;
7use crate::auth::AuthError; 7use crate::auth::AuthError;
8 8
9#[derive(Debug)]
9pub enum WebolError { 10pub enum WebolError {
10 Auth(AuthError), 11 Auth(AuthError),
11 Generic, 12 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;
3use axum::{Router, routing::post}; 3use axum::{Router, routing::post};
4use axum::routing::{get, put}; 4use axum::routing::{get, put};
5use sqlx::PgPool; 5use sqlx::PgPool;
6use sqlx::postgres::PgPoolOptions;
7use time::util::local_offset; 6use time::util::local_offset;
8use tracing::{debug, info, level_filters::LevelFilter}; 7use tracing::{info, level_filters::LevelFilter};
9use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; 8use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*};
10use crate::config::SETTINGS; 9use crate::config::SETTINGS;
10use crate::db::{init_db_pool, setup_db};
11use crate::routes::device::{get_device, post_device, put_device}; 11use crate::routes::device::{get_device, post_device, put_device};
12use crate::routes::start::start; 12use crate::routes::start::start;
13 13
@@ -41,6 +41,7 @@ async fn main() {
41 info!("start webol v{}", version); 41 info!("start webol v{}", version);
42 42
43 let db = init_db_pool().await; 43 let db = init_db_pool().await;
44 setup_db(&db).await.unwrap();
44 45
45 let shared_state = Arc::new(AppState { db }); 46 let shared_state = Arc::new(AppState { db });
46 47
@@ -62,23 +63,3 @@ async fn main() {
62pub struct AppState { 63pub struct AppState {
63 db: PgPool 64 db: PgPool
64} 65}
65
66async fn init_db_pool() -> PgPool {
67 #[cfg(not(debug_assertions))]
68 let db_url = SETTINGS.get_string("database.url").unwrap();
69
70 #[cfg(debug_assertions)]
71 let db_url = env::var("DATABASE_URL").unwrap();
72
73 debug!("attempt to connect dbPool to '{}'", db_url);
74
75 let pool = PgPoolOptions::new()
76 .max_connections(5)
77 .connect(&db_url)
78 .await
79 .unwrap();
80
81 info!("dbPool successfully connected to '{}'", db_url);
82
83 pool
84}