diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b9af2b0..30baaa7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,8 +14,11 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Choose nightly - run: rustup update && cargo update && rustup override set nightly + - name: Set up rust nightly + run: | + rustup update + cargo update + rustup override set nightly - - name: Build + - name: Compile run: cargo build --verbose diff --git a/.gitignore b/.gitignore index 73f8571..9cb171f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,13 +1,16 @@ # IDE /.idea -# Generated by Cargo -# will have compiled files and executables -/target/ +# Compiled +/target +/md-to-pdf -# These are backup files generated by rustfmt +# Generated by rustfmt **/*.rs.bk +# Individual +docker-compose.override.yml + +# Generated *.pdf .bash_history -md-to-pdf diff --git a/Cargo.lock b/Cargo.lock index df92c70..112c48b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -85,9 +85,9 @@ dependencies = [ [[package]] name = "base64" -version = "0.13.0" +version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" [[package]] name = "bitflags" @@ -138,7 +138,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be2018768ed1d848cc4d347d551546474025ba820e5db70e4c9aaa349f678bd7" dependencies = [ "aes-gcm", - "base64 0.13.0", + "base64 0.13.1", "hkdf", "hmac", "percent-encoding 2.2.0", @@ -233,9 +233,9 @@ dependencies = [ [[package]] name = "filetime" -version = "0.2.17" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e94a7bbaa59354bc20dd75b67f23e2797b4490e9d6928203fb105c79e448c86c" +checksum = "4b9663d381d07ae25dc88dbdf27df458faa83a9b25336bcac83d5e452b5fc9d3" dependencies = [ "cfg-if 1.0.0", "libc", @@ -290,9 +290,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.7" +version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" +checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" dependencies = [ "cfg-if 1.0.0", "libc", @@ -388,9 +388,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "1.9.1" +version = "1.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" +checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" dependencies = [ "autocfg", "hashbrown", @@ -458,9 +458,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" [[package]] name = "libc" -version = "0.2.134" +version = "0.2.137" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "329c933548736bc49fd575ee68c89e8be4d260064184389a5b77517cddd99ffb" +checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89" [[package]] name = "log" @@ -556,9 +556,9 @@ dependencies = [ [[package]] name = "net2" -version = "0.2.37" +version = "0.2.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "391630d12b68002ae1e25e8f974306474966550ad82dac6886fb8910c19568ae" +checksum = "74d0df99cfcd2530b2e694f6e17e7f37b8e26bb23983ac530c0c97408837c631" dependencies = [ "cfg-if 0.1.10", "libc", @@ -585,9 +585,9 @@ dependencies = [ [[package]] name = "num_cpus" -version = "1.13.1" +version = "1.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" +checksum = "f6058e64324c71e02bc2b150e4f3bc8286db6c83092132ffa3f6b1eab0f9def5" dependencies = [ "hermit-abi", "libc", @@ -646,9 +646,9 @@ dependencies = [ [[package]] name = "ppv-lite86" -version = "0.2.16" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "proc-macro2" @@ -723,7 +723,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "83b9d9dc08c5dcc1d8126a9dd615545e6a358f8c13c883c8dfed8c0376fa355e" dependencies = [ "atty", - "base64 0.13.0", + "base64 0.13.1", "log 0.4.17", "memchr", "num_cpus", @@ -797,9 +797,9 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.145" +version = "1.0.147" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "728eb6351430bccb993660dfffc5a72f91ccc1295abaa8ce19b27ebe4f75568b" +checksum = "d193d69bae983fc11a79df82342761dfbf28a99fc8d203dca4c3c1b590948965" [[package]] name = "sha2" @@ -1050,46 +1050,60 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-sys" -version = "0.36.1" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" dependencies = [ + "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_msvc", "windows_x86_64_gnu", + "windows_x86_64_gnullvm", "windows_x86_64_msvc", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e" + [[package]] name = "windows_aarch64_msvc" -version = "0.36.1" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" +checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" [[package]] name = "windows_i686_gnu" -version = "0.36.1" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" +checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" [[package]] name = "windows_i686_msvc" -version = "0.36.1" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" +checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" [[package]] name = "windows_x86_64_gnu" -version = "0.36.1" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" +checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028" [[package]] name = "windows_x86_64_msvc" -version = "0.36.1" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" +checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" [[package]] name = "ws2_32-sys" diff --git a/Dockerfile b/Dockerfile index 034066a..dc1c4d0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,3 +26,4 @@ USER rocket WORKDIR /home/rocket COPY static /home/rocket/static +COPY Rocket.toml /home/rocket/Rocket.toml diff --git a/Makefile b/Makefile index 14839ec..cbfe237 100644 --- a/Makefile +++ b/Makefile @@ -2,14 +2,14 @@ dcrust=$$( [ -f /.dockerenv ] && echo "" || echo "docker-compose exec rust") dcpandoc=$$( [ -f /.dockerenv ] && echo "" || echo "docker-compose exec pandoc") .PHONY: it -it: fmt build test ## Peform common targets +it: fmt target/debug test ## Perform common targets .PHONY: help help: ## Displays this list of targets with descriptions @grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(firstword $(MAKEFILE_LIST)) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}' .PHONY: setup -setup: dc-build cargo-deps build ## Setup the local environment +setup: dc-build cargo-deps ## Set up the local environment .PHONY: dc-build dc-build: ## Build the local dev image @@ -23,8 +23,7 @@ up: ## Bring up the containers cargo-deps: up ## Reinstall cargo dependencies ${dcrust} cargo update -.PHONY: build -build: up ## Reinstall cargo dependencies +target/debug: up src ## Compile ${dcrust} cargo build .PHONY: rust @@ -36,11 +35,11 @@ pandoc: up ## Enter an interactive shell into the pandoc container ${dcpandoc} bash .PHONY: serve -serve: up ## Serve the compiled application +serve: up target/debug ## Serve the compiled application ${dcpandoc} target/debug/md-to-pdf .PHONY: fmt -fmt: ## Format the rust code +fmt: up ## Format the rust code ${dcrust} cargo fmt .PHONY: test diff --git a/Rocket.toml b/Rocket.toml index 4f3cd1b..e67a012 100644 --- a/Rocket.toml +++ b/Rocket.toml @@ -1,2 +1,5 @@ [development] address = "0.0.0.0" + +[global.limits] +forms = 131072 diff --git a/docker-compose.yml b/docker-compose.yml index ecc0cc4..4190b9e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,6 +5,7 @@ services: - ./:/workdir working_dir: /workdir tty: true + pandoc: build: context: . diff --git a/src/main.rs b/src/main.rs index 2e3f3b9..4966d8b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,34 +42,34 @@ struct ConvertForm { } #[derive(Debug)] -enum PandocError { +enum ConvertError { Output(Output), IO(io::Error), } -impl From for PandocError { - fn from(err: io::Error) -> PandocError { - PandocError::IO(err) +impl From for ConvertError { + fn from(err: io::Error) -> ConvertError { + ConvertError::IO(err) } } -impl<'r> Responder<'r> for PandocError { +impl<'r> Responder<'r> for ConvertError { fn respond_to(self, _: &Request) -> response::Result<'r> { let mut builder = Response::build(); match self { - PandocError::Output(output) => builder + ConvertError::Output(output) => builder .header(ContentType::Plain) .sized_body(io::Cursor::new(output.stderr)) .status(Status::BadRequest), - PandocError::IO(_) => builder.status(Status::InternalServerError), + ConvertError::IO(_) => builder.status(Status::InternalServerError), }; builder.ok() } } -#[post("/", data = "")] -fn pandoc(convert: Form) -> Result { +#[post("/", data = "
")] +fn convert(form: Form) -> Result { let mut pandoc_builder = Command::new("pandoc"); // Pandoc can not perform PDF conversion to STDOUT, so we need a temp file @@ -79,7 +79,7 @@ fn pandoc(convert: Form) -> Result { // - Rocket will set the correct Content-Type response header .suffix(".pdf") .tempfile() - .map_err(PandocError::IO)? + .map_err(ConvertError::IO)? .into_temp_path(); let pdf_path = pdf_temp_path .to_str() @@ -88,7 +88,7 @@ fn pandoc(convert: Form) -> Result { pandoc_builder.arg( "--pdf-engine=".to_owned() - + convert + + form .engine .as_ref() .unwrap_or(&PdfEngine::Weasyprint) @@ -98,15 +98,15 @@ fn pandoc(convert: Form) -> Result { // Declare outside of the if block to keep the file around until the end of this function if needed let mut css_file; - if convert.css.is_some() { + if form.css.is_some() { css_file = Builder::new() // Necessary for weasyprint to recognize it as a proper stylesheet .suffix(".css") .tempfile() - .map_err(PandocError::IO)?; + .map_err(ConvertError::IO)?; css_file - .write_all(convert.css.as_ref().unwrap().as_bytes()) - .map_err(PandocError::IO)?; + .write_all(form.css.as_ref().unwrap().as_bytes()) + .map_err(ConvertError::IO)?; pandoc_builder.arg("--css=".to_owned() + css_file.path().to_str().unwrap()); } @@ -116,34 +116,30 @@ fn pandoc(convert: Form) -> Result { pandoc_builder.stdout(Stdio::piped()).stderr(Stdio::piped()); - let mut pandoc_process = pandoc_builder.spawn().map_err(PandocError::IO)?; + let mut pandoc_process = pandoc_builder.spawn().map_err(ConvertError::IO)?; { let pandoc_stdin = pandoc_process.stdin.as_mut().unwrap(); pandoc_stdin - .write_all(convert.markdown.as_bytes()) - .map_err(PandocError::IO)?; + .write_all(form.markdown.as_bytes()) + .map_err(ConvertError::IO)?; } - let output = pandoc_process.wait_with_output().map_err(PandocError::IO)?; + let output = pandoc_process + .wait_with_output() + .map_err(ConvertError::IO)?; debug!("{:?}", output); if !output.status.success() { - return Err(PandocError::Output(output)); + return Err(ConvertError::Output(output)); } - NamedFile::open(Path::new(pdf_path)).map_err(PandocError::IO) + NamedFile::open(Path::new(pdf_path)).map_err(ConvertError::IO) } fn main() { - // Heroku compatibility - match std::env::var("PORT") { - Ok(p) => std::env::set_var("ROCKET_PORT", p), - Err(_e) => (), - } - rocket::ignite() - .mount("/", routes![pandoc]) + .mount("/", routes![convert]) .mount("/", StaticFiles::from("static")) .launch(); }