aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorfx <[email protected]>2023-10-09 17:26:59 +0200
committerfx <[email protected]>2023-10-09 17:26:59 +0200
commit3e6a72428824c5a50a873a4284b86d0a9e47a778 (patch)
tree7f3594f4068a8009210039bc33e0205a672828f7 /src/main.rs
parent732c487d3dab4af9fc561527591d3d56299e39f2 (diff)
downloadwebol-3e6a72428824c5a50a873a4284b86d0a9e47a778.tar
webol-3e6a72428824c5a50a873a4284b86d0a9e47a778.tar.gz
webol-3e6a72428824c5a50a873a4284b86d0a9e47a778.zip
db int for api
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs35
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 @@
1use std::env;
1use std::sync::Arc; 2use std::sync::Arc;
2use axum::{Router, routing::post}; 3use axum::{Router, routing::post};
3use sqlx::SqlitePool; 4use axum::routing::{get, put};
5use sqlx::PgPool;
6use sqlx::postgres::PgPoolOptions;
4use time::util::local_offset; 7use time::util::local_offset;
5use tracing::{debug, info, level_filters::LevelFilter}; 8use tracing::{debug, info, level_filters::LevelFilter};
6use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; 9use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*};
10use crate::routes::device::{get_device, post_device, put_device};
7use crate::routes::start::start; 11use crate::routes::start::start;
8 12
9mod auth; 13mod auth;
@@ -11,6 +15,7 @@ mod config;
11mod routes; 15mod routes;
12mod wol; 16mod wol;
13mod db; 17mod db;
18mod error;
14 19
15#[tokio::main] 20#[tokio::main]
16async fn main() { 21async 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
56pub struct AppState { 61pub struct AppState {
57 db: SqlitePool 62 db: PgPool
63}
64
65async 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