Browse Source

Add env file stuff

Lee Morgan 2 months ago
parent
commit
32f3983947
4 changed files with 13 additions and 1 deletions
  1. 1 0
      .env.example
  2. 1 0
      Cargo.lock
  3. 1 0
      Cargo.toml
  4. 10 1
      src/main.rs

+ 1 - 0
.env.example

@@ -0,0 +1 @@
+PORT=8000

+ 1 - 0
Cargo.lock

@@ -1026,6 +1026,7 @@ dependencies = [
  "actix-web",
  "argon2",
  "chrono",
+ "dotenvy",
  "email_address",
  "rumqttc",
  "serde",

+ 1 - 0
Cargo.toml

@@ -8,6 +8,7 @@ actix-cors = "0.7.1"
 actix-web = "4.13.0"
 argon2 = "0.5.3"
 chrono = { version="0.4.44", features=["serde"] }
+dotenvy = "0.15.7"
 email_address = "0.2.9"
 rumqttc = "0.25.1"
 serde = "1.0.228"

+ 10 - 1
src/main.rs

@@ -2,6 +2,7 @@ use actix_web::{HttpServer, web, middleware, App};
 use actix_cors::Cors;
 use sqlx::PgPool;
 use rumqttc::{MqttOptions, AsyncClient};
+use std::env;
 use crate::app_error::AppError;
 
 mod app_error;
@@ -13,6 +14,14 @@ mod auth;
 
 #[actix_web::main]
 async fn main() -> Result<(), AppError> {
+    //Environment variables
+    dotenvy::dotenv().expect("Failed to load .env file");
+
+    let port:u16 = env::var("PORT")
+        .expect("Environment variable PORT not set")
+        .parse()
+        .expect("PORT must be a valid number");
+
     //MQTT
     let mut options = MqttOptions::new("actix_server", "localhost", 1883);
     options.set_keep_alive(std::time::Duration::from_secs(10));
@@ -64,7 +73,7 @@ async fn main() -> Result<(), AppError> {
             .configure(routes::home::config)
             .configure(routes::device::config)
     })
-        .bind(("0.0.0.0", 8000))
+        .bind(("0.0.0.0", port))
         .expect("Failed to bind to port")
         .run()
         .await