Create base Rust project
This commit is contained in:
		
							
								
								
									
										160
									
								
								src/app_config.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										160
									
								
								src/app_config.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,160 @@
 | 
			
		||||
use clap::Parser;
 | 
			
		||||
use s3::creds::Credentials;
 | 
			
		||||
use s3::{Bucket, Region};
 | 
			
		||||
 | 
			
		||||
/// GeneIT backend API
 | 
			
		||||
#[derive(Parser, Debug, Clone)]
 | 
			
		||||
#[clap(author, version, about, long_about = None)]
 | 
			
		||||
pub struct AppConfig {
 | 
			
		||||
    /// Listen address
 | 
			
		||||
    #[clap(short, long, env, default_value = "0.0.0.0:8000")]
 | 
			
		||||
    pub listen_address: String,
 | 
			
		||||
 | 
			
		||||
    /// Website origin
 | 
			
		||||
    #[clap(short, long, env, default_value = "http://localhost:8000")]
 | 
			
		||||
    pub website_origin: String,
 | 
			
		||||
 | 
			
		||||
    /// Proxy IP, might end with a star "*"
 | 
			
		||||
    #[clap(short, long, env)]
 | 
			
		||||
    pub proxy_ip: Option<String>,
 | 
			
		||||
 | 
			
		||||
    /// Matrix API origin
 | 
			
		||||
    #[clap(short, long, env, default_value = "http://127.0.0.1:8448")]
 | 
			
		||||
    pub matrix_api: String,
 | 
			
		||||
 | 
			
		||||
    /// Redis connection hostname
 | 
			
		||||
    #[clap(long, env, default_value = "localhost")]
 | 
			
		||||
    redis_hostname: String,
 | 
			
		||||
 | 
			
		||||
    /// Redis connection port
 | 
			
		||||
    #[clap(long, env, default_value_t = 6379)]
 | 
			
		||||
    redis_port: u16,
 | 
			
		||||
 | 
			
		||||
    /// Redis database number
 | 
			
		||||
    #[clap(long, env, default_value_t = 0)]
 | 
			
		||||
    redis_db_number: i64,
 | 
			
		||||
 | 
			
		||||
    /// Redis username
 | 
			
		||||
    #[clap(long, env)]
 | 
			
		||||
    redis_username: Option<String>,
 | 
			
		||||
 | 
			
		||||
    /// Redis password
 | 
			
		||||
    #[clap(long, env, default_value = "secretredis")]
 | 
			
		||||
    redis_password: String,
 | 
			
		||||
 | 
			
		||||
    /// URL where the OpenID configuration can be found
 | 
			
		||||
    #[arg(
 | 
			
		||||
        long,
 | 
			
		||||
        env,
 | 
			
		||||
        default_value = "http://localhost:9001/dex/.well-known/openid-configuration"
 | 
			
		||||
    )]
 | 
			
		||||
    pub oidc_configuration_url: String,
 | 
			
		||||
 | 
			
		||||
    /// OpenID client ID
 | 
			
		||||
    #[arg(long, env, default_value = "foo")]
 | 
			
		||||
    pub oidc_client_id: String,
 | 
			
		||||
 | 
			
		||||
    /// OpenID client secret
 | 
			
		||||
    #[arg(long, env, default_value = "bar")]
 | 
			
		||||
    pub oidc_client_secret: String,
 | 
			
		||||
 | 
			
		||||
    /// OpenID login redirect URL
 | 
			
		||||
    #[arg(long, env, default_value = "APP_ORIGIN/oidc_cb")]
 | 
			
		||||
    oidc_redirect_url: String,
 | 
			
		||||
 | 
			
		||||
    /// S3 Bucket name
 | 
			
		||||
    #[arg(long, env, default_value = "matrix-gw")]
 | 
			
		||||
    s3_bucket_name: String,
 | 
			
		||||
 | 
			
		||||
    /// S3 region (if not using Minio)
 | 
			
		||||
    #[arg(long, env, default_value = "eu-central-1")]
 | 
			
		||||
    s3_region: String,
 | 
			
		||||
 | 
			
		||||
    /// S3 API endpoint
 | 
			
		||||
    #[arg(long, env, default_value = "http://localhost:9000")]
 | 
			
		||||
    s3_endpoint: String,
 | 
			
		||||
 | 
			
		||||
    /// S3 access key
 | 
			
		||||
    #[arg(long, env, default_value = "topsecret")]
 | 
			
		||||
    s3_access_key: String,
 | 
			
		||||
 | 
			
		||||
    /// S3 secret key
 | 
			
		||||
    #[arg(long, env, default_value = "topsecret")]
 | 
			
		||||
    s3_secret_key: String,
 | 
			
		||||
 | 
			
		||||
    /// S3 skip auto create bucket if not existing
 | 
			
		||||
    #[arg(long, env)]
 | 
			
		||||
    pub s3_skip_auto_create_bucket: bool,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
