Skip to content
Snippets Groups Projects
Commit 7556e7a2 authored by DeathwingTheBoss's avatar DeathwingTheBoss
Browse files

- Add Timestamp to logs.

- Write logs to a file called `drone.log` instead of printing it.
- Version bump to 0.1.2
parent 6f53e360
No related branches found
No related tags found
No related merge requests found
...@@ -406,9 +406,10 @@ dependencies = [ ...@@ -406,9 +406,10 @@ dependencies = [
[[package]] [[package]]
name = "drone" name = "drone"
version = "0.1.1" version = "0.1.2"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"humantime",
"lru_time_cache", "lru_time_cache",
"reqwest", "reqwest",
"serde", "serde",
...@@ -637,6 +638,12 @@ version = "1.0.2" ...@@ -637,6 +638,12 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]] [[package]]
name = "hyper" name = "hyper"
version = "0.14.25" version = "0.14.25"
......
[package] [package]
name = "drone" name = "drone"
version = "0.1.1" version = "0.1.2"
edition = "2021" edition = "2021"
authors = ["Deathwing <hi@deathwing.me>"] authors = ["Deathwing <hi@deathwing.me>"]
description = "A caching reverse-proxy application for the Hive blockchain." description = "A caching reverse-proxy application for the Hive blockchain."
...@@ -13,3 +13,4 @@ reqwest = { version = "0.11.14", features = ["blocking", "json"] } ...@@ -13,3 +13,4 @@ reqwest = { version = "0.11.14", features = ["blocking", "json"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.94" serde_json = "1.0.94"
lru_time_cache = "0.11.11" lru_time_cache = "0.11.11"
humantime = "2.1.0"
Timestamp: 2023-03-19T16:41:20Z || IP: 127.0.0.1 || HTTP Version: HTTP/1.1 || Request Method: condenser_api.get_block || Request Params: ["48784515"]
Timestamp: 2023-03-19T16:41:21Z || IP: 127.0.0.1 || HTTP Version: HTTP/1.1 || Request Method: condenser_api.get_block || Request Params: ["48784515"]
Timestamp: 2023-03-19T16:43:19Z || IP: 127.0.0.1 || HTTP Version: HTTP/1.1 || Request Method: condenser_api.get_block || Request Params: ["48784515"]
Timestamp: 2023-03-19T16:43:19Z || IP: 127.0.0.1 || HTTP Version: HTTP/1.1 || Request Method: condenser_api.get_block || Request Params: ["48784515"]
use std::{time::Duration, sync::Mutex}; use std::{time::Duration, sync::Mutex, fs::{File, OpenOptions}};
use actix_web::{web, App, HttpResponse, HttpServer, Responder, HttpRequest}; use actix_web::{web, App, HttpResponse, HttpServer, Responder, HttpRequest};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use reqwest::{Client, ClientBuilder}; use reqwest::{Client, ClientBuilder};
use lru_time_cache::LruCache; use lru_time_cache::LruCache;
use serde_json::Value; use serde_json::Value;
use std::io::Write;
// Drone Configuration Parameters // Drone Configuration Parameters
const DRONE_VERSION: &str = env!("CARGO_PKG_VERSION"); const DRONE_VERSION: &str = env!("CARGO_PKG_VERSION");
...@@ -92,6 +93,9 @@ async fn api_call(req: HttpRequest, call: web::Json<APICall>, data: web::Data<AP ...@@ -92,6 +93,9 @@ async fn api_call(req: HttpRequest, call: web::Json<APICall>, data: web::Data<AP
params: call.params, params: call.params,
}; };
// Get humantime for logging.
let human_timestamp = humantime::format_rfc3339_seconds(std::time::SystemTime::now());
// Log the request, if there's Cloudflare header (CF-Connecting-IP) use that instead of peer_addr. // Log the request, if there's Cloudflare header (CF-Connecting-IP) use that instead of peer_addr.
let get_cloudflare_ip = req.headers().get("CF-Connecting-IP"); let get_cloudflare_ip = req.headers().get("CF-Connecting-IP");
let client_ip = match get_cloudflare_ip { let client_ip = match get_cloudflare_ip {
...@@ -99,8 +103,9 @@ async fn api_call(req: HttpRequest, call: web::Json<APICall>, data: web::Data<AP ...@@ -99,8 +103,9 @@ async fn api_call(req: HttpRequest, call: web::Json<APICall>, data: web::Data<AP
None => req.peer_addr().unwrap().ip().to_string(), None => req.peer_addr().unwrap().ip().to_string(),
}; };
let formatted_log = let formatted_log =
format!("IP: {} || HTTP Version: {:?} || Request Method: {} || Request Params: {}", client_ip, req.version(), json_rpc_call.method, json_rpc_call.params); format!("Timestamp: {} || IP: {} || HTTP Version: {:?} || Request Method: {} || Request Params: {}", human_timestamp, client_ip, req.version(), json_rpc_call.method, json_rpc_call.params);
println!("{}", formatted_log); let mut log_file = data.log_file.lock().unwrap();
writeln!(log_file, "{}", formatted_log).unwrap();
// Pick the endpoints depending on the method. // Pick the endpoints depending on the method.
let endpoints = match json_rpc_call.method.as_str() { let endpoints = match json_rpc_call.method.as_str() {
// HAF // HAF
...@@ -207,15 +212,23 @@ async fn api_call(req: HttpRequest, call: web::Json<APICall>, data: web::Data<AP ...@@ -207,15 +212,23 @@ async fn api_call(req: HttpRequest, call: web::Json<APICall>, data: web::Data<AP
struct APIFunctions { struct APIFunctions {
cache: Mutex<LruCache<String, Value>>, cache: Mutex<LruCache<String, Value>>,
log_file: Mutex<File>,
webclient: Client, webclient: Client,
} }
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
// Create log file if it doesn't exist, if it does, read it.
let logger = OpenOptions::new()
.create(true)
.append(true)
.open("drone.log")
.unwrap();
// Create the cache. // Create the cache.
let _cache = web::Data::new(APIFunctions { let _cache = web::Data::new(APIFunctions {
cache: Mutex::new(LruCache::<String, serde_json::Value>::with_expiry_duration_and_capacity(DRONE_CACHE_TTL, DRONE_CACHE_COUNT)), cache: Mutex::new(LruCache::<String, serde_json::Value>::with_expiry_duration_and_capacity(DRONE_CACHE_TTL, DRONE_CACHE_COUNT)),
log_file: Mutex::new(logger),
webclient: ClientBuilder::new() webclient: ClientBuilder::new()
.pool_max_idle_per_host(8) .pool_max_idle_per_host(8)
.build() .build()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment