فهرست منبع

In development, serve front-end from file instead of binary.

Lee Morgan 3 هفته پیش
والد
کامیت
8ac179b386
3فایلهای تغییر یافته به همراه52 افزوده شده و 9 حذف شده
  1. 26 9
      src/controllers/home.rs
  2. 22 0
      src/environment.rs
  3. 4 0
      src/main.rs

+ 26 - 9
src/controllers/home.rs

@@ -1,19 +1,36 @@
 use actix_web::{HttpResponse, web, get};
-use std::sync::Mutex;
-use crate::users::User;
+use std::{fs, sync::Mutex, borrow::Cow};
+use crate::{
+    users::User,
+    environment::Environment
+};
 
 const HTML: &str = include_str!("../../ui/build.html");
 
 #[get("/")]
-pub async fn route(users: web::Data<Mutex<Vec<User>>>) -> HttpResponse {
-    if users.lock().unwrap().is_empty() {
-        let updated_html = HTML.replacen(
+pub async fn route(
+    users: web::Data<Mutex<Vec<User>>>,
+    app_env: web::Data<Environment>
+) -> HttpResponse {
+    let html: Cow<str> = match **app_env {
+        Environment::Development => {
+            Cow::Owned(
+                fs::read_to_string("./ui/build.html")
+                    .expect("Failed to read build file")
+            )
+        },
+        Environment::Production => Cow::Borrowed(HTML)
+    };
+
+    let final_html = if users.lock().unwrap().is_empty() {
+        Cow::Owned(html.replacen(
             "window.adminExists=\"true\";",
             "window.adminExists=\"false\";",
             1
-        );
-        HttpResponse::Ok().body(updated_html)
+        ))
     } else {
-        HttpResponse::Ok().body(HTML)
-    }
+        html
+    };
+
+    HttpResponse::Ok().body(final_html)
 }

+ 22 - 0
src/environment.rs

@@ -0,0 +1,22 @@
+use std::str::FromStr;
+
+#[derive(Clone)]
+pub enum Environment {
+    Development,
+    Production
+}
+
+#[derive(Debug)]
+pub struct ParseEnvironmentError;
+
+impl FromStr for Environment {
+    type Err = ParseEnvironmentError;
+
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        match s.to_lowercase().as_str() {
+            "development" => Ok(Environment::Development),
+            "production" => Ok(Environment::Production),
+            _ => Err(ParseEnvironmentError)
+        }
+    }
+}

+ 4 - 0
src/main.rs

@@ -2,12 +2,14 @@ use actix_web::{HttpServer, App, middleware, web, rt};
 use awc::ws::Message;
 use tokio::sync::mpsc;
 use std::sync::Mutex;
+use crate::environment::Environment;
 
 mod routes;
 mod controllers;
 mod websocket;
 mod users;
 mod logic;
+mod environment;
 
 #[actix_web::main]
 async fn main() -> Result<(), std::io::Error> {
@@ -16,6 +18,7 @@ async fn main() -> Result<(), std::io::Error> {
     let port: u16 = env_var("PORT");
     let ha_ip: String = env_var("HA_IP");
     let ha_token: String = env_var("HA_TOKEN");
+    let app_env: Environment = env_var("APP_ENV");
 
     //Read User Data
     let users = Mutex::new(users::User::read());
@@ -31,6 +34,7 @@ async fn main() -> Result<(), std::io::Error> {
         App::new()
             .app_data(users_data.clone())
             .app_data(ha_sender.clone())
+            .app_data(app_env.clone())
             .wrap(middleware::Compress::default())
             .configure(routes::config)
     })