Add base server
This commit is contained in:
		
							
								
								
									
										43
									
								
								geneit_backend/src/app_config.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								geneit_backend/src/app_config.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
			
		||||
use clap::Parser;
 | 
			
		||||
 | 
			
		||||
/// 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:3000")]
 | 
			
		||||
    pub website_origin: String,
 | 
			
		||||
 | 
			
		||||
    /// Proxy IP, might end with a star "*"
 | 
			
		||||
    #[clap(short, long, env)]
 | 
			
		||||
    pub proxy_ip: Option<String>,
 | 
			
		||||
 | 
			
		||||
    /// PostgreSQL connexion chain
 | 
			
		||||
    #[clap(long, env, default_value = "postgres://localhost/geneit")]
 | 
			
		||||
    db_chain: String,
 | 
			
		||||
 | 
			
		||||
    /// PostgreSQL username
 | 
			
		||||
    #[clap(long, env, default_value = "user")]
 | 
			
		||||
    db_username: String,
 | 
			
		||||
 | 
			
		||||
    /// PostgreSQL password
 | 
			
		||||
    #[clap(long, env, default_value = "user")]
 | 
			
		||||
    db_password: String,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
lazy_static::lazy_static! {
 | 
			
		||||
    static ref ARGS: AppConfig = {
 | 
			
		||||
        AppConfig::parse()
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl AppConfig {
 | 
			
		||||
    /// Get parsed command line arguments
 | 
			
		||||
    pub fn get() -> &'static AppConfig {
 | 
			
		||||
        &ARGS
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										28
									
								
								geneit_backend/src/constants.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								geneit_backend/src/constants.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
 | 
			
		||||
pub struct SizeConstraint {
 | 
			
		||||
    min: usize,
 | 
			
		||||
    max: usize,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl SizeConstraint {
 | 
			
		||||
    pub fn new(min: usize, max: usize) -> Self {
 | 
			
		||||
        Self { min, max }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Clone, serde::Serialize)]
 | 
			
		||||
pub struct StaticConstraints {
 | 
			
		||||
    pub mail_len: SizeConstraint,
 | 
			
		||||
    pub user_name_len: SizeConstraint,
 | 
			
		||||
    pub password_len: SizeConstraint,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Default for StaticConstraints {
 | 
			
		||||
    fn default() -> Self {
 | 
			
		||||
        Self {
 | 
			
		||||
            mail_len: SizeConstraint::new(5, 255),
 | 
			
		||||
            user_name_len: SizeConstraint::new(3, 30),
 | 
			
		||||
            password_len: SizeConstraint::new(8, 255),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										1
									
								
								geneit_backend/src/controllers.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								geneit_backend/src/controllers.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
pub mod config_controller;
 | 
			
		||||
							
								
								
									
										17
									
								
								geneit_backend/src/controllers/config_controller.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								geneit_backend/src/controllers/config_controller.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
use crate::constants::StaticConstraints;
 | 
			
		||||
use actix_web::{HttpResponse, Responder};
 | 
			
		||||
 | 
			
		||||
/// Default hello route
 | 
			
		||||
pub async fn home() -> impl Responder {
 | 
			
		||||
    HttpResponse::Ok().json("GeneIT API service.")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Clone, serde::Serialize, Default)]
 | 
			
		||||
struct StaticConfig {
 | 
			
		||||
    constraints: StaticConstraints,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Get static configuration
 | 
			
		||||
pub async fn static_config() -> impl Responder {
 | 
			
		||||
    HttpResponse::Ok().json(StaticConfig::default())
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										4
									
								
								geneit_backend/src/lib.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								geneit_backend/src/lib.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
			
		||||
pub mod app_config;
 | 
			
		||||
pub mod constants;
 | 
			
		||||
pub mod controllers;
 | 
			
		||||
pub mod schema;
 | 
			
		||||
@@ -1,3 +1,23 @@
 | 
			
		||||
fn main() {
 | 
			
		||||
    println!("Hello, world!");
 | 
			
		||||
use actix_web::{web, App, HttpServer};
 | 
			
		||||
use geneit_backend::app_config::AppConfig;
 | 
			
		||||
use geneit_backend::controllers::config_controller;
 | 
			
		||||
 | 
			
		||||
#[actix_web::main]
 | 
			
		||||
async fn main() -> std::io::Result<()> {
 | 
			
		||||
    env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
 | 
			
		||||
 | 
			
		||||
    log::info!("Start to listen on {}", AppConfig::get().listen_address);
 | 
			
		||||
 | 
			
		||||
    HttpServer::new(|| {
 | 
			
		||||
        App::new()
 | 
			
		||||
            // Config controller
 | 
			
		||||
            .route("/", web::get().to(config_controller::home))
 | 
			
		||||
            .route(
 | 
			
		||||
                "/config/static",
 | 
			
		||||
                web::get().to(config_controller::static_config),
 | 
			
		||||
            )
 | 
			
		||||
    })
 | 
			
		||||
    .bind(AppConfig::get().listen_address.as_str())?
 | 
			
		||||
    .run()
 | 
			
		||||
    .await
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@
 | 
			
		||||
diesel::table! {
 | 
			
		||||
    users (id) {
 | 
			
		||||
        id -> Int4,
 | 
			
		||||
        name -> Varchar,
 | 
			
		||||
        email -> Varchar,
 | 
			
		||||
        password -> Nullable<Varchar>,
 | 
			
		||||
        reset_password_token -> Nullable<Varchar>,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user