From a069b318bc6ace295c8c00a0272979701946c0ad Mon Sep 17 00:00:00 2001 From: Adrian Wozniak Date: Fri, 1 May 2020 22:21:28 +0200 Subject: [PATCH] Add multipart --- .builds/client.yml | 18 +++++++++++++----- .builds/server.yml | 9 ++++++++- README.md | 29 +++++++++++++++++++++++++++++ jirs-server/src/web/avatar.rs | 20 +++++++++++++++++++- 4 files changed, 69 insertions(+), 7 deletions(-) diff --git a/.builds/client.yml b/.builds/client.yml index 1cf34017..dcc5e0d2 100644 --- a/.builds/client.yml +++ b/.builds/client.yml @@ -2,20 +2,28 @@ image: archlinux packages: - nodejs - rustup + - yarn sources: - https://git.sr.ht/~tsumanu/jirs environment: deploy: adrian.wozniak@ita-prog.pl + NODE_ENV: production + DEBUG: false + JIRS_CLIENT_PORT: 80 + JIRS_CLIENT_BIND: jirs.ita-prog.pl + JIRS_SERVER_PORT: 80 + JIRS_SERVER_BIND: jirs.ita-prog.pl secrets: - 7ebab768-e5e4-4c9d-ba57-ec41a72c5665 tasks: - setup: | - curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh + curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sudo sh - test: | - cd jirs/jirs-client + cd ~/jirs/jirs-client wasm-pack test --node - build: | - cd jirs/jirs-client - wasm-pack build --release + cd ~/jirs/jirs-client + yarn + yarn webpack-cli -p --config ./webpack.config.js - deploy: | - cd jirs + cd ~/jirs diff --git a/.builds/server.yml b/.builds/server.yml index c32a6d4a..e59cd5d2 100644 --- a/.builds/server.yml +++ b/.builds/server.yml @@ -1,6 +1,7 @@ -image: shepmaster/rust-nightly +image: archlinux packages: - postgresql + - rustup sources: - https://git.sr.ht/~tsumanu/jirs environment: @@ -8,8 +9,14 @@ environment: secrets: - 7ebab768-e5e4-4c9d-ba57-ec41a72c5665 tasks: + - build_config: | + cd jirs/jirs-server + echo 'concurrency = 2' >> db.test.toml + echo 'database_url = "postgres://postgres@localhost:5432/jirs_test"' >> db.test.toml - setup: | sudo systemctl start postgresql + rustup toolchain install nightly + rustup default nightly cargo install diesel_cli --no-default-features --features postgres cd jirs/jirs-server diesel migration run diff --git a/README.md b/README.md index 8652bad1..3473b357 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # A simplified Jira clone built with seed.rs and actix +[![builds.sr.ht status](https://builds.sr.ht/~tsumanu/jirs.sr.ht.svg)](https://builds.sr.ht/~tsumanu/jirs.sr.ht?) + +https://git.sr.ht/~tsumanu/jirs + ## Features * Actor based asynchronous backend @@ -7,6 +11,31 @@ ## How to run it +### Config files + +```toml +# web.toml +concurrency = 2 +port = "5000" +bind = "0.0.0.0" +ssl = false +``` + +```toml +# db.toml +concurrency = 2 +database_url = "postgres://postgres@localhost:5432/jirs" +``` + +```toml +# mail.toml +concurrency = 2 +user = "apikey" +pass = "YOUR-TOKEN" +host = "smtp.sendgrid.net" +from = "contact@jirs.pl" +``` + ### Local variables Within `jirs` directory place `.env` file with following content diff --git a/jirs-server/src/web/avatar.rs b/jirs-server/src/web/avatar.rs index 23fbcb81..da61ef07 100644 --- a/jirs-server/src/web/avatar.rs +++ b/jirs-server/src/web/avatar.rs @@ -1,7 +1,25 @@ +use actix_multipart::Multipart; use actix_web::{get, post, web, HttpResponse, Responder}; +use futures::{StreamExt, TryStreamExt}; +use std::io::Write; #[post("/")] -async fn upload() -> impl Responder { +async fn upload(mut payload: Multipart) -> impl Responder { + while let Ok(Some(mut field)) = payload.try_next().await { + let content_type = field.content_disposition().unwrap(); + let filename = content_type.get_filename().unwrap(); + let filepath = format!("./tmp/{}", filename); + // File::create is blocking operation, use threadpool + let mut f = web::block(|| std::fs::File::create(filepath)) + .await + .unwrap(); + // Field in turn is stream of *Bytes* object + while let Some(chunk) = field.next().await { + let data = chunk.unwrap(); + // filesystem operations are blocking, we have to use threadpool + f = web::block(move || f.write_all(&data).map(|_| f)).await?; + } + } HttpResponse::Ok().json("") }