Add base server

This commit is contained in:
Pierre HUBERT 2023-05-24 14:38:18 +02:00
parent df64d51445
commit 9912428fd6
10 changed files with 1292 additions and 19 deletions

1185
geneit_backend/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -6,5 +6,10 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
log = "0.4.17"
env_logger = "0.10.0"
clap = { version = "4.3.0", features = ["derive", "env"] } clap = { version = "4.3.0", features = ["derive", "env"] }
lazy_static = "1.4.0"
actix-web = "4.3.1"
diesel = { version = "2.0.4", features = ["postgres"] } diesel = { version = "2.0.4", features = ["postgres"] }
serde = { version = "1.0.163", features = ["derive"] }

View File

@ -1,6 +1,7 @@
-- Create table -- Create table
CREATE TABLE users ( CREATE TABLE users (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
name VARCHAR(30) NOT NULL,
email VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL,
password VARCHAR NULL, password VARCHAR NULL,
reset_password_token VARCHAR(150) NULL, reset_password_token VARCHAR(150) NULL,

View 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
}
}

View 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),
}
}
}

View File

@ -0,0 +1 @@
pub mod config_controller;

View 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())
}

View File

@ -0,0 +1,4 @@
pub mod app_config;
pub mod constants;
pub mod controllers;
pub mod schema;

View File

@ -1,3 +1,23 @@
fn main() { use actix_web::{web, App, HttpServer};
println!("Hello, world!"); 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
} }

View File

@ -3,6 +3,7 @@
diesel::table! { diesel::table! {
users (id) { users (id) {
id -> Int4, id -> Int4,
name -> Varchar,
email -> Varchar, email -> Varchar,
password -> Nullable<Varchar>, password -> Nullable<Varchar>,
reset_password_token -> Nullable<Varchar>, reset_password_token -> Nullable<Varchar>,