about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2022-05-07 20:28:43 +0300
committerVika <vika@fireburn.ru>2022-05-07 20:28:43 +0300
commit139b7ec10bc7f08dae9bd57eef8eff73fbb22061 (patch)
tree973c4d903e731cb5e38b682f22c38c3da1dbcd92
parent0679de841840c74ab49f54905783fac1faf028e1 (diff)
downloadkittybox-139b7ec10bc7f08dae9bd57eef8eff73fbb22061.tar.zst
Split into different crates
Templates and utility types are now separate crates to speed up
compilation, linting and potential reuse/replacement.

Potentially more crates could be split out/modularized, resulting in
speedups, smaller binaries (whenever features are excluded) and even
more reuse capabilities.
-rw-r--r--Cargo.lock23
-rw-r--r--Cargo.toml13
-rw-r--r--src/database/mod.rs10
-rw-r--r--src/frontend/login.rs19
-rw-r--r--src/frontend/mod.rs21
-rw-r--r--src/lib.rs9
-rw-r--r--templates/Cargo.toml19
-rw-r--r--templates/src/lib.rs6
-rw-r--r--templates/src/login.rs17
-rw-r--r--templates/src/onboarding.rs (renamed from src/frontend/templates/onboarding.rs)0
-rw-r--r--templates/src/templates.rs (renamed from src/frontend/templates/mod.rs)10
-rw-r--r--util/Cargo.toml12
-rw-r--r--util/src/lib.rs18
13 files changed, 117 insertions, 60 deletions
diff --git a/Cargo.lock b/Cargo.lock
index acf370d..40a3401 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1571,7 +1571,6 @@ dependencies = [
  "data-encoding",
  "easy-scraper",
  "either",
- "ellipse",
  "env_logger 0.8.4",
  "futures",
  "futures-util",
@@ -1579,6 +1578,8 @@ dependencies = [
  "httpmock",
  "hyper",
  "hyper-rustls",
+ "kittybox-templates",
+ "kittybox-util",
  "lazy_static",
  "listenfd",
  "log 0.4.17",
@@ -1609,6 +1610,26 @@ dependencies = [
 ]
 
 [[package]]
+name = "kittybox-templates"
+version = "0.1.0"
+dependencies = [
+ "chrono",
+ "ellipse",
+ "http",
+ "kittybox-util",
+ "log 0.4.17",
+ "markup",
+ "serde_json",
+]
+
+[[package]]
+name = "kittybox-util"
+version = "0.1.0"
+dependencies = [
+ "serde",
+]
+
+[[package]]
 name = "kuchiki"
 version = "0.8.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index f8dbd6a..eaaac7b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -24,6 +24,16 @@ name = "kittybox-database-converter"
 path = "src/bin/kittybox_database_converter.rs"
 required-features = ["util", "redis"]
 
+[workspace]
+members = [".", "./util", "./templates"]
+default-members = [".", "./util", "./templates"]
+[dependencies.kittybox-util]
+version = "0.1.0"
+path = "./util"
+[dependencies.kittybox-templates]
+version = "0.1.0"
+path = "./templates"
+
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dev-dependencies]
@@ -39,7 +49,6 @@ bytes = "^1.1.0"
 data-encoding = "^2.3.2"     # Efficient and customizable data-encoding functions like base64, base32, and hex
 easy-scraper = "^0.2.0"      # HTML scraping library focused on ease of use
 either = "^1.6.1"            # A general purpose sum type with two cases
-ellipse = "^0.2.0"           # Truncate and ellipsize strings in a human-friendly way
 env_logger = "^0.8.3"        # A logging implementation for `log` which is configured via an environment variable
 futures = "^0.3.14"          # An implementation of futures and streams
 futures-util = "^0.3.14"     # Common utilities and extension traits for the futures-rs library
@@ -102,4 +111,4 @@ features = ["webpki-tokio", "http1", "http2", "tls12", "logging"]
 [dependencies.reqwest]
 version = "^0.11.10"
 default-features = false
-features = ["rustls-tls-webpki-roots", "gzip", "brotli", "json", "stream"]
+features = ["rustls-tls-webpki-roots", "gzip", "brotli", "json", "stream"]
\ No newline at end of file
diff --git a/src/database/mod.rs b/src/database/mod.rs
index 0d98dd4..5a1dd3f 100644
--- a/src/database/mod.rs
+++ b/src/database/mod.rs
@@ -9,15 +9,7 @@ mod memory;
 #[cfg(test)]
 pub use crate::database::memory::MemoryStorage;
 
-
-/// Data structure representing a Micropub channel in the ?q=channels output.
-#[derive(Serialize, Deserialize, PartialEq, Debug)]
-pub struct MicropubChannel {
-    /// The channel's UID. It is usually also a publically accessible permalink URL.
-    pub uid: String,
-    /// The channel's user-friendly name used to recognize it in lists.
-    pub name: String,
-}
+pub use kittybox_util::MicropubChannel;
 
 /// Enum representing different errors that might occur during the database query.
 #[derive(Debug, Clone, Copy)]
diff --git a/src/frontend/login.rs b/src/frontend/login.rs
index 35ce3db..9665ce7 100644
--- a/src/frontend/login.rs
+++ b/src/frontend/login.rs
@@ -9,24 +9,7 @@ use std::str::FromStr;
 use crate::frontend::templates::Template;
 use crate::frontend::{FrontendError, IndiewebEndpoints};
 use crate::{database::Storage, ApplicationState};
-
-markup::define! {
-    LoginPage {
-        form[method="POST"] {
-            h1 { "Sign in with your website" }
-            p {
-                "Signing in to Kittybox might allow you to view private content "
-                    "intended for your eyes only."
-            }
-
-            section {
-                label[for="url"] { "Your website URL" }
-                input[id="url", name="url", placeholder="https://example.com/"];
-                input[type="submit"];
-            }
-        }
-    }
-}
+use kittybox_templates::LoginPage;
 
 pub async fn form<S: Storage>(req: Request<ApplicationState<S>>) -> Result {
     let owner = req.url().origin().ascii_serialization() + "/";
diff --git a/src/frontend/mod.rs b/src/frontend/mod.rs
index daeebd9..106d839 100644
--- a/src/frontend/mod.rs
+++ b/src/frontend/mod.rs
@@ -4,21 +4,12 @@ use serde::{Deserialize, Serialize};
 use futures_util::TryFutureExt;
 use warp::{http::StatusCode, Filter, host::Authority, path::FullPath};
 
-static POSTS_PER_PAGE: usize = 20;
-
 //pub mod login;
 
-mod templates;
 #[allow(unused_imports)]
-use templates::{ErrorPage, MainPage, OnboardingPage, Template};
-
-#[derive(Clone, Serialize, Deserialize)]
-pub struct IndiewebEndpoints {
-    pub authorization_endpoint: String,
-    pub token_endpoint: String,
-    pub webmention: Option<String>,
-    pub microsub: Option<String>,
-}
+use kittybox_templates::{ErrorPage, MainPage, OnboardingPage, Template, POSTS_PER_PAGE};
+
+pub use kittybox_util::IndiewebEndpoints;
 
 #[derive(Deserialize)]
 struct QueryParams {
@@ -364,17 +355,17 @@ pub fn catchall<D: Storage>(db: D, endpoints: IndiewebEndpoints) -> impl Filter<
             {
                 Some("h-entry") => Ok((
                     post_name.unwrap_or("Note").to_string(),
-                    templates::Entry { post: &post }.to_string(),
+                    kittybox_templates::Entry { post: &post }.to_string(),
                     StatusCode::OK
                 )),
                 Some("h-card") => Ok((
                     post_name.unwrap_or("Contact card").to_string(),
-                    templates::VCard { card: &post }.to_string(),
+                    kittybox_templates::VCard { card: &post }.to_string(),
                     StatusCode::OK
                 )),
                 Some("h-feed") => Ok((
                     post_name.unwrap_or("Feed").to_string(),
-                    templates::Feed { feed: &post }.to_string(),
+                    kittybox_templates::Feed { feed: &post }.to_string(),
                     StatusCode::OK
                 )),
                 _ => Err(warp::reject::custom(FrontendError::with_code(
diff --git a/src/lib.rs b/src/lib.rs
index 2709022..9d4335a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -31,15 +31,6 @@ pub mod util {
             })
     }
 
-    pub fn template<R>(
-        template: R
-    ) -> impl warp::Reply
-    where
-        R: markup::Render + std::fmt::Display
-    {
-        warp::reply::html(template.to_string())
-    }
-
     pub fn parse_accept() -> impl Filter<Extract = (http_types::Mime,), Error = warp::Rejection> + Copy {
         warp::header::value("Accept").and_then(|accept: warp::http::HeaderValue| async move {
             let mut accept: http_types::content::Accept = {
diff --git a/templates/Cargo.toml b/templates/Cargo.toml
new file mode 100644
index 0000000..c4c7f46
--- /dev/null
+++ b/templates/Cargo.toml
@@ -0,0 +1,19 @@
+[package]
+name = "kittybox-templates"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+ellipse = "^0.2.0"           # Truncate and ellipsize strings in a human-friendly way
+http = "^0.2.7"              # Hyper's strong HTTP types
+log = "^0.4.14"              # A lightweight logging facade for Rust
+markup = "^0.12.0"           # HTML templating engine
+serde_json = "^1.0.64"       # A JSON serialization file format
+[dependencies.chrono]        # Date and time library for Rust
+version = "^0.4.19"
+features = ["serde"]
+[dependencies.kittybox-util]
+version = "0.1.0"
+path = "../util"
\ No newline at end of file
diff --git a/templates/src/lib.rs b/templates/src/lib.rs
new file mode 100644
index 0000000..c7b03ea
--- /dev/null
+++ b/templates/src/lib.rs
@@ -0,0 +1,6 @@
+mod templates;
+pub use templates::{ErrorPage, MainPage, Template, POSTS_PER_PAGE, Entry, VCard, Feed};
+mod onboarding;
+pub use onboarding::OnboardingPage;
+mod login;
+pub use login::LoginPage;
diff --git a/templates/src/login.rs b/templates/src/login.rs
new file mode 100644
index 0000000..042c308
--- /dev/null
+++ b/templates/src/login.rs
@@ -0,0 +1,17 @@
+markup::define! {
+    LoginPage {
+        form[method="POST"] {
+            h1 { "Sign in with your website" }
+            p {
+                "Signing in to Kittybox might allow you to view private content "
+                    "intended for your eyes only."
+            }
+
+            section {
+                label[for="url"] { "Your website URL" }
+                input[id="url", name="url", placeholder="https://example.com/"];
+                input[type="submit"];
+            }
+        }
+    }
+}
diff --git a/src/frontend/templates/onboarding.rs b/templates/src/onboarding.rs
index 9d0f2e1..9d0f2e1 100644
--- a/src/frontend/templates/onboarding.rs
+++ b/templates/src/onboarding.rs
diff --git a/src/frontend/templates/mod.rs b/templates/src/templates.rs
index 1f7ac6a..53b0965 100644
--- a/src/frontend/templates/mod.rs
+++ b/templates/src/templates.rs
@@ -1,9 +1,10 @@
-use crate::database::MicropubChannel;
-use crate::frontend::IndiewebEndpoints;
+use kittybox_util::{MicropubChannel, IndiewebEndpoints};
 use ellipse::Ellipse;
-use warp::http::StatusCode;
+use http::StatusCode;
 use log::error;
 
+pub static POSTS_PER_PAGE: usize = 20;
+
 /// Return a pretty location specifier from a geo: URI.
 fn decode_geo_uri(uri: &str) -> String {
     if let Some(part) = uri.split(':').collect::<Vec<_>>().get(1) {
@@ -21,9 +22,6 @@ fn decode_geo_uri(uri: &str) -> String {
     }
 }
 
-mod onboarding;
-pub use onboarding::OnboardingPage;
-
 markup::define! {
     Template<'a>(title: &'a str, blog_name: &'a str, endpoints: Option<IndiewebEndpoints>, feeds: Vec<MicropubChannel>, user: Option<String>, content: String) {
         @markup::doctype()
diff --git a/util/Cargo.toml b/util/Cargo.toml
new file mode 100644
index 0000000..31c6bca
--- /dev/null
+++ b/util/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "kittybox-util"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+[dependencies.serde]         # A generic serialization/deserialization framework
+version = "^1.0.125"
+features = ["derive"]
+
diff --git a/util/src/lib.rs b/util/src/lib.rs
new file mode 100644
index 0000000..bc41689
--- /dev/null
+++ b/util/src/lib.rs
@@ -0,0 +1,18 @@
+use serde::{Deserialize, Serialize};
+
+#[derive(Clone, Serialize, Deserialize)]
+pub struct IndiewebEndpoints {
+    pub authorization_endpoint: String,
+    pub token_endpoint: String,
+    pub webmention: Option<String>,
+    pub microsub: Option<String>,
+}
+
+/// Data structure representing a Micropub channel in the ?q=channels output.
+#[derive(Serialize, Deserialize, PartialEq, Debug)]
+pub struct MicropubChannel {
+    /// The channel's UID. It is usually also a publically accessible permalink URL.
+    pub uid: String,
+    /// The channel's user-friendly name used to recognize it in lists.
+    pub name: String,
+}