Skip to content

Commit

Permalink
Move everything but structs from http to server module
Browse files Browse the repository at this point in the history
  • Loading branch information
marcua committed Jan 21, 2024
1 parent 6991f6d commit 24bbde4
Show file tree
Hide file tree
Showing 24 changed files with 103 additions and 97 deletions.
4 changes: 2 additions & 2 deletions src/bin/ayb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::run_server;
use clap::builder::ValueParser;
use clap::{arg, command, value_parser, Command, ValueEnum};
use directories::ProjectDirs;
Expand Down
4 changes: 2 additions & 2 deletions src/email/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/email/templating.rs
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/hosted_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/hosted_db/sqlite.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
8 changes: 0 additions & 8 deletions src/http.rs
Original file line number Diff line number Diff line change
@@ -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;
52 changes: 0 additions & 52 deletions src/http/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>,
pub authentication: AybConfigAuthentication,
pub email: AybConfigEmail,
pub web: Option<AybConfigWeb>,
pub cors: AybConfigCors,
pub isolation: Option<AybConfigIsolation>,
}

impl AybConfig {
pub fn e2e_testing_on(&self) -> bool {
self.e2e_testing.unwrap_or(false)
}
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Database {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ pub mod error;
pub mod formatting;
pub mod hosted_db;
pub mod http;
pub mod server;
pub mod templating;
8 changes: 8 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub mod config;
pub mod endpoints;
pub mod permissions;
pub mod server;
pub mod tokens;
pub mod url_verification;
pub mod utils;
pub mod web_frontend;
54 changes: 53 additions & 1 deletion src/http/config.rs → src/server/config.rs
Original file line number Diff line number Diff line change
@@ -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<bool>,
pub authentication: AybConfigAuthentication,
pub email: AybConfigEmail,
pub web: Option<AybConfigWeb>,
pub cors: AybConfigCors,
pub isolation: Option<AybConfigIsolation>,
}

impl AybConfig {
pub fn e2e_testing_on(&self) -> bool {
self.e2e_testing.unwrap_or(false)
}
}

pub fn config_to_toml(ayb_config: AybConfig) -> Result<String, AybError> {
Ok(toml::to_string(&ayb_config)?)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
Original file line number Diff line number Diff line change
@@ -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}")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions src/http/endpoints/query.rs → src/server/endpoints/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions src/http/server.rs → src/server/server.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
3 changes: 2 additions & 1 deletion src/http/tokens.rs → src/server/tokens.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 24bbde4

Please sign in to comment.