From 3a6982e7b2aa8057f69633a01d69608c4392bfa0 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 2 Sep 2020 18:17:35 -0500 Subject: [PATCH] Adding rate_limiter and api_structs. --- docker/dev/Dockerfile | 10 +++++++++- docker/prod/Dockerfile | 2 ++ server/Cargo.toml | 1 + server/lemmy_api_structs/Cargo.toml | 4 ++++ server/lemmy_rate_limit/Cargo.toml | 4 ++++ server/lemmy_rate_limit/src/lib.rs | 12 +++++++----- server/lemmy_rate_limit/src/rate_limiter.rs | 3 +-- server/lemmy_utils/src/lib.rs | 12 ++++++------ 8 files changed, 34 insertions(+), 14 deletions(-) diff --git a/docker/dev/Dockerfile b/docker/dev/Dockerfile index 9bd74da07..dce2dc5b8 100644 --- a/docker/dev/Dockerfile +++ b/docker/dev/Dockerfile @@ -20,12 +20,18 @@ RUN USER=root cargo new server # Install cargo-build-deps RUN cargo install --git https://github.com/romac/cargo-build-deps.git WORKDIR /app/server -RUN mkdir -p lemmy_db/src/ lemmy_utils/src/ +RUN mkdir -p lemmy_db/src/ \ + lemmy_utils/src/ \ + lemmy_api_structs/src/ \ + lemmy_rate_limit/src/ \ + lemmy # Copy the cargo tomls COPY server/Cargo.toml server/Cargo.lock ./ COPY server/lemmy_db/Cargo.toml ./lemmy_db/ COPY server/lemmy_utils/Cargo.toml ./lemmy_utils/ +COPY server/lemmy_api_structs/Cargo.toml ./lemmy_api_structs/ +COPY server/lemmy_rate_limit/Cargo.toml ./lemmy_rate_limit/ # Cache the deps RUN cargo build-deps @@ -34,6 +40,8 @@ RUN cargo build-deps COPY server/src ./src/ COPY server/lemmy_db/src ./lemmy_db/src/ COPY server/lemmy_utils/src/ ./lemmy_utils/src/ +COPY server/lemmy_api_structs/src/ ./lemmy_api_structs/src/ +COPY server/lemmy_rate_limit/src/ ./lemmy_rate_limit/src/ COPY server/migrations ./migrations/ # Build for debug diff --git a/docker/prod/Dockerfile b/docker/prod/Dockerfile index 845df88de..9bccb4489 100644 --- a/docker/prod/Dockerfile +++ b/docker/prod/Dockerfile @@ -13,6 +13,8 @@ WORKDIR /app/server COPY server/Cargo.toml server/Cargo.lock ./ COPY server/lemmy_db ./lemmy_db COPY server/lemmy_utils ./lemmy_utils +COPY server/lemmy_api_structs ./lemmy_api_structs +COPY server/lemmy_rate_limit ./lemmy_rate_limit RUN mkdir -p ./src/bin \ && echo 'fn main() { println!("Dummy") }' > ./src/bin/main.rs RUN cargo build --release diff --git a/server/Cargo.toml b/server/Cargo.toml index 912d130d4..e12d8c61a 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -11,6 +11,7 @@ members = [ "lemmy_utils", "lemmy_db", "lemmy_api_structs", + "lemmy_rate_limit", ] [dependencies] diff --git a/server/lemmy_api_structs/Cargo.toml b/server/lemmy_api_structs/Cargo.toml index d1695eca6..93653fba2 100644 --- a/server/lemmy_api_structs/Cargo.toml +++ b/server/lemmy_api_structs/Cargo.toml @@ -4,6 +4,10 @@ version = "0.1.0" authors = ["Felix Ableitner "] edition = "2018" +[lib] +name = "lemmy_api_structs" +path = "src/lib.rs" + [dependencies] lemmy_db = { path = "../lemmy_db" } serde = { version = "1.0.105", features = ["derive"] } diff --git a/server/lemmy_rate_limit/Cargo.toml b/server/lemmy_rate_limit/Cargo.toml index 4fc65254d..001297fcc 100644 --- a/server/lemmy_rate_limit/Cargo.toml +++ b/server/lemmy_rate_limit/Cargo.toml @@ -4,6 +4,10 @@ version = "0.1.0" authors = ["Felix Ableitner "] edition = "2018" +[lib] +name = "lemmy_rate_limit" +path = "src/lib.rs" + [dependencies] lemmy_utils = { path = "../lemmy_utils" } lemmy_api_structs = { path = "../lemmy_api_structs" } diff --git a/server/lemmy_rate_limit/src/lib.rs b/server/lemmy_rate_limit/src/lib.rs index 431a2d760..2d3553f01 100644 --- a/server/lemmy_rate_limit/src/lib.rs +++ b/server/lemmy_rate_limit/src/lib.rs @@ -1,13 +1,17 @@ #[macro_use] pub extern crate strum_macros; -pub extern crate tokio; -pub extern crate futures; pub extern crate actix_web; +pub extern crate futures; pub extern crate log; +pub extern crate tokio; use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform}; use futures::future::{ok, Ready}; -use lemmy_utils::settings::{RateLimitConfig, Settings}; +use lemmy_utils::{ + get_ip, + settings::{RateLimitConfig, Settings}, + LemmyError, +}; use rate_limiter::{RateLimitType, RateLimiter}; use std::{ future::Future, @@ -16,8 +20,6 @@ use std::{ task::{Context, Poll}, }; use tokio::sync::Mutex; -use lemmy_utils::get_ip; -use lemmy_utils::LemmyError; pub mod rate_limiter; diff --git a/server/lemmy_rate_limit/src/rate_limiter.rs b/server/lemmy_rate_limit/src/rate_limiter.rs index 1c90f90e0..b48547290 100644 --- a/server/lemmy_rate_limit/src/rate_limiter.rs +++ b/server/lemmy_rate_limit/src/rate_limiter.rs @@ -1,6 +1,5 @@ -use lemmy_utils::IPAddr; -use lemmy_utils::LemmyError; use lemmy_api_structs::APIError; +use lemmy_utils::{IPAddr, LemmyError}; use log::debug; use std::{collections::HashMap, time::SystemTime}; use strum::IntoEnumIterator; diff --git a/server/lemmy_utils/src/lib.rs b/server/lemmy_utils/src/lib.rs index 97f32f27f..205093433 100644 --- a/server/lemmy_utils/src/lib.rs +++ b/server/lemmy_utils/src/lib.rs @@ -1,5 +1,7 @@ #[macro_use] pub extern crate lazy_static; +pub extern crate actix_web; +pub extern crate anyhow; pub extern crate comrak; pub extern crate lettre; pub extern crate lettre_email; @@ -8,12 +10,11 @@ pub extern crate rand; pub extern crate regex; pub extern crate serde_json; pub extern crate url; -pub extern crate actix_web; -pub extern crate anyhow; pub mod settings; use crate::settings::Settings; +use actix_web::dev::ConnectionInfo; use chrono::{DateTime, FixedOffset, Local, NaiveDateTime}; use itertools::Itertools; use lettre::{ @@ -32,7 +33,6 @@ use rand::{distributions::Alphanumeric, thread_rng, Rng}; use regex::{Regex, RegexBuilder}; use std::io::{Error, ErrorKind}; use url::Url; -use actix_web::dev::ConnectionInfo; pub type ConnectionId = usize; pub type PostId = i32; @@ -58,8 +58,8 @@ pub struct LemmyError { } impl From for LemmyError - where - T: Into, +where + T: Into, { fn from(t: T) -> Self { LemmyError { inner: t.into() } @@ -386,4 +386,4 @@ pub fn get_ip(conn_info: &ConnectionInfo) -> String { .next() .unwrap_or("127.0.0.1") .to_string() -} \ No newline at end of file +}