@@ 12,6 12,12 @@ The format is based on [Keep a Changelog][] and this project adheres to
Unreleased
==========
+Added
+-----
+
+- Log messages to the file when `LOG_DIR` environment variable is set.
+
+
0.1.2 -- 2020-08-18
===================
@@ 20,6 26,7 @@ Changed
- Set listening port to 80.
+
0.1.1 -- 2020-08-13
===================
@@ 28,6 35,7 @@ Fixed
- Listen on any, not on localhost, due to Docker deploy.
+
0.1.0 -- 2020-08-12
===================
@@ 1,10 1,14 @@
use chrono::Utc;
-use futures::{FutureExt, StreamExt};
+use futures::StreamExt;
use jsonwebtoken::{DecodingKey, Validation, Algorithm};
use serde::{Deserialize, Serialize};
use serde_json::Value;
-use std::env;
use std::collections::HashMap;
+use std::env;
+use std::fs::File;
+use std::fs::OpenOptions;
+use std::io::Write;
+use std::path::Path;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use tokio::sync::RwLock;
@@ 67,7 71,7 @@ async fn send_msg(msg: Message, clients: &Clients) {
};
let mut val = Validation::new(Algorithm::HS256);
val.validate_exp = false;
- let payload = if let Ok(p) = jsonwebtoken::decode::<Claims>(
+ let _payload = if let Ok(p) = jsonwebtoken::decode::<Claims>(
token,
&DecodingKey::from_secret(jwt_secret.as_ref()),
&val,
@@ 81,7 85,37 @@ async fn send_msg(msg: Message, clients: &Clients) {
}
msg["tim"] = serde_json::json!(Utc::now());
for (&_id, tx) in clients.read().await.iter() {
- tx.send(Ok(Message::text(msg.to_string())));
+ tx.send(Ok(Message::text(msg.to_string()))).expect("failed to send");
+ }
+ match env::var("LOG_DIR") {
+ Ok(dname) => {
+ let today = Utc::now().format("%Y-%m-%d").to_string();
+ let mut dname = dname;
+ dname.push_str("/");
+ dname.push_str(&today);
+ let path = Path::new(&dname);
+ let mut file = match OpenOptions::new()
+ .append(true)
+ .open(&path) {
+ Ok(f) => f,
+ Err(_) => {
+ let mut err_msg = String::from("can't create ");
+ err_msg.push_str(&dname);
+ File::create(&path).expect(&err_msg)
+ },
+ };
+ let mut to_write = msg;
+ match to_write.get("jwt") {
+ Some(_) => {
+ to_write["jwt"] = serde_json::json!("...");
+ },
+ None => {},
+ }
+ let mut to_write = to_write.to_string();
+ to_write.push_str("\n");
+ file.write_all(to_write.as_bytes()).expect("write failed");
+ },
+ Err(_) => {},
}
}