added logging

This commit is contained in:
Piyush मिश्रः 2021-02-20 14:14:46 +05:30
parent 6a26ff18aa
commit 9c51a04f69
7 changed files with 44 additions and 161 deletions

43
Cargo.lock generated
View File

@ -677,6 +677,19 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "env_logger"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17392a012ea30ef05a610aa97dfb49496e71c9f676b27879922ea5bdf60d9d3f"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]] [[package]]
name = "flate2" name = "flate2"
version = "1.0.20" version = "1.0.20"
@ -935,6 +948,12 @@ version = "1.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "615caabe2c3160b313d52ccc905335f4ed5f10881dd63dc5699d47e90be85691" checksum = "615caabe2c3160b313d52ccc905335f4ed5f10881dd63dc5699d47e90be85691"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]] [[package]]
name = "idna" name = "idna"
version = "0.2.0" version = "0.2.0"
@ -1063,11 +1082,11 @@ dependencies = [
"actix-web", "actix-web",
"actix-web-actors", "actix-web-actors",
"clap", "clap",
"env_logger",
"futures", "futures",
"rand 0.8.3", "rand 0.8.3",
"serde", "serde",
"serde_json", "serde_json",
"vecmap",
] ]
[[package]] [[package]]
@ -1668,6 +1687,15 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "termcolor"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "textwrap" name = "textwrap"
version = "0.11.0" version = "0.11.0"
@ -1976,10 +2004,6 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "vecmap"
version = "0.1.0"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.1.5" version = "0.1.5"
@ -2092,6 +2116,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi 0.3.9",
]
[[package]] [[package]]
name = "winapi-x86_64-pc-windows-gnu" name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"

View File

@ -13,10 +13,10 @@ actix-web = "3"
actix-web-actors = "3" actix-web-actors = "3"
actix-broker = "0.3.1" actix-broker = "0.3.1"
actix-files = "0.5.0" actix-files = "0.5.0"
env_logger = "0.8.3"
clap = "2.33.3" clap = "2.33.3"
serde = "1.0.123" serde = "1.0.123"
serde_json = "1.0.62" serde_json = "1.0.62"
rand = "0.8.3" rand = "0.8.3"
futures = "0.3.12" futures = "0.3.12"
vecmap = { path = "vecmap" }

View File

@ -9,7 +9,7 @@
//! |--> ws_sansad4 <----/ //! |--> ws_sansad4 <----/
//! //!
use actix_web::{App, Error, HttpRequest, HttpResponse, HttpServer, web}; use actix_web::{App, Error, HttpRequest, HttpResponse, HttpServer, middleware::Logger, web};
use actix_files as fs; use actix_files as fs;
use actix_web_actors::ws; use actix_web_actors::ws;
use ws_sansad::WsSansad; use ws_sansad::WsSansad;
@ -22,10 +22,14 @@ mod chat_pinnd;
mod validator; mod validator;
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
let config = config::Config::new(); let config = config::Config::new();
let static_path = config.static_path; let static_path = config.static_path;
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.wrap(Logger::new("%t [%a] %s %{User-Agent}i %r"))
.service(web::resource("/ws/").route(web::get().to(ws_index))) .service(web::resource("/ws/").route(web::get().to(ws_index)))
.service(fs::Files::new("/", &static_path).index_file("index.html")) .service(fs::Files::new("/", &static_path).index_file("index.html"))
}) })

5
vecmap/Cargo.lock generated
View File

@ -1,5 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "vecmap"
version = "0.1.0"

View File

@ -1,9 +0,0 @@
[package]
name = "vecmap"
version = "0.1.0"
authors = ["piyush"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -1,24 +0,0 @@
use std::fmt::{Display, Formatter, Result};
use std::error::Error;
#[derive(Debug)]
pub struct ElementNotFount;
#[derive(Debug)]
pub struct KeyAlreadyExist;
impl Display for ElementNotFount {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "Element not found")
}
}
impl Error for ElementNotFount {}
impl Display for KeyAlreadyExist {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "Key already do exist")
}
}
impl Error for KeyAlreadyExist {}

View File

@ -1,116 +0,0 @@
use std::error::Error;
pub mod errors;
#[derive(Debug, Clone)]
pub struct VecMap<K: Clone + PartialEq,V: Clone>(Vec<VecMapElement<K,V>>);
#[derive(Debug, Clone)]
pub struct VecMapElement<K: PartialEq,V: Clone> {
key: K,
value: V
}
impl<K: Clone + PartialEq, V: Clone> IntoIterator for VecMap<K, V> {
type IntoIter = std::vec::IntoIter<VecMapElement<K,V>>;
type Item = VecMapElement<K, V>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<K: Clone + PartialEq,V: Clone> VecMap<K,V> {
pub fn new() -> VecMap<K,V> {
VecMap(Vec::new())
}
pub fn insert(&mut self, key: K, value: V) {
let key_tmp = key.clone();
match self.0.iter_mut().find(move |a| a.key == key_tmp) {
Some(i) => {
i.value = value.clone();
}
None => {
self.0.push(VecMapElement {
key,
value
});
}
}
}
pub fn get(&self, key: &K) -> Option<&V> {
match self.0.iter().find(|a| &a.key == key) {
Some(v) => Some(&v.value),
None => None
}
}
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
match self.0.iter_mut().find(move |a| &a.key == key) {
Some(v) => Some(&mut v.value),
None => None
}
}
pub fn remove(&mut self, key: &K) -> Result<(), errors::ElementNotFount> {
match self.0.iter().position(move |a| &a.key == key) {
Some(i) => {
self.0.remove(i);
}
None => {
return Err(errors::ElementNotFount);
}
}
Ok(())
}
pub fn change_key(&mut self, key: &K, new_key: &K) -> Result<(), &dyn Error> {
let key_tmp = key.clone();
if let Some(_) = self.0.iter().position(move |a| a.key == key_tmp) {
return Err(&errors::KeyAlreadyExist);
}
match self.0.iter_mut().find(move |a| &a.key == key) {
Some(i) => {
i.key = new_key.to_owned();
}
None => {
return Err(&errors::ElementNotFount);
}
}
Ok(())
}
pub fn key_exist(&self, key: &K) -> bool {
match self.0.iter().position(move |a| &a.key == key) {
Some(_) => true,
None => false
}
}
pub fn len(&self) -> usize {
self.0.len()
}
}
#[cfg(test)]
mod tests {
use crate::VecMap;
#[test]
fn it_works() {
let mut a: VecMap<i32, i32> = super::VecMap::new();
a.insert(1, 5);
a.insert(2, 10);
a.remove(&1);
a.into_iter().enumerate().for_each(|(i,e)| {
println!("{}\t-> {:?}", i,e);
});
}
}