From a78b1eb25d662869b5a9bfb5a78fe36f749c3b3e Mon Sep 17 00:00:00 2001 From: realaravinth Date: Sat, 3 Apr 2021 17:18:18 +0530 Subject: [PATCH] frontend templating --- Cargo.lock | 53 ++++++++++--- Cargo.toml | 6 ++ frontend/.gitignore | 3 + frontend/Cargo.toml | 14 ++++ frontend/package.json | 4 +- frontend/public/signup/index.html | 75 ------------------ frontend/src/main.rs | 70 ++++++++++++++++ .../{public => static}/img/icon-trans.png | Bin frontend/{public => static}/img/icon.png | Bin frontend/{src => static/js}/api/v1/routes.js | 0 .../{src => static/js}/auth/emailExists.js | 0 frontend/{src => static/js}/auth/register.js | 0 frontend/{src => static/js}/auth/signin.js | 0 .../{src => static/js}/auth/userExists.js | 0 frontend/static/js/css/_reset.scss | 4 + frontend/{src => static/js}/css/forms.scss | 5 +- frontend/static/js/css/main.scss | 0 frontend/{src => static/js}/index.js | 0 frontend/{src => static/js}/signup/index.js | 0 .../js}/utils/genJsonPayload.js | 0 .../{src => static/js}/utils/isBlankString.js | 0 frontend/templates/components/headers.stpl | 7 ++ .../index.html => templates/index.stpl} | 12 +-- frontend/templates/signup/index.stpl | 31 ++++++++ frontend/webpack.common.js | 2 +- frontend/webpack.dev.js | 4 +- frontend/webpack.prod.js | 6 +- 27 files changed, 189 insertions(+), 107 deletions(-) create mode 100644 frontend/Cargo.toml delete mode 100644 frontend/public/signup/index.html create mode 100644 frontend/src/main.rs rename frontend/{public => static}/img/icon-trans.png (100%) rename frontend/{public => static}/img/icon.png (100%) rename frontend/{src => static/js}/api/v1/routes.js (100%) rename frontend/{src => static/js}/auth/emailExists.js (100%) rename frontend/{src => static/js}/auth/register.js (100%) rename frontend/{src => static/js}/auth/signin.js (100%) rename frontend/{src => static/js}/auth/userExists.js (100%) create mode 100644 frontend/static/js/css/_reset.scss rename frontend/{src => static/js}/css/forms.scss (97%) create mode 100644 frontend/static/js/css/main.scss rename frontend/{src => static/js}/index.js (100%) rename frontend/{src => static/js}/signup/index.js (100%) rename frontend/{src => static/js}/utils/genJsonPayload.js (100%) rename frontend/{src => static/js}/utils/isBlankString.js (100%) create mode 100644 frontend/templates/components/headers.stpl rename frontend/{public/index.html => templates/index.stpl} (77%) create mode 100644 frontend/templates/signup/index.stpl diff --git a/Cargo.lock b/Cargo.lock index 8fdb05e8..be0aad88 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,7 +19,7 @@ dependencies = [ "parking_lot", "pin-project 0.4.28", "smallvec", - "tokio", + "tokio 0.2.25", "tokio-util", "trust-dns-proto", "trust-dns-resolver", @@ -37,7 +37,7 @@ dependencies = [ "futures-sink", "log", "pin-project 0.4.28", - "tokio", + "tokio 0.2.25", "tokio-util", ] @@ -176,7 +176,7 @@ dependencies = [ "futures-channel", "futures-util", "smallvec", - "tokio", + "tokio 0.2.25", ] [[package]] @@ -1114,6 +1114,16 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "frontend" +version = "0.1.0" +dependencies = [ + "log", + "pretty_env_logger", + "sailfish", + "tokio 1.4.0", +] + [[package]] name = "fuchsia-zircon" version = "0.3.3" @@ -1335,7 +1345,7 @@ dependencies = [ "http", "indexmap", "slab", - "tokio", + "tokio 0.2.25", "tokio-util", "tracing", "tracing-futures", @@ -2681,7 +2691,7 @@ dependencies = [ "actix-rt", "actix-threadpool", "once_cell", - "tokio", + "tokio 0.2.25", "tokio-rustls", ] @@ -2950,10 +2960,22 @@ dependencies = [ "pin-project-lite 0.1.12", "signal-hook-registry", "slab", - "tokio-macros", + "tokio-macros 0.2.6", "winapi 0.3.9", ] +[[package]] +name = "tokio" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "134af885d758d645f0f0505c9a8b3f9bf8a348fd822e112ab5248138348f1722" +dependencies = [ + "autocfg", + "num_cpus", + "pin-project-lite 0.2.6", + "tokio-macros 1.1.0", +] + [[package]] name = "tokio-macros" version = "0.2.6" @@ -2965,6 +2987,17 @@ dependencies = [ "syn", ] +[[package]] +name = "tokio-macros" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caf7b11a536f46a809a8a9f0bb4237020f70ecbf115b842360afb127ea2fda57" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tokio-rustls" version = "0.14.1" @@ -2973,7 +3006,7 @@ checksum = "e12831b255bcfa39dc0436b01e19fea231a37db570686c06ee72c423479f889a" dependencies = [ "futures-core", "rustls", - "tokio", + "tokio 0.2.25", "webpki", ] @@ -2989,7 +3022,7 @@ dependencies = [ "futures-sink", "log", "pin-project-lite 0.1.12", - "tokio", + "tokio 0.2.25", ] [[package]] @@ -3048,7 +3081,7 @@ dependencies = [ "rand 0.7.3", "smallvec", "thiserror", - "tokio", + "tokio 0.2.25", "url", ] @@ -3067,7 +3100,7 @@ dependencies = [ "resolv-conf", "smallvec", "thiserror", - "tokio", + "tokio 0.2.25", "trust-dns-proto", ] diff --git a/Cargo.toml b/Cargo.toml index ad2dccf8..7d5507eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,12 @@ path = "./src/main.rs" name = "tests-migrate" path = "./src/tests-migrate.rs" +[workspace] +members = [ + ".", + "frontend", +] + [dependencies] actix-web = "3.3.2" actix = "0.10" diff --git a/frontend/.gitignore b/frontend/.gitignore index b9470778..ba827864 100644 --- a/frontend/.gitignore +++ b/frontend/.gitignore @@ -1,2 +1,5 @@ node_modules/ dist/ +/target +.html +output/ diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml new file mode 100644 index 00000000..6e47c2a4 --- /dev/null +++ b/frontend/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "frontend" +version = "0.1.0" +authors = ["realaravinth "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +sailfish = { version = "0.3.2", features = ["derive"]} +tokio = { version = "1.4.0", features = [ "rt-multi-thread", "macros", "fs", "test-util" ]} + +pretty_env_logger = "0.4" +log = "0.4" diff --git a/frontend/package.json b/frontend/package.json index f3cf56dc..18a4ccaf 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -7,8 +7,8 @@ "author": "Aravinth Manivannan ", "license": "AGPLv3 or above", "scripts": { - "start": "webpack-dev-server --config webpack.dev.js --open", - "build": "webpack --config webpack.prod.js" + "start": "cargo run && webpack-dev-server --config webpack.dev.js --open", + "build": "cargo run && webpack --config webpack.prod.js" }, "private": true, diff --git a/frontend/public/signup/index.html b/frontend/public/signup/index.html deleted file mode 100644 index ceda34f6..00000000 --- a/frontend/public/signup/index.html +++ /dev/null @@ -1,75 +0,0 @@ - - - - - - Login | mCaptcha - - -
- -

Join mCaptcha

- -
- - - - - - - - -
-
-

- Already have an account? - Click here to login -

-
-
- - diff --git a/frontend/src/main.rs b/frontend/src/main.rs new file mode 100644 index 00000000..b7d90821 --- /dev/null +++ b/frontend/src/main.rs @@ -0,0 +1,70 @@ +use log::{debug, info}; +use sailfish::TemplateOnce; +use tokio::fs; +use tokio::io::{Error, ErrorKind}; + +#[derive(TemplateOnce)] // automatically implement `TemplateOnce` trait +#[template(path = "index.stpl")] // specify the path to template +struct IndexPage { + // data to be passed to the template + name: String, + title: String, +} + +const BASE_DIR: &str = "./output"; + +#[tokio::main] +async fn main() { + pretty_env_logger::init(); + match fs::create_dir(BASE_DIR).await { + Err(e) => { + if e.kind() == ErrorKind::AlreadyExists { + info!("cleaning up old assetes"); + fs::remove_dir_all(BASE_DIR).await.unwrap(); + debug!("creating target location"); + fs::create_dir(BASE_DIR).await.unwrap(); + } + } + _ => (), + }; + + let ctx = IndexPage { + name: "mCaptcha".into(), + title: "Login".into(), + }; + + // Now render templates with given data + info!("rendering {}", path("index.html")); + let index = ctx.render_once().unwrap(); + fs::write(path("index.html"), index).await.unwrap(); + info!("wrote {}", path("index.html")); + + let ctx = signup::IndexPage { + name: "mCaptcha".into(), + title: "Register".into(), + }; + + // Now render templates with given data + info!("rendering {}", path("signup/index.html")); + let index = ctx.render_once().unwrap(); + fs::create_dir(path("signup")).await.unwrap(); + info!("creating dir {}", path("signup/")); + + fs::write(path("signup/index.html"), index).await.unwrap(); + info!("wrote {}", path("signup/index.html")); +} + +fn path(rel: &str) -> String { + format!("{}/{}", BASE_DIR, rel) +} + +mod signup { + use super::*; + #[derive(TemplateOnce)] // automatically implement `TemplateOnce` trait + #[template(path = "signup/index.stpl", escape = false)] // specify the path to template + pub struct IndexPage { + // data to be passed to the template + pub name: String, + pub title: String, + } +} diff --git a/frontend/public/img/icon-trans.png b/frontend/static/img/icon-trans.png similarity index 100% rename from frontend/public/img/icon-trans.png rename to frontend/static/img/icon-trans.png diff --git a/frontend/public/img/icon.png b/frontend/static/img/icon.png similarity index 100% rename from frontend/public/img/icon.png rename to frontend/static/img/icon.png diff --git a/frontend/src/api/v1/routes.js b/frontend/static/js/api/v1/routes.js similarity index 100% rename from frontend/src/api/v1/routes.js rename to frontend/static/js/api/v1/routes.js diff --git a/frontend/src/auth/emailExists.js b/frontend/static/js/auth/emailExists.js similarity index 100% rename from frontend/src/auth/emailExists.js rename to frontend/static/js/auth/emailExists.js diff --git a/frontend/src/auth/register.js b/frontend/static/js/auth/register.js similarity index 100% rename from frontend/src/auth/register.js rename to frontend/static/js/auth/register.js diff --git a/frontend/src/auth/signin.js b/frontend/static/js/auth/signin.js similarity index 100% rename from frontend/src/auth/signin.js rename to frontend/static/js/auth/signin.js diff --git a/frontend/src/auth/userExists.js b/frontend/static/js/auth/userExists.js similarity index 100% rename from frontend/src/auth/userExists.js rename to frontend/static/js/auth/userExists.js diff --git a/frontend/static/js/css/_reset.scss b/frontend/static/js/css/_reset.scss new file mode 100644 index 00000000..e209c163 --- /dev/null +++ b/frontend/static/js/css/_reset.scss @@ -0,0 +1,4 @@ +* { + padding: 0; + margin: 0; +} diff --git a/frontend/src/css/forms.scss b/frontend/static/js/css/forms.scss similarity index 97% rename from frontend/src/css/forms.scss rename to frontend/static/js/css/forms.scss index 166b8714..5dfcada7 100644 --- a/frontend/src/css/forms.scss +++ b/frontend/static/js/css/forms.scss @@ -1,7 +1,4 @@ -* { - padding: 0; - margin: 0; -} +@import 'reset'; .form__logo { width: 110px; diff --git a/frontend/static/js/css/main.scss b/frontend/static/js/css/main.scss new file mode 100644 index 00000000..e69de29b diff --git a/frontend/src/index.js b/frontend/static/js/index.js similarity index 100% rename from frontend/src/index.js rename to frontend/static/js/index.js diff --git a/frontend/src/signup/index.js b/frontend/static/js/signup/index.js similarity index 100% rename from frontend/src/signup/index.js rename to frontend/static/js/signup/index.js diff --git a/frontend/src/utils/genJsonPayload.js b/frontend/static/js/utils/genJsonPayload.js similarity index 100% rename from frontend/src/utils/genJsonPayload.js rename to frontend/static/js/utils/genJsonPayload.js diff --git a/frontend/src/utils/isBlankString.js b/frontend/static/js/utils/isBlankString.js similarity index 100% rename from frontend/src/utils/isBlankString.js rename to frontend/static/js/utils/isBlankString.js diff --git a/frontend/templates/components/headers.stpl b/frontend/templates/components/headers.stpl new file mode 100644 index 00000000..59b92c80 --- /dev/null +++ b/frontend/templates/components/headers.stpl @@ -0,0 +1,7 @@ + + + +<%= title %>|<%= name %> + + + diff --git a/frontend/public/index.html b/frontend/templates/index.stpl similarity index 77% rename from frontend/public/index.html rename to frontend/templates/index.stpl index 74ff4be9..45ad2ff6 100644 --- a/frontend/public/index.html +++ b/frontend/templates/index.stpl @@ -1,11 +1,6 @@ - - - - Login | mCaptcha - - +<% include!("components/headers.stpl"); %>
- +

Sign in to mCaptcha

@@ -32,7 +27,4 @@

- - - diff --git a/frontend/templates/signup/index.stpl b/frontend/templates/signup/index.stpl new file mode 100644 index 00000000..d96322cf --- /dev/null +++ b/frontend/templates/signup/index.stpl @@ -0,0 +1,31 @@ +<% include!("../components/headers.stpl"); %> + +
+ +

Join mCaptcha

+ + + + + + + +
+

+ New to mCaptcha? + Create account +

+
+
+ diff --git a/frontend/webpack.common.js b/frontend/webpack.common.js index 13f8eed6..eb54c8cf 100644 --- a/frontend/webpack.common.js +++ b/frontend/webpack.common.js @@ -2,7 +2,7 @@ const path = require('path'); module.exports = { entry: { - main: './src/index.js', + main: './static/js/index.js', }, module: { rules: [ diff --git a/frontend/webpack.dev.js b/frontend/webpack.dev.js index b414b859..c1e66c33 100644 --- a/frontend/webpack.dev.js +++ b/frontend/webpack.dev.js @@ -11,11 +11,11 @@ module.exports = merge(common, { }, plugins: [ new HtmlWebpackPlugin({ - template: "./public/index.html" + template: "./output/index.html" }), new HtmlWebpackPlugin({ filename: "signup/index.html", - template: "./public/signup/index.html" + template: "./output/signup/index.html" }) ], diff --git a/frontend/webpack.prod.js b/frontend/webpack.prod.js index 35c4eb0a..e7c49161 100644 --- a/frontend/webpack.prod.js +++ b/frontend/webpack.prod.js @@ -18,7 +18,7 @@ module.exports = merge(common, { new OptimizeCssAssetsPlugin(), new TerserPlugin(), new HtmlWebpackPlugin({ - template: path.resolve(__dirname, 'public/', 'index.html'), + template: path.resolve(__dirname, 'output', 'index.html'), minify: { removeAttributeQuotes: true, collapseWhitespace: true, @@ -26,8 +26,8 @@ module.exports = merge(common, { }, }), new HtmlWebpackPlugin({ - filename: 'signup/index.html', - template: path.resolve(__dirname, 'public/signup/', 'index.html'), + filename: 'signup/index.html', // output filename + template: path.resolve(__dirname, 'output/signup/', 'index.html'), minify: { removeAttributeQuotes: true, collapseWhitespace: true,