diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/auth.rs | 3 | ||||
-rw-r--r-- | src/db.rs | 26 | ||||
-rw-r--r-- | src/error.rs | 1 | ||||
-rw-r--r-- | src/main.rs | 25 |
4 files changed, 31 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)] | ||
28 | pub enum AuthError { | 29 | pub 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 | } |
@@ -1,8 +1,32 @@ | |||
1 | use std::env; | ||
2 | |||
1 | use serde::Serialize; | 3 | use serde::Serialize; |
4 | use sqlx::{PgPool, postgres::PgPoolOptions}; | ||
5 | use tracing::{debug, info}; | ||
2 | 6 | ||
3 | #[derive(Serialize)] | 7 | #[derive(Serialize)] |
4 | pub struct Device { | 8 | pub struct Device { |
5 | pub id: String, | 9 | pub id: String, |
6 | pub mac: String, | 10 | pub mac: String, |
7 | pub broadcast_addr: String | 11 | pub broadcast_addr: String |
8 | } \ No newline at end of file | 12 | } |
13 | |||
14 | pub async fn init_db_pool() -> PgPool { | ||
15 | #[cfg(not(debug_assertions))] | ||
16 | let db_url = SETTINGS.get_string("database.url").unwrap(); | ||
17 | |||
18 | #[cfg(debug_assertions)] | ||
19 | let db_url = env::var("DATABASE_URL").unwrap(); | ||
20 | |||
21 | debug!("attempt to connect dbPool to '{}'", db_url); | ||
22 | |||
23 | let pool = PgPoolOptions::new() | ||
24 | .max_connections(5) | ||
25 | .connect(&db_url) | ||
26 | .await | ||
27 | .unwrap(); | ||
28 | |||
29 | info!("dbPool successfully connected to '{}'", db_url); | ||
30 | |||
31 | pool | ||
32 | } | ||
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; | |||
6 | use tracing::error; | 6 | use tracing::error; |
7 | use crate::auth::AuthError; | 7 | use crate::auth::AuthError; |
8 | 8 | ||
9 | #[derive(Debug)] | ||
9 | pub enum WebolError { | 10 | pub enum WebolError { |
10 | Auth(AuthError), | 11 | Auth(AuthError), |
11 | Generic, | 12 | Generic, |
diff --git a/src/main.rs b/src/main.rs index b7306ea..ce12cf6 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -3,11 +3,11 @@ use std::sync::Arc; | |||
3 | use axum::{Router, routing::post}; | 3 | use axum::{Router, routing::post}; |
4 | use axum::routing::{get, put}; | 4 | use axum::routing::{get, put}; |
5 | use sqlx::PgPool; | 5 | use sqlx::PgPool; |
6 | use sqlx::postgres::PgPoolOptions; | ||
7 | use time::util::local_offset; | 6 | use time::util::local_offset; |
8 | use tracing::{debug, info, level_filters::LevelFilter}; | 7 | use tracing::{info, level_filters::LevelFilter}; |
9 | use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; | 8 | use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; |
10 | use crate::config::SETTINGS; | 9 | use crate::config::SETTINGS; |
10 | use crate::db::init_db_pool; | ||
11 | use crate::routes::device::{get_device, post_device, put_device}; | 11 | use crate::routes::device::{get_device, post_device, put_device}; |
12 | use crate::routes::start::start; | 12 | use 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 | sqlx::migrate!().run(&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() { | |||
62 | pub struct AppState { | 63 | pub struct AppState { |
63 | db: PgPool | 64 | db: PgPool |
64 | } | 65 | } |
65 | |||
66 | async 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 | } | ||