Skip to content

Commit

Permalink
Add an actual logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Tricked-dev committed Sep 11, 2021
1 parent 9274a5c commit 7cb7249
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 104 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
node_modules
.idea/
lowestbins.json
debug.log
info.log
153 changes: 65 additions & 88 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 3 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ serde_json = "1.0"
hyper = { version = "0.14", features = ["full"] }
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["full"] }
serde_derive = "1.0.130"
pretty_env_logger = "0.4"
tokio-util = "0.6.8"
reqwest = { version="0.11.4", features = ["json", "gzip", "brotli"] }
base64 = {version="0.13.0" }
hematite-nbt = { version="0.5.2" }
nbtrs = {git="https://github.com/overviewer/nbtrs"}
flate2 = "1"
lazy_static = "1.4.0"


log="0.4.14"
simplelog = "^0.10.0"
substring="1.4.5"
15 changes: 9 additions & 6 deletions src/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
use crate::bazaar::get as get_bazaar;
use crate::util::{get, parse_hypixel};
use log::{debug, info};
use std::collections::HashMap;
use std::fs;
use std::time::Instant;

pub async fn fetch_auctions() {
info!("fetching auctions");
let started = Instant::now();

let mut auctions: HashMap<String, i64> = HashMap::new();

let r = get(1).await;
auctions = parse_hypixel(r.auctions, auctions);
for a in 2..r.total_pages {
println!("------------------------ req: {}", a);
debug!("------------------------ req: {}", a);
let now = Instant::now();
let r = get(a).await;
println!(": request took {} miliseconds", now.elapsed().as_millis());
debug!(": request took {} miliseconds", now.elapsed().as_millis());
let nowss = Instant::now();
auctions = parse_hypixel(r.auctions, auctions);
println!("$ parsing took {} miliseconds", nowss.elapsed().as_millis());
println!(
debug!("$ parsing took {} miliseconds", nowss.elapsed().as_millis());
debug!(
"! request and parsing took {} miliseconds",
now.elapsed().as_millis()
);
}
info!("fetching bazaar");
let r = get_bazaar().await;
let prods = r.products;
for (key, val) in prods.iter() {
auctions.insert(key.to_string(), val.quick_status.buy_price.round() as i64);
}

let xs = serde_json::to_string(&auctions).unwrap();
println!("writing file");
println!("!! Total time taken {}", started.elapsed().as_secs());
debug!("writing file");
info!("!! Total time taken {}", started.elapsed().as_secs());
fs::write("lowestbins.json", xs).unwrap();
}
16 changes: 16 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
use lowestbins::fetch::fetch_auctions;
use lowestbins::server::start_server;
use lowestbins::util::set_interval;
use simplelog::*;
use std::fs::File;
use tokio::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("creating logger");
CombinedLogger::init(vec![
WriteLogger::new(
LevelFilter::Info,
Config::default(),
File::create("info.log").unwrap(),
),
WriteLogger::new(
LevelFilter::Debug,
Config::default(),
File::create("debug.log").unwrap(),
),
])
.unwrap();
fetch_auctions().await;

set_interval(
Expand Down
9 changes: 5 additions & 4 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
use hyper::service::{make_service_fn, service_fn};
use hyper::{header, Body, Method, Request, Response, Result, Server};
use log::{error, info};
use substring::Substring;
use tokio::fs::File;
use tokio_util::codec::{BytesCodec, FramedRead};

static INDEX: &str = "lowestbins.json";
static NOTFOUND: &[u8] = b"Not Found";

pub async fn start_server() {
pretty_env_logger::init();

let addr = "127.0.0.1:1337".parse().unwrap();

let make_service =
make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(response_examples)) });

let server = Server::bind(&addr).serve(make_service);

info!("Listening on http://{}", addr);
println!("Listening on http://{}", addr);

if let Err(e) = server.await {
eprintln!("server error: {}", e);
error!("server error: {}", e);
}
}

async fn response_examples(req: Request<Body>) -> Result<Response<Body>> {
println!("{} {}", req.method(), req.uri().path());
info!("{} {}", req.method(), req.uri().path().substring(0, 30));

match (req.method(), req.uri().path()) {
(&Method::GET, "/lowestbins.json") | (&Method::GET, "/lowestbins") => {
Expand Down

0 comments on commit 7cb7249

Please sign in to comment.