Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptronicek committed Aug 6, 2023
1 parent a6f07f3 commit dcb4e3b
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub(crate) mod db;
pub(crate) mod id;
pub mod structs;
pub mod log;
pub mod rate_limit;
pub mod redis;
pub mod log;
pub mod structs;
2 changes: 1 addition & 1 deletion src/utils/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ pub fn gen_id(length: usize) -> String {
}

code
}
}
6 changes: 3 additions & 3 deletions src/utils/log.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::env;

extern crate fern;
extern crate chrono;
extern crate fern;

pub fn setup_logger() -> Result<String, fern::InitError> {
let temp_dir = env::temp_dir();
let log_file = temp_dir.join("server.log");
let log_file_path = log_file.as_path();
fern::Dispatch::new()
fern::Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(
"{}[{}][{}] {}",
Expand All @@ -22,4 +22,4 @@ pub fn setup_logger() -> Result<String, fern::InitError> {
.chain(fern::log_file(log_file_path)?)
.apply()?;
Ok(log_file_path.display().to_string())
}
}
11 changes: 4 additions & 7 deletions src/utils/rate_limit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rocket::request::{self, FromRequest, Outcome};

use std::sync::atomic::{Ordering, AtomicU32};
use std::sync::atomic::{AtomicU32, Ordering};

extern crate serde;
extern crate serde_json;
Expand All @@ -17,7 +17,7 @@ pub struct RateLimiter {
}

impl RateLimiter {
/// Create a new RateLimiter
/// Create a new RateLimiter
pub fn new() -> Self {
RateLimiter {
requests: Arc::new(AtomicU32::new(0)),
Expand Down Expand Up @@ -54,13 +54,10 @@ impl<'r> FromRequest<'r> for RateLimiter {
.state::<RateLimiter>()
.expect("RateLimiter registered as state");

if rate_limiter
.should_limit(Duration::from_secs(10), 15)
.await
{
if rate_limiter.should_limit(Duration::from_secs(10), 15).await {
Outcome::Failure((rocket::http::Status::TooManyRequests, ()))
} else {
Outcome::Success(rate_limiter.clone())
}
}
}
}
2 changes: 1 addition & 1 deletion src/utils/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ pub enum APIStatus {
Success,
#[serde(rename = "error")]
Error,
}
}

0 comments on commit dcb4e3b

Please sign in to comment.