implemented logging for main components
This commit is contained in:
parent
21f4449da0
commit
d7dd730a88
6 changed files with 102 additions and 29 deletions
|
@ -47,7 +47,7 @@ impl Configuration {
|
|||
|
||||
fn write_config(data: &Self) {
|
||||
let serialized = ron::to_string(data).unwrap();
|
||||
write(CONFIG_PATHS[0], serialized);
|
||||
write(CONFIG_PATHS[0], serialized).unwrap_or_else(|_| ());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,19 +2,49 @@ pub mod models;
|
|||
|
||||
use sled::Db;
|
||||
|
||||
use super::{config::Configuration, harsh::Error};
|
||||
use super::{
|
||||
config::Configuration,
|
||||
harsh::Error,
|
||||
log::{Loggable, Logger},
|
||||
};
|
||||
|
||||
///
|
||||
/// handle the database access
|
||||
///
|
||||
pub struct DbManager {
|
||||
handle: Db,
|
||||
path: String,
|
||||
handle: Option<Db>,
|
||||
logger: Logger,
|
||||
}
|
||||
|
||||
impl DbManager {
|
||||
/// constructor
|
||||
pub fn new(config: Configuration) -> Result<Self, Error> {
|
||||
let handle = sled::open(config.database_path)?;
|
||||
Ok(DbManager { handle })
|
||||
pub fn new(config: Configuration, logger: Logger) -> Result<Self, Error> {
|
||||
let mut result = DbManager {
|
||||
path: config.database_path,
|
||||
handle: None,
|
||||
logger,
|
||||
};
|
||||
result.info("ininitalized new manager");
|
||||
|
||||
result.open_db()?;
|
||||
result.info("openned database");
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn open_db(&mut self) -> Result<(), Error> {
|
||||
let handle = sled::open(&self.path)?;
|
||||
self.handle = Some(handle);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Loggable for DbManager {
|
||||
fn name(&self) -> String {
|
||||
"db manager".to_string()
|
||||
}
|
||||
|
||||
fn logger(&self) -> Logger {
|
||||
self.logger.clone()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,10 +22,10 @@ pub struct Harsh {
|
|||
|
||||
impl Harsh {
|
||||
pub fn new(configuration: Configuration) -> Self {
|
||||
let db_manager =
|
||||
DbManager::new(configuration.clone()).expect("failed to open / create the database");
|
||||
let logger = Logger::new(configuration.clone());
|
||||
let shared_state = State::new_shared(db_manager, logger);
|
||||
let db_manager = DbManager::new(configuration.clone(), logger.clone())
|
||||
.expect("failed to open / create database");
|
||||
let shared_state = State::new_shared(db_manager, logger.clone());
|
||||
let http_manager = HttpManager::new(configuration.clone(), shared_state.clone());
|
||||
|
||||
Harsh {
|
||||
|
@ -49,6 +49,7 @@ pub struct State {
|
|||
|
||||
impl State {
|
||||
pub fn new_shared(db_manager: DbManager, logger: Logger) -> SharedState {
|
||||
logger.info("initialized shared state");
|
||||
let state = State {
|
||||
db_manager: RwLock::new(db_manager),
|
||||
logger: RwLock::new(logger),
|
||||
|
|
|
@ -14,32 +14,51 @@ use self::routes::setup_routes;
|
|||
|
||||
use super::config::Configuration;
|
||||
use super::harsh::SharedState;
|
||||
use super::log::Loggable;
|
||||
use super::log::Logger;
|
||||
|
||||
///
|
||||
/// listen for incoming requests and handle both incoming and outgoing requests
|
||||
///
|
||||
pub struct HttpManager {
|
||||
_shared_state: SharedState,
|
||||
shared_state: SharedState,
|
||||
port: u32,
|
||||
app: Router,
|
||||
logger: Logger,
|
||||
}
|
||||
|
||||
impl HttpManager {
|
||||
/// constructor
|
||||
pub fn new(_config: Configuration, shared_state: SharedState) -> Self {
|
||||
pub fn new(config: Configuration, shared_state: SharedState) -> Self {
|
||||
let app = setup_routes(Router::new()).layer(AddExtensionLayer::new(shared_state.clone()));
|
||||
let logger = shared_state.logger.try_read().unwrap().clone();
|
||||
|
||||
HttpManager {
|
||||
_shared_state: shared_state.clone(),
|
||||
let result = HttpManager {
|
||||
shared_state: shared_state.clone(),
|
||||
port: config.port,
|
||||
app,
|
||||
}
|
||||
logger,
|
||||
};
|
||||
result.info("initialized new manager");
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// listen for and handle received requests
|
||||
pub async fn serve(self) {
|
||||
let address = "0.0.0.0:3000".parse().unwrap();
|
||||
axum::Server::bind(&address)
|
||||
.serve(self.app.into_make_service())
|
||||
.await
|
||||
.unwrap();
|
||||
let address = format!("0.0.0.0:{}", self.port).parse().unwrap();
|
||||
self.info(format!("listening to address '{}'", address));
|
||||
let server = axum::Server::bind(&address).serve(self.app.into_make_service());
|
||||
server.await.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
impl Loggable for HttpManager {
|
||||
fn name(&self) -> String {
|
||||
"http manager".to_string()
|
||||
}
|
||||
|
||||
fn logger(&self) -> Logger {
|
||||
self.logger.clone()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
use std::{borrow::Borrow, ops::Deref};
|
||||
|
||||
use super::config::Configuration;
|
||||
use chrono::prelude::*;
|
||||
use colored::Colorize;
|
||||
|
@ -5,6 +7,7 @@ use colored::Colorize;
|
|||
///
|
||||
/// configurable structure with methods to log informations in different ways
|
||||
///
|
||||
#[derive(Clone)]
|
||||
pub struct Logger {
|
||||
//
|
||||
}
|
||||
|
@ -15,19 +18,16 @@ impl Logger {
|
|||
Logger {}
|
||||
}
|
||||
|
||||
pub fn info(&self, msg: String) {
|
||||
let tag_section = "[info]".blue();
|
||||
println!("{} {} {}", time_segment(), tag_section, msg);
|
||||
pub fn info<T: Into<String>>(&self, msg: T) {
|
||||
println!("{} {} {}", time_segment(), "[info]".blue(), msg.into());
|
||||
}
|
||||
|
||||
pub fn warn(&self, msg: String) {
|
||||
let tag_section = "[warn]".yellow();
|
||||
println!("{} {} {}", time_segment(), tag_section, msg);
|
||||
pub fn warn<T: Into<String>>(&self, msg: T) {
|
||||
println!("{} {} {}", time_segment(), "[warn]".yellow(), msg.into());
|
||||
}
|
||||
|
||||
pub fn fail(&self, msg: String) {
|
||||
let tag_section = "[fail]".red();
|
||||
println!("{} {} {}", time_segment(), tag_section, msg);
|
||||
pub fn fail<T: Into<String>>(&self, msg: T) {
|
||||
println!("{} {} {}", time_segment(), "[fail]".red(), msg.into());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -37,3 +37,27 @@ fn time_segment() -> String {
|
|||
let result = now.format("[%Y.%m.%d-%H:%M:%S]").to_string();
|
||||
result
|
||||
}
|
||||
|
||||
pub trait Loggable {
|
||||
fn name(&self) -> String;
|
||||
|
||||
fn logger(&self) -> Logger;
|
||||
|
||||
fn info<T: Into<String>>(&self, msg: T) {
|
||||
self.logger().info(self.format_message(msg));
|
||||
}
|
||||
|
||||
fn warn<T: Into<String>>(&self, msg: T) {
|
||||
self.logger().warn(self.format_message(msg));
|
||||
}
|
||||
|
||||
fn fail<T: Into<String>>(&self, msg: T) {
|
||||
self.logger().fail(self.format_message(msg));
|
||||
}
|
||||
|
||||
fn format_message<T: Into<String>>(&self, msg: T) -> String {
|
||||
let message: String = msg.into();
|
||||
let formatted = format!("[{}] {}", self.name(), message);
|
||||
formatted
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,5 @@ pub mod lib;
|
|||
|
||||
#[tokio::main]
|
||||
pub async fn main() {
|
||||
println!("Hello, harmony!");
|
||||
lib::harsh::main().await;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue