diff options
author | fx <[email protected]> | 2023-10-09 17:26:59 +0200 |
---|---|---|
committer | fx <[email protected]> | 2023-10-09 17:26:59 +0200 |
commit | 3e6a72428824c5a50a873a4284b86d0a9e47a778 (patch) | |
tree | 7f3594f4068a8009210039bc33e0205a672828f7 /src/main.rs | |
parent | 732c487d3dab4af9fc561527591d3d56299e39f2 (diff) | |
download | webol-3e6a72428824c5a50a873a4284b86d0a9e47a778.tar webol-3e6a72428824c5a50a873a4284b86d0a9e47a778.tar.gz webol-3e6a72428824c5a50a873a4284b86d0a9e47a778.zip |
db int for api
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/src/main.rs b/src/main.rs index 761e925..bb37dc2 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -1,9 +1,13 @@ | |||
1 | use std::env; | ||
1 | use std::sync::Arc; | 2 | use std::sync::Arc; |
2 | use axum::{Router, routing::post}; | 3 | use axum::{Router, routing::post}; |
3 | use sqlx::SqlitePool; | 4 | use axum::routing::{get, put}; |
5 | use sqlx::PgPool; | ||
6 | use sqlx::postgres::PgPoolOptions; | ||
4 | use time::util::local_offset; | 7 | use time::util::local_offset; |
5 | use tracing::{debug, info, level_filters::LevelFilter}; | 8 | use tracing::{debug, info, level_filters::LevelFilter}; |
6 | use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; | 9 | use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; |
10 | use crate::routes::device::{get_device, post_device, put_device}; | ||
7 | use crate::routes::start::start; | 11 | use crate::routes::start::start; |
8 | 12 | ||
9 | mod auth; | 13 | mod auth; |
@@ -11,6 +15,7 @@ mod config; | |||
11 | mod routes; | 15 | mod routes; |
12 | mod wol; | 16 | mod wol; |
13 | mod db; | 17 | mod db; |
18 | mod error; | ||
14 | 19 | ||
15 | #[tokio::main] | 20 | #[tokio::main] |
16 | async fn main() { | 21 | async fn main() { |
@@ -30,20 +35,20 @@ async fn main() { | |||
30 | ) | 35 | ) |
31 | .init(); | 36 | .init(); |
32 | 37 | ||
33 | debug!("connecting to db"); | ||
34 | let db = SqlitePool::connect("sqlite:devices.sqlite").await.unwrap(); | ||
35 | sqlx::migrate!().run(&db).await.unwrap(); | ||
36 | info!("connected to db"); | ||
37 | |||
38 | let version = env!("CARGO_PKG_VERSION"); | 38 | let version = env!("CARGO_PKG_VERSION"); |
39 | 39 | ||
40 | info!("starting webol v{}", version); | 40 | info!("starting webol v{}", version); |
41 | 41 | ||
42 | let db = init_db_pool().await; | ||
43 | |||
42 | let shared_state = Arc::new(AppState { db }); | 44 | let shared_state = Arc::new(AppState { db }); |
43 | 45 | ||
44 | // build our application with a single route | 46 | // build our application with a single route |
45 | let app = Router::new() | 47 | let app = Router::new() |
46 | .route("/start", post(start)) | 48 | .route("/start", post(start)) |
49 | .route("/device", get(get_device)) | ||
50 | .route("/device", put(put_device)) | ||
51 | .route("/device", post(post_device)) | ||
47 | .with_state(shared_state); | 52 | .with_state(shared_state); |
48 | 53 | ||
49 | // run it with hyper on localhost:3000 | 54 | // run it with hyper on localhost:3000 |
@@ -54,5 +59,21 @@ async fn main() { | |||
54 | } | 59 | } |
55 | 60 | ||
56 | pub struct AppState { | 61 | pub struct AppState { |
57 | db: SqlitePool | 62 | db: PgPool |
63 | } | ||
64 | |||
65 | async fn init_db_pool() -> PgPool { | ||
66 | let db_url = env::var("DATABASE_URL").unwrap(); | ||
67 | |||
68 | debug!("attempting to connect dbPool to '{}'", db_url); | ||
69 | |||
70 | let pool = PgPoolOptions::new() | ||
71 | .max_connections(5) | ||
72 | .connect(&db_url) | ||
73 | .await | ||
74 | .unwrap(); | ||
75 | |||
76 | info!("dbPool successfully connected to '{}'", db_url); | ||
77 | |||
78 | pool | ||
58 | } \ No newline at end of file | 79 | } \ No newline at end of file |