Now rading database from env var
All checks were successful
Build Project / test (push) Successful in 5m39s
All checks were successful
Build Project / test (push) Successful in 5m39s
This commit is contained in:
@@ -18,10 +18,10 @@ pub enum DatabaseError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Database {
|
impl Database {
|
||||||
pub async fn init(host: &str, user: &str, pass: &str, db_name: &str) -> Database {
|
pub async fn init(database_url: &str) -> Database {
|
||||||
match PgPoolOptions::new()
|
match PgPoolOptions::new()
|
||||||
.max_connections(10)
|
.max_connections(10)
|
||||||
.connect(&format!("postgres://{user}:{pass}@{host}/{db_name}"))
|
.connect(database_url)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(pool) => {
|
Ok(pool) => {
|
||||||
|
|||||||
15
src/main.rs
15
src/main.rs
@@ -1,3 +1,5 @@
|
|||||||
|
use std::{env, process};
|
||||||
|
|
||||||
use crate::schemas::{TelemetryMessageFromDevice, ValueMessageFromDevice};
|
use crate::schemas::{TelemetryMessageFromDevice, ValueMessageFromDevice};
|
||||||
use actix_web::{get, http::StatusCode, post, web, App, HttpResponse, HttpServer, Responder};
|
use actix_web::{get, http::StatusCode, post, web, App, HttpResponse, HttpServer, Responder};
|
||||||
use database::Database;
|
use database::Database;
|
||||||
@@ -102,8 +104,17 @@ async fn main() -> std::io::Result<()> {
|
|||||||
env_logger::init();
|
env_logger::init();
|
||||||
info!("Starting");
|
info!("Starting");
|
||||||
|
|
||||||
info!("Connecting to Database");
|
|
||||||
let db = Database::init("db", "dev", "dev", "iot").await;
|
let db_url = match env::var("DATABASE_URL") {
|
||||||
|
Ok(url) => url,
|
||||||
|
Err(e) => {
|
||||||
|
error!("Failed reading DATABASE_URL");
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
info!("Connecting to Database {}", db_url);
|
||||||
|
|
||||||
|
let db = Database::init(&db_url).await;
|
||||||
db.init_db().await;
|
db.init_db().await;
|
||||||
|
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
|
|||||||
Reference in New Issue
Block a user