Skip to content

Commit

Permalink
Split package to workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
cofob committed Aug 2, 2024
1 parent fdf997e commit 89d64c0
Show file tree
Hide file tree
Showing 32 changed files with 498 additions and 314 deletions.
577 changes: 353 additions & 224 deletions Cargo.lock

Large diffs are not rendered by default.

40 changes: 3 additions & 37 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,40 +1,6 @@
[package]
name = "fastside"
description = "A smart redirecting gateway for various frontend services."
version = "0.2.0"
edition = "2021"

[dependencies]
actix-web = { version = "4.5.1", features = [
"macros",
"cookies",
], default-features = false } # web framework
askama = "0.12.1" # templating engine
reqwest = { version = "0.12.4", default-features = false, features = [
"rustls-tls",
"http2",
"socks",
] } # http client

clap = { version = "4.5.4", features = ["derive"] } # cli
config = "0.14.0" # config
serde = { version = "1.0.201", features = ["derive"] } # serialization
serde_json = "1.0.117" # serialization
serde_qs = "0.13.0" # serialization
url = { version = "2.5.0", features = ["serde"] } # url
log = "0.4.21" # logging
pretty_env_logger = "0.5.0" # logging
anyhow = "1.0.83" # error
thiserror = "1.0.60" # error
tokio = { version = "1.37.0", features = ["full"] } # async
futures = "0.3.30" # async
num_cpus = "1.16.0" # get number of cpus
rand = "0.8.5" # random
chrono = "0.4.38" # datetime
time = "0.3.36" # time offsets
regex = "1.10.5" # regex
base64 = "0.22.1" # base64
urlencoding = "2.1.3" # url encoding
[workspace]
members = ["fastside", "fastside-actualizer", "fastside-shared"]
resolver = "2"

[profile.release]
overflow-checks = true
Expand Down
12 changes: 12 additions & 0 deletions fastside-actualizer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "fastside-actualizer"
version = "0.1.0"
edition = "2021"

[dependencies]
fastside-shared = { path = "../fastside-shared" }

reqwest = { version = "0.12.4", default-features = false, features = [
"rustls-tls",
"http2",
] } # http client
5 changes: 5 additions & 0 deletions fastside-actualizer/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// Fastside services.json actualizer.

fn main() {
println!("Hello, world!");
}
11 changes: 11 additions & 0 deletions fastside-shared/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "fastside-shared"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = { version = "1.0.201", features = ["derive"] } # serialization
serde_json = "1.0.117" # serialization
url = { version = "2.5.0", features = ["serde"] } # url
base64 = "0.22.1" # base64
thiserror = "1.0.60" # error
1 change: 1 addition & 0 deletions fastside-shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod serde_types;
33 changes: 13 additions & 20 deletions src/serde_types.rs → fastside-shared/src/serde_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use serde::{
de::{self, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};
use thiserror::Error;
use url::Url;

use crate::errors::RedirectError;

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Instance {
pub url: Url,
Expand All @@ -18,19 +17,12 @@ fn default_test_url() -> String {
"/".to_string()
}

pub struct CompiledRegexSearch {
pub regex: regex::Regex,
pub url: String,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct RegexSearch {
pub regex: String,
pub url: String,
}

pub type Regexes = HashMap<String, Vec<CompiledRegexSearch>>;

pub trait HttpCodeRanges {
fn is_allowed(&self, code: u16) -> bool;
}
Expand Down Expand Up @@ -212,18 +204,26 @@ pub struct UserConfig {
pub ignore_fallback_warning: bool,
}

#[derive(Error, Debug)]
pub enum UserConfigError {
#[error("serialization error: `{0}`")]
Serialization(#[from] serde_json::Error),
#[error("urlencode error: `{0}`")]
Base64Decode(#[from] base64::DecodeError),
}

impl UserConfig {
pub fn to_config_string(&self) -> Result<String, RedirectError> {
pub fn to_config_string(&self) -> Result<String, UserConfigError> {
use base64::prelude::*;
let json: String = serde_json::to_string(&self).map_err(RedirectError::Serialization)?;
let json: String = serde_json::to_string(&self).map_err(UserConfigError::Serialization)?;
Ok(BASE64_STANDARD.encode(json.as_bytes()))
}

pub fn from_config_string(data: &str) -> Result<Self, RedirectError> {
pub fn from_config_string(data: &str) -> Result<Self, UserConfigError> {
use base64::prelude::*;
let decoded = BASE64_STANDARD.decode(data.as_bytes())?;
let json = String::from_utf8(decoded).unwrap();
serde_json::from_str(&json).map_err(RedirectError::from)
serde_json::from_str(&json).map_err(UserConfigError::from)
}
}

Expand All @@ -234,10 +234,3 @@ pub struct StoredData {
#[serde(default)]
pub default_settings: UserConfig,
}

#[derive(Debug)]
pub struct LoadedData {
pub services: ServicesData,
pub proxies: ProxyData,
pub default_settings: UserConfig,
}
40 changes: 40 additions & 0 deletions fastside/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[package]
name = "fastside"
description = "A smart redirecting gateway for various frontend services."
version = "0.2.0"
edition = "2021"
default-run = "fastside"

[dependencies]
fastside-shared = { path = "../fastside-shared" }

actix-web = { version = "4.5.1", features = [
"macros",
"cookies",
], default-features = false } # web framework
askama = "0.12.1" # templating engine
reqwest = { version = "0.12.4", default-features = false, features = [
"rustls-tls",
"http2",
"socks",
] } # http client

clap = { version = "4.5.4", features = ["derive"] } # cli
config = "0.14.0" # config
serde = { version = "1.0.201", features = ["derive"] } # serialization
serde_json = "1.0.117" # serialization
serde_qs = "0.13.0" # serialization
url = { version = "2.5.0", features = ["serde"] } # url
log = "0.4.21" # logging
pretty_env_logger = "0.5.0" # logging
anyhow = "1.0.83" # error
thiserror = "1.0.60" # error
tokio = { version = "1.37.0", features = ["full"] } # async
futures = "0.3.30" # async
num_cpus = "1.16.0" # get number of cpus
rand = "0.8.5" # random
chrono = "0.4.38" # datetime
time = "0.3.36" # time offsets
regex = "1.10.5" # regex
base64 = "0.22.1" # base64
urlencoding = "2.1.3" # url encoding
File renamed without changes.
7 changes: 2 additions & 5 deletions src/crawler.rs → fastside/src/crawler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ use thiserror::Error;
use tokio::{sync::RwLock, time::sleep};
use url::Url;

use crate::{
config::CrawlerConfig,
serde_types::{HttpCodeRanges, Instance, LoadedData, Service},
utils::parallel::Parallelise,
};
use crate::{config::CrawlerConfig, types::LoadedData, utils::parallel::Parallelise};
use fastside_shared::serde_types::{HttpCodeRanges, Instance, Service};

fn default_headers() -> reqwest::header::HeaderMap {
let mut headers = reqwest::header::HeaderMap::new();
Expand Down
9 changes: 3 additions & 6 deletions src/errors.rs → fastside/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,10 @@ use crate::search::SearchError;
pub enum RedirectError {
#[error("search error: `{0}`")]
Search(#[from] SearchError),
#[error("serialization error: `{0}`")]
Serialization(#[from] serde_json::Error),
#[error("urlencode error: `{0}`")]
Base64Decode(#[from] base64::DecodeError),
#[error("url parse error: `{0}`")]
UrlParse(#[from] url::ParseError),
#[error("user config error: `{0}`")]
UserConfig(#[from] fastside_shared::serde_types::UserConfigError),
}

impl_template_error!(RedirectError,
Expand All @@ -79,9 +77,8 @@ impl_template_error!(RedirectError,
SearchError::ServiceNotFound => StatusCode::NOT_FOUND,
_ => StatusCode::INTERNAL_SERVER_ERROR
},
RedirectError::Serialization(_) => StatusCode::INTERNAL_SERVER_ERROR,
RedirectError::Base64Decode(_) => StatusCode::INTERNAL_SERVER_ERROR,
RedirectError::UrlParse(_) => StatusCode::INTERNAL_SERVER_ERROR,
RedirectError::UserConfig(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
);

Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions src/main.rs → fastside/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
/// Fastside API server.
mod config;
mod crawler;
mod errors;
mod filters;
mod log_setup;
mod routes;
mod search;
mod serde_types;
mod types;
mod utils;

use crate::crawler::Crawler;
use crate::serde_types::ServicesData;

use actix_web::{middleware::Logger, web, App, HttpServer};
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use config::load_config;
use crawler::Crawler;
use fastside_shared::serde_types::{ServicesData, StoredData};
use log_setup::configure_logging;
use regex::Regex;
use routes::main_scope;
use serde_types::{CompiledRegexSearch, LoadedData, StoredData};
use std::{
collections::HashMap,
net::{SocketAddr, SocketAddrV4},
Expand All @@ -27,6 +26,7 @@ use std::{
sync::Arc,
};
use thiserror::Error;
use types::{CompiledRegexSearch, LoadedData};

#[deny(unused_imports)]
#[deny(unused_variables)]
Expand Down
14 changes: 10 additions & 4 deletions src/routes/api.rs → fastside/src/routes/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use serde::{Deserialize, Serialize};
use crate::{
config::AppConfig,
crawler::Crawler,
errors::RedirectApiError,
serde_types::{LoadedData, Regexes, UserConfig},
errors::{RedirectApiError, RedirectError},
types::{LoadedData, Regexes},
};
use fastside_shared::serde_types::UserConfig;

pub fn scope(_config: &AppConfig) -> Scope {
web::scope("/api/v1")
Expand Down Expand Up @@ -55,7 +56,10 @@ async fn make_user_config_string(
user_config: web::Json<UserConfig>,
) -> actix_web::Result<impl Responder> {
Ok(web::Json(
user_config.to_config_string().map_err(RedirectApiError)?,
user_config
.to_config_string()
.map_err(RedirectError::from)
.map_err(RedirectApiError)?,
))
}

Expand All @@ -65,6 +69,8 @@ async fn parse_user_config_string(
user_config_string: web::Json<String>,
) -> actix_web::Result<impl Responder> {
Ok(web::Json(
UserConfig::from_config_string(&user_config_string).map_err(RedirectApiError)?,
UserConfig::from_config_string(&user_config_string)
.map_err(RedirectError::from)
.map_err(RedirectApiError)?,
))
}
25 changes: 16 additions & 9 deletions src/routes/config.rs → fastside/src/routes/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use actix_web::{cookie::Cookie, get, http::header::LOCATION, web, HttpRequest, R
use askama::Template;

use crate::{
config::AppConfig,
serde_types::{LoadedData, UserConfig},
config::AppConfig, errors::RedirectError, types::LoadedData,
utils::user_config::load_settings_cookie,
};
use fastside_shared::serde_types::UserConfig;

pub fn scope(_config: &AppConfig) -> Scope {
web::scope("/configure")
Expand All @@ -27,7 +27,9 @@ async fn configure_page(
let user_config = load_settings_cookie(&req, &loaded_data.default_settings);

let template = ConfigureTemplate {
current_config: &user_config.to_config_string()?,
current_config: &user_config
.to_config_string()
.map_err(RedirectError::from)?,
};

Ok(actix_web::HttpResponse::Ok()
Expand All @@ -38,12 +40,17 @@ async fn configure_page(
#[get("/save")]
async fn configure_save(req: HttpRequest) -> actix_web::Result<impl Responder> {
let query_string = req.query_string();
let user_config = UserConfig::from_config_string(query_string)?;
let cookie = Cookie::build("config", user_config.to_config_string()?)
.path("/")
.expires(time::OffsetDateTime::now_utc() + time::Duration::days(9999))
.max_age(time::Duration::days(9999))
.finish();
let user_config = UserConfig::from_config_string(query_string).map_err(RedirectError::from)?;
let cookie = Cookie::build(
"config",
user_config
.to_config_string()
.map_err(RedirectError::from)?,
)
.path("/")
.expires(time::OffsetDateTime::now_utc() + time::Duration::days(9999))
.max_age(time::Duration::days(9999))
.finish();
Ok(actix_web::HttpResponse::TemporaryRedirect()
.cookie(cookie)
.insert_header((LOCATION, "/configure?success"))
Expand Down
3 changes: 2 additions & 1 deletion src/routes/index.rs → fastside/src/routes/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use crate::{
errors::RedirectError,
filters,
search::SearchError,
serde_types::{LoadedData, ServicesData},
types::LoadedData,
};
use fastside_shared::serde_types::ServicesData;

use super::{api, config, redirect};

Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion src/routes/redirect.rs → fastside/src/routes/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ use crate::{
find_redirect_service_by_name, find_redirect_service_by_url, get_redirect_instance,
get_redirect_instances, SearchError,
},
serde_types::{LoadedData, Regexes, SelectMethod, Service, UserConfig},
types::{LoadedData, Regexes},
utils::user_config::load_settings_cookie,
};
use fastside_shared::serde_types::{SelectMethod, Service, UserConfig};

pub fn scope(_config: &AppConfig) -> Scope {
web::scope("")
Expand Down
3 changes: 2 additions & 1 deletion src/search.rs → fastside/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use tokio::sync::RwLockReadGuard;

use crate::{
crawler::{CrawledInstance, CrawledInstanceStatus, CrawledService, CrawledServices},
serde_types::{Regexes, SelectMethod, Service, ServicesData, UserConfig},
types::Regexes,
};
use fastside_shared::serde_types::{SelectMethod, Service, ServicesData, UserConfig};
use rand::seq::SliceRandom;
use thiserror::Error;

Expand Down
Loading

0 comments on commit 89d64c0

Please sign in to comment.