setup main project layout

Co-authored-by: Clément Rehs <clement.rehs@epita.fr>
This commit is contained in:
Jolimaitre Matthieu 2021-11-11 20:28:45 +01:00
parent a71a059feb
commit b95003257d
12 changed files with 963 additions and 4 deletions

13
src/lib/config.rs Normal file
View file

@ -0,0 +1,13 @@
///
/// Encapsulate a configuration for the server
///
pub struct Configuration {
pub path: String, //
}
impl Configuration {
/// try to read the configuration from local file or load default
pub fn read() -> Self {
todo!()
}
}

15
src/lib/database.rs Normal file
View file

@ -0,0 +1,15 @@
use super::config::Configuration;
///
/// handle the database access
///
pub struct DbManager {
//
}
impl DbManager {
/// constructor
pub fn new(_config: &Configuration) -> Self {
todo!()
}
}

14
src/lib/harsh.rs Normal file
View file

@ -0,0 +1,14 @@
use super::config::Configuration;
use super::database::DbManager;
use super::http::serve;
use super::log::Logger;
///
/// main function
///
pub fn main() {
let configuration = Configuration::read();
Logger::configure(&configuration);
let mut db_manager = DbManager::new(&configuration);
serve(&configuration, &mut db_manager);
}

9
src/lib/http.rs Normal file
View file

@ -0,0 +1,9 @@
use super::config::Configuration;
use super::database::DbManager;
///
/// listen for and handle received requests
///
pub fn serve(_config: &Configuration, _db_manager: &mut DbManager) {
todo!()
}

59
src/lib/log.rs Normal file
View file

@ -0,0 +1,59 @@
use std::cell::RefCell;
use super::config::Configuration;
use chrono::prelude::*;
use colored::Colorize;
///
/// a static custom logger
///
pub struct Logger {
//
}
/// static Logger instance
pub const LOGGER: RefCell<Logger> = RefCell::new(Logger {});
impl Logger {
/// adapt the static instance according to parameters specified by the configuration
pub fn configure(_config: &Configuration) {
let refcell = LOGGER;
let _logger = refcell.try_borrow_mut().unwrap();
todo!()
}
fn log_info(&self, msg: String) {
let tag_section = "[info]".blue();
println!("{} {} {}", time_section(), tag_section, msg);
}
fn log_warn(&self, msg: String) {
let tag_section = "[warn]".yellow();
println!("{} {} {}", time_section(), tag_section, msg);
}
fn log_fail(&self, msg: String) {
let tag_section = "[fail]".red();
println!("{} {} {}", time_section(), tag_section, msg);
}
}
fn time_section() -> String {
let now = Local::now();
let result = now.format("[%Y.%m.%d-%H:%M:%S]").to_string();
result
}
pub mod shorthands {
pub fn log_info(msg: String) {
super::LOGGER.try_borrow().unwrap().log_info(msg);
}
pub fn log_warn(msg: String) {
super::LOGGER.try_borrow().unwrap().log_warn(msg);
}
pub fn log_fail(msg: String) {
super::LOGGER.try_borrow().unwrap().log_fail(msg);
}
}

5
src/lib/mod.rs Normal file
View file

@ -0,0 +1,5 @@
pub mod config;
pub mod database;
pub mod harsh;
pub mod http;
pub mod log;

View file

@ -1,3 +1,6 @@
fn main() {
println!("Hello, harmony!");
pub mod lib;
pub fn main() {
println!("Hello, harmony!");
lib::harsh::main();
}