From 6b99a872ec31826fb37524c136e3dab2e6241bf3 Mon Sep 17 00:00:00 2001 From: blek Date: Sat, 30 Sep 2023 19:26:47 +1000 Subject: [PATCH] get it to run with docker --- .gitignore | 1 + conf/caddy/Caddyfile | 3 +++ docker-compose.dev.yml | 21 +++++++++++++++++++++ filed/.env | 2 +- filed/Cargo.lock | 14 ++++++++++++++ filed/Cargo.toml | 2 ++ filed/Dockerfile.dev | 7 +++++-- filed/dev-entry.sh | 2 ++ filed/src/web/forms.rs | 30 ++++++++++++++++++++++++++++++ filed/src/web/mod.rs | 6 +++++- filed/src/web/pages.rs | 4 +--- filed/static/form.css | 8 +++++++- filed/templates/index.html | 11 ++++++++++- 13 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 .gitignore create mode 100644 conf/caddy/Caddyfile create mode 100644 docker-compose.dev.yml create mode 100644 filed/src/web/forms.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1120be9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +docker-compose.yml diff --git a/conf/caddy/Caddyfile b/conf/caddy/Caddyfile new file mode 100644 index 0000000..5c0d6ce --- /dev/null +++ b/conf/caddy/Caddyfile @@ -0,0 +1,3 @@ +:80 { + reverse_proxy http://filed +} \ No newline at end of file diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml new file mode 100644 index 0000000..622fe1f --- /dev/null +++ b/docker-compose.dev.yml @@ -0,0 +1,21 @@ +version: '3.7' +services: + filed: + build: + context: filed + dockerfile: Dockerfile.dev + networks: + bfile: + volumes: + - './filed:/opt/code' + caddy: + image: caddy:alpine + volumes: + - './conf/caddy:/etc/caddy:ro' + ports: + - 80:80 + networks: + bfile: + +networks: + bfile: \ No newline at end of file diff --git a/filed/.env b/filed/.env index 518b22d..6107bd1 100644 --- a/filed/.env +++ b/filed/.env @@ -1,2 +1,2 @@ APP_LOGGING=true -APP_HOST=0.0.0.0:8080 +APP_HOST=0.0.0.0:80 diff --git a/filed/Cargo.lock b/filed/Cargo.lock index 241fa07..e3fd44e 100644 --- a/filed/Cargo.lock +++ b/filed/Cargo.lock @@ -210,8 +210,10 @@ name = "filed" version = "0.1.0" dependencies = [ "askama", + "bytes", "dotenvy", "femme", + "futures-util", "log", "tokio", "warp", @@ -248,6 +250,17 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" +[[package]] +name = "futures-macro" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "futures-sink" version = "0.3.28" @@ -267,6 +280,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" dependencies = [ "futures-core", + "futures-macro", "futures-sink", "futures-task", "pin-project-lite", diff --git a/filed/Cargo.toml b/filed/Cargo.toml index 1e769e6..94f065b 100644 --- a/filed/Cargo.toml +++ b/filed/Cargo.toml @@ -7,8 +7,10 @@ edition = "2021" [dependencies] askama = "0.12.0" +bytes = "1.5.0" dotenvy = "0.15.7" femme = "2.2.1" +futures-util = "0.3.28" log = "0.4.20" tokio = { version = "1.32.0", features = ["rt", "macros", "rt-multi-thread"] } warp = "0.3.6" diff --git a/filed/Dockerfile.dev b/filed/Dockerfile.dev index f5f512f..995c30c 100644 --- a/filed/Dockerfile.dev +++ b/filed/Dockerfile.dev @@ -4,11 +4,14 @@ FROM rust as builder WORKDIR /opt/code COPY . . -# No build or install is done during this step +# No build is done during this step # since the directory will be mounted anyways # to the dev's machine. # Therefore installing & compiling in the dockerfile # would be moronic, to say at least -CMD [ "dev-entry.sh" ] +# However, cargo watch needs to be installed +RUN cargo install cargo-watch + +CMD [ "/opt/code/dev-entry.sh" ] diff --git a/filed/dev-entry.sh b/filed/dev-entry.sh index 5b832d9..c614b70 100755 --- a/filed/dev-entry.sh +++ b/filed/dev-entry.sh @@ -1,5 +1,7 @@ #!/bin/sh +cd /opt/code + cargo check cargo build diff --git a/filed/src/web/forms.rs b/filed/src/web/forms.rs new file mode 100644 index 0000000..28ed684 --- /dev/null +++ b/filed/src/web/forms.rs @@ -0,0 +1,30 @@ + +/* + forms.rs - All the forms +*/ + +use std::collections::HashMap; + +use warp::{Filter, reply::{Html, Reply}, reject::Rejection, filters::multipart::FormData}; +use futures_util::TryStreamExt; +use bytes::BufMut; + +pub async fn upload(form: FormData) -> Result, Rejection> { + + let params: HashMap = form.and_then(|mut field| async move { + let mut bytes: Vec = vec![]; + while let Some(byte) = field.data().await { + bytes.put(byte.unwrap()) + } + + Ok((field.name().into(), String::from_utf8_lossy(&bytes).to_string())) + }).try_collect().await.unwrap(); + + Ok(Box::new(warp::reply::json(¶ms))) +} + +pub fn get_routes() -> impl Filter + Clone { + warp::post().and( + warp::multipart::form().and_then(upload) + ) +} \ No newline at end of file diff --git a/filed/src/web/mod.rs b/filed/src/web/mod.rs index 22e12fa..3384853 100644 --- a/filed/src/web/mod.rs +++ b/filed/src/web/mod.rs @@ -9,11 +9,15 @@ use warp::{Filter, reply::Reply, reject::Rejection}; use crate::env::Env; mod pages; +mod forms; pub fn routes() -> impl Filter + Clone { let staticpath = current_dir().unwrap(); let staticpath = staticpath.to_str().unwrap().to_string() + "/static"; - pages::get_routes().or(warp::fs::dir(staticpath.to_string())) + + pages::get_routes() + .or(forms::get_routes()) + .or(warp::fs::dir(staticpath.to_string())) } /* diff --git a/filed/src/web/pages.rs b/filed/src/web/pages.rs index 79114a6..5fc35c3 100644 --- a/filed/src/web/pages.rs +++ b/filed/src/web/pages.rs @@ -7,9 +7,7 @@ use askama::Template; #[derive(Template)] #[template( path = "index.html" )] -struct Index { - -} +struct Index {} pub fn index() -> Html { let rendered = Index {}; diff --git a/filed/static/form.css b/filed/static/form.css index 4ab8710..501fbf3 100644 --- a/filed/static/form.css +++ b/filed/static/form.css @@ -31,4 +31,10 @@ padding: 2px 4px; margin-left: 5px; } -} \ No newline at end of file +} +input[type=text], +input[type=password], +input[type=email] { + padding: 4px 6px; + border-radius: 6px; +} diff --git a/filed/templates/index.html b/filed/templates/index.html index 2fcb6d4..642d7eb 100644 --- a/filed/templates/index.html +++ b/filed/templates/index.html @@ -29,7 +29,7 @@

-

+

+ + +