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) {
|
fn write_config(data: &Self) {
|
||||||
let serialized = ron::to_string(data).unwrap();
|
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 sled::Db;
|
||||||
|
|
||||||
use super::{config::Configuration, harsh::Error};
|
use super::{
|
||||||
|
config::Configuration,
|
||||||
|
harsh::Error,
|
||||||
|
log::{Loggable, Logger},
|
||||||
|
};
|
||||||
|
|
||||||
///
|
///
|
||||||
/// handle the database access
|
/// handle the database access
|
||||||
///
|
///
|
||||||
pub struct DbManager {
|
pub struct DbManager {
|
||||||
handle: Db,
|
path: String,
|
||||||
|
handle: Option<Db>,
|
||||||
|
logger: Logger,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DbManager {
|
impl DbManager {
|
||||||
/// constructor
|
/// constructor
|
||||||
pub fn new(config: Configuration) -> Result<Self, Error> {
|
pub fn new(config: Configuration, logger: Logger) -> Result<Self, Error> {
|
||||||
let handle = sled::open(config.database_path)?;
|
let mut result = DbManager {
|
||||||
Ok(DbManager { handle })
|
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 {
|
impl Harsh {
|
||||||
pub fn new(configuration: Configuration) -> Self {
|
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 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());
|
let http_manager = HttpManager::new(configuration.clone(), shared_state.clone());
|
||||||
|
|
||||||
Harsh {
|
Harsh {
|
||||||
|
@ -49,6 +49,7 @@ pub struct State {
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
pub fn new_shared(db_manager: DbManager, logger: Logger) -> SharedState {
|
pub fn new_shared(db_manager: DbManager, logger: Logger) -> SharedState {
|
||||||
|
logger.info("initialized shared state");
|
||||||
let state = State {
|
let state = State {
|
||||||
db_manager: RwLock::new(db_manager),
|
db_manager: RwLock::new(db_manager),
|
||||||
logger: RwLock::new(logger),
|
logger: RwLock::new(logger),
|
||||||
|
|
|
@ -14,32 +14,51 @@ use self::routes::setup_routes;
|
||||||
|
|
||||||
use super::config::Configuration;
|
use super::config::Configuration;
|
||||||
use super::harsh::SharedState;
|
use super::harsh::SharedState;
|
||||||
|
use super::log::Loggable;
|
||||||
|
use super::log::Logger;
|
||||||
|
|
||||||
///
|
///
|
||||||
/// listen for incoming requests and handle both incoming and outgoing requests
|
/// listen for incoming requests and handle both incoming and outgoing requests
|
||||||
///
|
///
|
||||||
pub struct HttpManager {
|
pub struct HttpManager {
|
||||||
_shared_state: SharedState,
|
shared_state: SharedState,
|
||||||
|
port: u32,
|
||||||
app: Router,
|
app: Router,
|
||||||
|
logger: Logger,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HttpManager {
|
impl HttpManager {
|
||||||
/// constructor
|
/// 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 app = setup_routes(Router::new()).layer(AddExtensionLayer::new(shared_state.clone()));
|
||||||
|
let logger = shared_state.logger.try_read().unwrap().clone();
|
||||||
|
|
||||||
HttpManager {
|
let result = HttpManager {
|
||||||
_shared_state: shared_state.clone(),
|
shared_state: shared_state.clone(),
|
||||||
|
port: config.port,
|
||||||
app,
|
app,
|
||||||
}
|
logger,
|
||||||
|
};
|
||||||
|
result.info("initialized new manager");
|
||||||
|
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
/// listen for and handle received requests
|
/// listen for and handle received requests
|
||||||
pub async fn serve(self) {
|
pub async fn serve(self) {
|
||||||
let address = "0.0.0.0:3000".parse().unwrap();
|
let address = format!("0.0.0.0:{}", self.port).parse().unwrap();
|
||||||
axum::Server::bind(&address)
|
self.info(format!("listening to address '{}'", address));
|
||||||
.serve(self.app.into_make_service())
|
let server = axum::Server::bind(&address).serve(self.app.into_make_service());
|
||||||
.await
|
server.await.unwrap();
|
||||||
.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 super::config::Configuration;
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
|
@ -5,6 +7,7 @@ use colored::Colorize;
|
||||||
///
|
///
|
||||||
/// configurable structure with methods to log informations in different ways
|
/// configurable structure with methods to log informations in different ways
|
||||||
///
|
///
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct Logger {
|
pub struct Logger {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
@ -15,19 +18,16 @@ impl Logger {
|
||||||
Logger {}
|
Logger {}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn info(&self, msg: String) {
|
pub fn info<T: Into<String>>(&self, msg: T) {
|
||||||
let tag_section = "[info]".blue();
|
println!("{} {} {}", time_segment(), "[info]".blue(), msg.into());
|
||||||
println!("{} {} {}", time_segment(), tag_section, msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn warn(&self, msg: String) {
|
pub fn warn<T: Into<String>>(&self, msg: T) {
|
||||||
let tag_section = "[warn]".yellow();
|
println!("{} {} {}", time_segment(), "[warn]".yellow(), msg.into());
|
||||||
println!("{} {} {}", time_segment(), tag_section, msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fail(&self, msg: String) {
|
pub fn fail<T: Into<String>>(&self, msg: T) {
|
||||||
let tag_section = "[fail]".red();
|
println!("{} {} {}", time_segment(), "[fail]".red(), msg.into());
|
||||||
println!("{} {} {}", time_segment(), tag_section, msg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,3 +37,27 @@ fn time_segment() -> String {
|
||||||
let result = now.format("[%Y.%m.%d-%H:%M:%S]").to_string();
|
let result = now.format("[%Y.%m.%d-%H:%M:%S]").to_string();
|
||||||
result
|
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]
|
#[tokio::main]
|
||||||
pub async fn main() {
|
pub async fn main() {
|
||||||
println!("Hello, harmony!");
|
|
||||||
lib::harsh::main().await;
|
lib::harsh::main().await;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue