diff --git a/src/bin/ayb.rs b/src/bin/ayb.rs index 2ea2c84a..c044148a 100644 --- a/src/bin/ayb.rs +++ b/src/bin/ayb.rs @@ -2,9 +2,9 @@ use ayb::ayb_db::models::{DBType, EntityType}; use ayb::client::config::ClientConfig; use ayb::client::http::AybClient; use ayb::formatting::TabularFormatter; -use ayb::http::config::{config_to_toml, default_server_config}; -use ayb::http::server::run_server; use ayb::http::structs::{EntityDatabasePath, ProfileLinkUpdate}; +use ayb::server::config::{config_to_toml, default_server_config}; +use ayb::server::server_runner::run_server; use clap::builder::ValueParser; use clap::{arg, command, value_parser, Command, ValueEnum}; use directories::ProjectDirs; diff --git a/src/email/mod.rs b/src/email/mod.rs index 8816fbe1..d113fb4c 100644 --- a/src/email/mod.rs +++ b/src/email/mod.rs @@ -1,7 +1,7 @@ use crate::email::templating::render_confirmation_template; use crate::error::AybError; -use crate::http::structs::AybConfigEmail; -use crate::http::web_frontend::WebFrontendDetails; +use crate::server::config::AybConfigEmail; +use crate::server::web_frontend::WebFrontendDetails; use lettre::{ message::header::ContentType, transport::smtp::authentication::Credentials, diff --git a/src/email/templating.rs b/src/email/templating.rs index 66cf4c1f..e81659cf 100644 --- a/src/email/templating.rs +++ b/src/email/templating.rs @@ -1,4 +1,4 @@ -use crate::http::web_frontend::WebFrontendDetails; +use crate::server::web_frontend::WebFrontendDetails; use crate::templating::TemplateString; pub fn render_confirmation_template( diff --git a/src/hosted_db.rs b/src/hosted_db.rs index b78a8ba1..eb8ca4ca 100644 --- a/src/hosted_db.rs +++ b/src/hosted_db.rs @@ -6,7 +6,7 @@ use crate::ayb_db::models::DBType; use crate::error::AybError; use crate::formatting::TabularFormatter; use crate::hosted_db::sqlite::potentially_isolated_sqlite_query; -use crate::http::structs::AybConfigIsolation; +use crate::server::config::AybConfigIsolation; use prettytable::{Cell, Row, Table}; use serde::{Deserialize, Serialize}; use std::path::PathBuf; diff --git a/src/hosted_db/sqlite.rs b/src/hosted_db/sqlite.rs index c2165f45..78402ef3 100644 --- a/src/hosted_db/sqlite.rs +++ b/src/hosted_db/sqlite.rs @@ -1,6 +1,6 @@ use crate::error::AybError; use crate::hosted_db::{sandbox::run_in_sandbox, QueryResult}; -use crate::http::structs::AybConfigIsolation; +use crate::server::config::AybConfigIsolation; use rusqlite::config::DbConfig; use rusqlite::limits::Limit; use rusqlite::types::ValueRef; diff --git a/src/http.rs b/src/http.rs index e5ac8209..21dab618 100644 --- a/src/http.rs +++ b/src/http.rs @@ -1,9 +1 @@ -pub mod config; -pub mod endpoints; -pub mod permissions; -pub mod server; pub mod structs; -pub mod tokens; -pub mod url_verification; -pub mod utils; -pub mod web_frontend; diff --git a/src/http/structs.rs b/src/http/structs.rs index 42317fda..0673b98b 100644 --- a/src/http/structs.rs +++ b/src/http/structs.rs @@ -5,58 +5,6 @@ use crate::ayb_db::models::{ use crate::formatting::TabularFormatter; use prettytable::{Cell, Row, Table}; use serde::{Deserialize, Serialize}; -use url::Url; - -#[derive(Clone, Serialize, Deserialize)] -pub struct AybConfigWeb { - pub info_url: Url, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct AybConfigCors { - pub origin: String, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct AybConfigAuthentication { - pub fernet_key: String, - pub token_expiration_seconds: u64, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct AybConfigEmail { - pub from: String, - pub reply_to: String, - pub smtp_host: String, - pub smtp_port: u16, - pub smtp_username: String, - pub smtp_password: String, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct AybConfigIsolation { - pub nsjail_path: String, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct AybConfig { - pub host: String, - pub port: u16, - pub database_url: String, - pub data_path: String, - pub e2e_testing: Option, - pub authentication: AybConfigAuthentication, - pub email: AybConfigEmail, - pub web: Option, - pub cors: AybConfigCors, - pub isolation: Option, -} - -impl AybConfig { - pub fn e2e_testing_on(&self) -> bool { - self.e2e_testing.unwrap_or(false) - } -} #[derive(Serialize, Deserialize, Debug)] pub struct Database { diff --git a/src/lib.rs b/src/lib.rs index 573e6852..2af91013 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,4 +9,5 @@ pub mod error; pub mod formatting; pub mod hosted_db; pub mod http; +pub mod server; pub mod templating; diff --git a/src/server.rs b/src/server.rs new file mode 100644 index 00000000..01f4f9dd --- /dev/null +++ b/src/server.rs @@ -0,0 +1,8 @@ +pub mod config; +pub mod endpoints; +pub mod permissions; +pub mod server_runner; +pub mod tokens; +pub mod url_verification; +pub mod utils; +pub mod web_frontend; diff --git a/src/http/config.rs b/src/server/config.rs similarity index 51% rename from src/http/config.rs rename to src/server/config.rs index a8f6bc74..9430033d 100644 --- a/src/http/config.rs +++ b/src/server/config.rs @@ -1,10 +1,62 @@ use fernet; +use serde::{Deserialize, Serialize}; use std::fs; use std::path::PathBuf; use toml; +use url::Url; use crate::error::AybError; -use crate::http::structs::{AybConfig, AybConfigAuthentication, AybConfigCors, AybConfigEmail}; + +#[derive(Clone, Serialize, Deserialize)] +pub struct AybConfigWeb { + pub info_url: Url, +} + +#[derive(Clone, Serialize, Deserialize)] +pub struct AybConfigCors { + pub origin: String, +} + +#[derive(Clone, Serialize, Deserialize)] +pub struct AybConfigAuthentication { + pub fernet_key: String, + pub token_expiration_seconds: u64, +} + +#[derive(Clone, Serialize, Deserialize)] +pub struct AybConfigEmail { + pub from: String, + pub reply_to: String, + pub smtp_host: String, + pub smtp_port: u16, + pub smtp_username: String, + pub smtp_password: String, +} + +#[derive(Clone, Serialize, Deserialize)] +pub struct AybConfigIsolation { + pub nsjail_path: String, +} + +#[derive(Clone, Serialize, Deserialize)] +pub struct AybConfig { + pub host: String, + pub port: u16, + pub database_url: String, + pub data_path: String, + pub e2e_testing: Option, + pub authentication: AybConfigAuthentication, + pub email: AybConfigEmail, + pub web: Option, + pub cors: AybConfigCors, + pub isolation: Option, +} + +impl AybConfig { + pub fn e2e_testing_on(&self) -> bool { + self.e2e_testing.unwrap_or(false) + } +} pub fn config_to_toml(ayb_config: AybConfig) -> Result { Ok(toml::to_string(&ayb_config)?) diff --git a/src/http/endpoints/confirm.rs b/src/server/endpoints/confirm.rs similarity index 93% rename from src/http/endpoints/confirm.rs rename to src/server/endpoints/confirm.rs index 851f97bb..bdc1f9ca 100644 --- a/src/http/endpoints/confirm.rs +++ b/src/server/endpoints/confirm.rs @@ -4,9 +4,10 @@ use crate::ayb_db::models::{ InstantiatedAuthenticationMethod, }; use crate::error::AybError; -use crate::http::structs::{APIToken as APIAPIToken, AybConfig}; -use crate::http::tokens::{decrypt_auth_token, generate_api_token}; -use crate::http::utils::get_header; +use crate::http::structs::APIToken as APIAPIToken; +use crate::server::config::AybConfig; +use crate::server::tokens::{decrypt_auth_token, generate_api_token}; +use crate::server::utils::get_header; use actix_web::{post, web, HttpRequest, HttpResponse}; #[post("/v1/confirm")] diff --git a/src/http/endpoints/create_database.rs b/src/server/endpoints/create_database.rs similarity index 87% rename from src/http/endpoints/create_database.rs rename to src/server/endpoints/create_database.rs index f0d03d33..6b18829f 100644 --- a/src/http/endpoints/create_database.rs +++ b/src/server/endpoints/create_database.rs @@ -5,9 +5,10 @@ use std::str::FromStr; use crate::error::AybError; use crate::hosted_db::paths::database_path; -use crate::http::permissions::can_create_database; -use crate::http::structs::{AybConfig, Database as APIDatabase, EntityDatabasePath}; -use crate::http::utils::{get_header, unwrap_authenticated_entity}; +use crate::http::structs::{Database as APIDatabase, EntityDatabasePath}; +use crate::server::config::AybConfig; +use crate::server::permissions::can_create_database; +use crate::server::utils::{get_header, unwrap_authenticated_entity}; use actix_web::{post, web, HttpRequest, HttpResponse}; #[post("/v1/{entity}/{database}/create")] diff --git a/src/http/endpoints/entity_details.rs b/src/server/endpoints/entity_details.rs similarity index 94% rename from src/http/endpoints/entity_details.rs rename to src/server/endpoints/entity_details.rs index d31c863b..e308a1ae 100644 --- a/src/http/endpoints/entity_details.rs +++ b/src/server/endpoints/entity_details.rs @@ -1,11 +1,11 @@ use crate::ayb_db::db_interfaces::AybDb; use crate::ayb_db::models::InstantiatedEntity; use crate::error::AybError; -use crate::http::permissions::can_query; use crate::http::structs::{ EntityDatabase, EntityPath, EntityProfile, EntityProfileLink, EntityQueryResponse, }; -use crate::http::utils::unwrap_authenticated_entity; +use crate::server::permissions::can_query; +use crate::server::utils::unwrap_authenticated_entity; use actix_web::{get, web}; #[get("/v1/entity/{entity}")] diff --git a/src/http/endpoints/log_in.rs b/src/server/endpoints/log_in.rs similarity index 88% rename from src/http/endpoints/log_in.rs rename to src/server/endpoints/log_in.rs index 1c15872e..ad12ab15 100644 --- a/src/http/endpoints/log_in.rs +++ b/src/server/endpoints/log_in.rs @@ -3,10 +3,11 @@ use crate::ayb_db::models::{AuthenticationMethodStatus, AuthenticationMethodType use crate::email::send_registration_email; use crate::error::AybError; -use crate::http::structs::{AuthenticationDetails, AybConfig, EmptyResponse}; -use crate::http::tokens::encrypt_auth_token; -use crate::http::utils::get_lowercased_header; -use crate::http::web_frontend::WebFrontendDetails; +use crate::http::structs::{AuthenticationDetails, EmptyResponse}; +use crate::server::config::AybConfig; +use crate::server::tokens::encrypt_auth_token; +use crate::server::utils::get_lowercased_header; +use crate::server::web_frontend::WebFrontendDetails; use actix_web::{post, web, HttpRequest, HttpResponse}; #[post("/v1/log_in")] diff --git a/src/http/endpoints/mod.rs b/src/server/endpoints/mod.rs similarity index 100% rename from src/http/endpoints/mod.rs rename to src/server/endpoints/mod.rs diff --git a/src/http/endpoints/query.rs b/src/server/endpoints/query.rs similarity index 88% rename from src/http/endpoints/query.rs rename to src/server/endpoints/query.rs index 1744d5d8..92ddcc1a 100644 --- a/src/http/endpoints/query.rs +++ b/src/server/endpoints/query.rs @@ -4,10 +4,10 @@ use crate::ayb_db::models::{DBType, InstantiatedEntity}; use crate::error::AybError; use crate::hosted_db::paths::database_path; use crate::hosted_db::{run_query, QueryResult}; -use crate::http::permissions::can_query; -use crate::http::structs::{AybConfig, EntityDatabasePath}; - -use crate::http::utils::unwrap_authenticated_entity; +use crate::http::structs::EntityDatabasePath; +use crate::server::config::AybConfig; +use crate::server::permissions::can_query; +use crate::server::utils::unwrap_authenticated_entity; use actix_web::{post, web}; #[post("/v1/{entity}/{database}/query")] diff --git a/src/http/endpoints/register.rs b/src/server/endpoints/register.rs similarity index 88% rename from src/http/endpoints/register.rs rename to src/server/endpoints/register.rs index 06645045..14d1a17c 100644 --- a/src/http/endpoints/register.rs +++ b/src/server/endpoints/register.rs @@ -2,10 +2,11 @@ use crate::ayb_db::db_interfaces::AybDb; use crate::ayb_db::models::{AuthenticationMethodType, EntityType}; use crate::email::send_registration_email; use crate::error::AybError; -use crate::http::structs::{AuthenticationDetails, AybConfig, EmptyResponse}; -use crate::http::tokens::encrypt_auth_token; -use crate::http::utils::{get_header, get_lowercased_header}; -use crate::http::web_frontend::WebFrontendDetails; +use crate::http::structs::{AuthenticationDetails, EmptyResponse}; +use crate::server::config::AybConfig; +use crate::server::tokens::encrypt_auth_token; +use crate::server::utils::{get_header, get_lowercased_header}; +use crate::server::web_frontend::WebFrontendDetails; use actix_web::{post, web, HttpRequest, HttpResponse}; use std::str::FromStr; diff --git a/src/http/endpoints/update_profile.rs b/src/server/endpoints/update_profile.rs similarity index 95% rename from src/http/endpoints/update_profile.rs rename to src/server/endpoints/update_profile.rs index f27b1a18..2b191b6d 100644 --- a/src/http/endpoints/update_profile.rs +++ b/src/server/endpoints/update_profile.rs @@ -2,9 +2,9 @@ use crate::ayb_db::db_interfaces::AybDb; use crate::ayb_db::models::{InstantiatedEntity, Link, PartialEntity}; use crate::error::AybError; use crate::http::structs::{EmptyResponse, EntityPath, ProfileLinkUpdate}; -use crate::http::url_verification::is_verified_url; -use crate::http::utils::unwrap_authenticated_entity; -use crate::http::web_frontend::WebFrontendDetails; +use crate::server::url_verification::is_verified_url; +use crate::server::utils::unwrap_authenticated_entity; +use crate::server::web_frontend::WebFrontendDetails; use actix_web::{patch, web, HttpResponse}; use std::collections::HashMap; use std::str::FromStr; diff --git a/src/http/permissions.rs b/src/server/permissions.rs similarity index 100% rename from src/http/permissions.rs rename to src/server/permissions.rs diff --git a/src/http/server.rs b/src/server/server_runner.rs similarity index 94% rename from src/http/server.rs rename to src/server/server_runner.rs index 079d383d..005b3ba6 100644 --- a/src/http/server.rs +++ b/src/server/server_runner.rs @@ -1,14 +1,14 @@ use crate::ayb_db::db_interfaces::connect_to_ayb_db; use crate::ayb_db::db_interfaces::AybDb; use crate::error::AybError; -use crate::http::config::read_config; -use crate::http::endpoints::{ +use crate::server::config::read_config; +use crate::server::config::AybConfigCors; +use crate::server::endpoints::{ confirm_endpoint, create_db_endpoint, entity_details_endpoint, log_in_endpoint, query_endpoint, register_endpoint, update_profile_endpoint, }; -use crate::http::structs::AybConfigCors; -use crate::http::tokens::retrieve_and_validate_api_token; -use crate::http::web_frontend::WebFrontendDetails; +use crate::server::tokens::retrieve_and_validate_api_token; +use crate::server::web_frontend::WebFrontendDetails; use actix_cors::Cors; use actix_web::dev::ServiceRequest; use actix_web::{middleware, web, App, Error, HttpMessage, HttpServer}; diff --git a/src/http/tokens.rs b/src/server/tokens.rs similarity index 96% rename from src/http/tokens.rs rename to src/server/tokens.rs index ae7e35e0..2227bb1b 100644 --- a/src/http/tokens.rs +++ b/src/server/tokens.rs @@ -1,7 +1,8 @@ use crate::ayb_db::db_interfaces::AybDb; use crate::ayb_db::models::{APIToken, APITokenStatus, InstantiatedEntity}; use crate::error::AybError; -use crate::http::structs::{AuthenticationDetails, AybConfigAuthentication}; +use crate::http::structs::AuthenticationDetails; +use crate::server::config::AybConfigAuthentication; use actix_web::web; use fernet::Fernet; use prefixed_api_key::rand::rngs::OsRng; diff --git a/src/http/url_verification.rs b/src/server/url_verification.rs similarity index 100% rename from src/http/url_verification.rs rename to src/server/url_verification.rs diff --git a/src/http/utils.rs b/src/server/utils.rs similarity index 100% rename from src/http/utils.rs rename to src/server/utils.rs diff --git a/src/http/web_frontend.rs b/src/server/web_frontend.rs similarity index 100% rename from src/http/web_frontend.rs rename to src/server/web_frontend.rs