lazy_static::lazy_static! {
 | 
			
		||||
    static ref ARGS: AppConfig = {
 | 
			
		||||
        AppConfig::parse()
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl AppConfig {
 | 
			
		||||
    /// Get parsed command line arguments
 | 
			
		||||
    pub fn get() -> &'static AppConfig {
 | 
			
		||||
        &ARGS
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Get Redis connection configuration
 | 
			
		||||
    pub fn redis_connection_config(&self) -> redis::ConnectionInfo {
 | 
			
		||||
        redis::ConnectionInfo {
 | 
			
		||||
            addr: redis::ConnectionAddr::Tcp(self.redis_hostname.clone(), self.redis_port),
 | 
			
		||||
            redis: redis::RedisConnectionInfo {
 | 
			
		||||
                db: self.redis_db_number,
 | 
			
		||||
                username: self.redis_username.clone(),
 | 
			
		||||
                password: Some(self.redis_password.clone()),
 | 
			
		||||
                protocol: Default::default(),
 | 
			
		||||
            },
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Get OpenID providers configuration
 | 
			
		||||
    pub fn openid_providers(&self) -> OIDCProvider<'_> {
 | 
			
		||||
        OIDCProvider {
 | 
			
		||||
            client_id: self.oidc_client_id.as_str(),
 | 
			
		||||
            client_secret: self.oidc_client_secret.as_str(),
 | 
			
		||||
            configuration_url: self.oidc_configuration_url.as_str(),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Get OIDC callback URL
 | 
			
		||||
    pub fn oidc_redirect_url(&self) -> String {
 | 
			
		||||
        self.oidc_redirect_url
 | 
			
		||||
            .replace("APP_ORIGIN", &self.website_origin)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Get s3 bucket credentials
 | 
			
		||||
    pub fn s3_credentials(&self) -> anyhow::Result<Credentials> {
 | 
			
		||||
        Ok(Credentials::new(
 | 
			
		||||
            Some(&self.s3_access_key),
 | 
			
		||||
            Some(&self.s3_secret_key),
 | 
			
		||||
            None,
 | 
			
		||||
            None,
 | 
			
		||||
            None,
 | 
			
		||||
        )?)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Get S3 bucket
 | 
			
		||||
    pub fn s3_bucket(&self) -> anyhow::Result<Box<Bucket>> {
 | 
			
		||||
        Ok(Bucket::new(
 | 
			
		||||
            &self.s3_bucket_name,
 | 
			
		||||
            Region::Custom {
 | 
			
		||||
                region: self.s3_region.to_string(),
 | 
			
		||||
                endpoint: self.s3_endpoint.to_string(),
 | 
			
		||||
            },
 | 
			
		||||
            self.s3_credentials()?,
 | 
			
		||||
        )?
 | 
			
		||||
        .with_path_style())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Clone, serde::Serialize)]
 | 
			
		||||
pub struct OIDCProvider<'a> {
 | 
			
		||||
    pub client_id: &'a str,
 | 
			
		||||
    pub client_secret: &'a str,
 | 
			
		||||
    pub configuration_url: &'a str,
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										1
									
								
								src/lib.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/lib.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
pub mod app_config;
 | 
			
		||||
							
								
								
									
										28
									
								
								src/main.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/main.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
 | 
			
		||||
use matrix_gateway::app_config::AppConfig;
 | 
			
		||||
 | 
			
		||||
async fn home() -> HttpResponse {
 | 
			
		||||
    HttpResponse::Ok().body("Hey there!")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[actix_web::main]
 | 
			
		||||
async fn main() -> std::io::Result<()> {
 | 
			
		||||
    env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
 | 
			
		||||
 | 
			
		||||
    log::info!(
 | 
			
		||||
        "Starting to listen on {} for {}",
 | 
			
		||||
        AppConfig::get().listen_address,
 | 
			
		||||
        AppConfig::get().website_origin
 | 
			
		||||
    );
 | 
			
		||||
    HttpServer::new(|| {
 | 
			
		||||
        App::new()
 | 
			
		||||
            // Web configuration routes
 | 
			
		||||
            .route("/", web::get().to(home))
 | 
			
		||||
 | 
			
		||||
        // API routes
 | 
			
		||||
        // TODO
 | 
			
		||||
    })
 | 
			
		||||
    .bind(&AppConfig::get().listen_address)?
 | 
			
		||||
    .run()
 | 
			
		||||
    .await
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user