diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/db.rs | 21 | ||||
-rw-r--r-- | src/main.rs | 4 |
2 files changed, 2 insertions, 23 deletions
@@ -4,8 +4,6 @@ use serde::Serialize; | |||
4 | use sqlx::{PgPool, postgres::PgPoolOptions}; | 4 | use sqlx::{PgPool, postgres::PgPoolOptions}; |
5 | use tracing::{debug, info}; | 5 | use tracing::{debug, info}; |
6 | 6 | ||
7 | use crate::error::WebolError; | ||
8 | |||
9 | #[derive(Serialize)] | 7 | #[derive(Serialize)] |
10 | pub struct Device { | 8 | pub struct Device { |
11 | pub id: String, | 9 | pub id: String, |
@@ -13,25 +11,6 @@ pub struct Device { | |||
13 | pub broadcast_addr: String | 11 | pub broadcast_addr: String |
14 | } | 12 | } |
15 | 13 | ||
16 | impl 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 | |||
31 | pub async fn setup_db(db: &PgPool) -> Result<(), WebolError> { | ||
32 | Device::init(db).await | ||
33 | } | ||
34 | |||
35 | pub async fn init_db_pool() -> PgPool { | 14 | pub async fn init_db_pool() -> PgPool { |
36 | #[cfg(not(debug_assertions))] | 15 | #[cfg(not(debug_assertions))] |
37 | let db_url = SETTINGS.get_string("database.url").unwrap(); | 16 | let db_url = SETTINGS.get_string("database.url").unwrap(); |
diff --git a/src/main.rs b/src/main.rs index 8b4e9eb..ce12cf6 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -7,7 +7,7 @@ use time::util::local_offset; | |||
7 | use tracing::{info, level_filters::LevelFilter}; | 7 | use tracing::{info, level_filters::LevelFilter}; |
8 | use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; | 8 | use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; |
9 | use crate::config::SETTINGS; | 9 | use crate::config::SETTINGS; |
10 | use crate::db::{init_db_pool, setup_db}; | 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,7 +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 | sqlx::migrate!().run(&db).await.unwrap(); |
45 | 45 | ||
46 | let shared_state = Arc::new(AppState { db }); | 46 | let shared_state = Arc::new(AppState { db }); |
47 | 47 | ||