Add basic server config
This commit is contained in:
		
							
								
								
									
										1
									
								
								virtweb_backend/Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										1
									
								
								virtweb_backend/Cargo.lock
									
									
									
										generated
									
									
									
								
							@@ -1856,6 +1856,7 @@ dependencies = [
 | 
				
			|||||||
 "actix-web",
 | 
					 "actix-web",
 | 
				
			||||||
 "clap",
 | 
					 "clap",
 | 
				
			||||||
 "env_logger",
 | 
					 "env_logger",
 | 
				
			||||||
 | 
					 "futures-util",
 | 
				
			||||||
 "lazy_static",
 | 
					 "lazy_static",
 | 
				
			||||||
 "light-openid",
 | 
					 "light-openid",
 | 
				
			||||||
 "log",
 | 
					 "log",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,3 +16,4 @@ actix-remote-ip = "0.1.0"
 | 
				
			|||||||
actix-session = { version = "0.7.2", features = ["cookie-session"] }
 | 
					actix-session = { version = "0.7.2", features = ["cookie-session"] }
 | 
				
			||||||
actix-identity = "0.5.2"
 | 
					actix-identity = "0.5.2"
 | 
				
			||||||
serde = { version = "1.0.175", features = ["derive"] }
 | 
					serde = { version = "1.0.175", features = ["derive"] }
 | 
				
			||||||
 | 
					futures-util = "0.3.28"
 | 
				
			||||||
@@ -1,5 +1,20 @@
 | 
				
			|||||||
 | 
					use crate::app_config::AppConfig;
 | 
				
			||||||
 | 
					use crate::extractors::local_auth_extractor::LocalAuthEnabled;
 | 
				
			||||||
use actix_web::{HttpResponse, Responder};
 | 
					use actix_web::{HttpResponse, Responder};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub async fn root_index() -> impl Responder {
 | 
					pub async fn root_index() -> impl Responder {
 | 
				
			||||||
    HttpResponse::Ok().body("Hello world!")
 | 
					    HttpResponse::Ok().body("Hello world!")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(serde::Serialize)]
 | 
				
			||||||
 | 
					struct StaticConfig {
 | 
				
			||||||
 | 
					    local_auth_enabled: bool,
 | 
				
			||||||
 | 
					    oidc_auth_enabled: bool,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub async fn static_config(local_auth: LocalAuthEnabled) -> impl Responder {
 | 
				
			||||||
 | 
					    HttpResponse::Ok().json(StaticConfig {
 | 
				
			||||||
 | 
					        local_auth_enabled: *local_auth,
 | 
				
			||||||
 | 
					        oidc_auth_enabled: !AppConfig::get().disable_oidc,
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										1
									
								
								virtweb_backend/src/extractors.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								virtweb_backend/src/extractors.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
				
			|||||||
 | 
					pub mod local_auth_extractor;
 | 
				
			||||||
							
								
								
									
										33
									
								
								virtweb_backend/src/extractors/local_auth_extractor.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								virtweb_backend/src/extractors/local_auth_extractor.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
				
			|||||||
 | 
					use crate::app_config::AppConfig;
 | 
				
			||||||
 | 
					use actix_web::dev::Payload;
 | 
				
			||||||
 | 
					use actix_web::{Error, FromRequest, HttpRequest};
 | 
				
			||||||
 | 
					use futures_util::future::{ready, Ready};
 | 
				
			||||||
 | 
					use std::ops::Deref;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Debug, Copy, Clone, PartialEq)]
 | 
				
			||||||
 | 
					pub struct LocalAuthEnabled(bool);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Deref for LocalAuthEnabled {
 | 
				
			||||||
 | 
					    type Target = bool;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn deref(&self) -> &Self::Target {
 | 
				
			||||||
 | 
					        &self.0
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl FromRequest for LocalAuthEnabled {
 | 
				
			||||||
 | 
					    type Error = Error;
 | 
				
			||||||
 | 
					    type Future = Ready<Result<Self, Error>>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
 | 
				
			||||||
 | 
					        if AppConfig::get().disable_local_auth {
 | 
				
			||||||
 | 
					            return ready(Ok(Self(false)));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        let has_disable_local_auth_header = req
 | 
				
			||||||
 | 
					            .headers()
 | 
				
			||||||
 | 
					            .get(&AppConfig::get().disable_auth_header_token)
 | 
				
			||||||
 | 
					            .is_some();
 | 
				
			||||||
 | 
					        return ready(Ok(Self(!has_disable_local_auth_header)));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,3 +1,4 @@
 | 
				
			|||||||
pub mod app_config;
 | 
					pub mod app_config;
 | 
				
			||||||
pub mod constants;
 | 
					pub mod constants;
 | 
				
			||||||
pub mod controllers;
 | 
					pub mod controllers;
 | 
				
			||||||
 | 
					pub mod extractors;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -43,6 +43,10 @@ async fn main() -> std::io::Result<()> {
 | 
				
			|||||||
                proxy: AppConfig::get().proxy_ip.clone(),
 | 
					                proxy: AppConfig::get().proxy_ip.clone(),
 | 
				
			||||||
            }))
 | 
					            }))
 | 
				
			||||||
            .route("/", web::get().to(server_controller::root_index))
 | 
					            .route("/", web::get().to(server_controller::root_index))
 | 
				
			||||||
 | 
					            .route(
 | 
				
			||||||
 | 
					                "/api/server/static_config",
 | 
				
			||||||
 | 
					                web::get().to(server_controller::static_config),
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
    .bind(&AppConfig::get().listen_address)?
 | 
					    .bind(&AppConfig::get().listen_address)?
 | 
				
			||||||
    .run()
 | 
					    .run()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user