From 80ef93f20fc9db41cc944a86096115c08d523f82 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Sat, 20 Jun 2020 12:50:22 -0700 Subject: [PATCH] Start of rewrite from warp->rocket. --- Cargo.lock | 362 +++++++++++++++++++++++++++++++++-- Cargo.toml | 1 + Rocket.toml | 3 + react-slideshow/package.json | 2 +- src/lib.rs | 7 +- src/library.rs | 31 ++- src/main.rs | 10 +- src/rweb.rs | 79 ++++++++ src/web.rs | 16 +- 9 files changed, 458 insertions(+), 53 deletions(-) create mode 100644 Rocket.toml create mode 100644 src/rweb.rs diff --git a/Cargo.lock b/Cargo.lock index 90fe71a..326de74 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,61 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5d2e7343e7fc9de883d1b0341e0b13970f764c14101234857d2ddafa1cb1cac2" +[[package]] +name = "aead" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cf01b9b56e767bb57b94ebf91a58b338002963785cdd7013e21c0d4679471e4" +dependencies = [ + "generic-array", +] + +[[package]] +name = "aes" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54eb1d8fe354e5fc611daf4f2ea97dd45a765f4f1e4512306ec183ae2e8f20c9" +dependencies = [ + "aes-soft", + "aesni", + "block-cipher-trait", +] + +[[package]] +name = "aes-gcm" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "834a6bda386024dbb7c8fc51322856c10ffe69559f972261c868485f5759c638" +dependencies = [ + "aead", + "aes", + "block-cipher-trait", + "ghash", + "subtle 2.2.3", + "zeroize", +] + +[[package]] +name = "aes-soft" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfd7e7ae3f9a1fb5c03b389fc6bb9a51400d0c13053f0dca698c832bfd893a0d" +dependencies = [ + "block-cipher-trait", + "byteorder 1.3.4", + "opaque-debug", +] + +[[package]] +name = "aesni" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f70a6b5f971e473091ab7cfb5ffac6cde81666c4556751d8d5620ead8abf100" +dependencies = [ + "block-cipher-trait", + "opaque-debug", +] + [[package]] name = "aho-corasick" version = "0.7.8" @@ -93,6 +148,16 @@ dependencies = [ "libc", ] +[[package]] +name = "base64" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643" +dependencies = [ + "byteorder 1.3.4", + "safemem", +] + [[package]] name = "base64" version = "0.10.1" @@ -108,6 +173,12 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7" +[[package]] +name = "base64" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e223af0dc48c96d4f8342ec01a4974f139df863896b316681efd36742f22cc67" + [[package]] name = "bindgen" version = "0.49.4" @@ -160,6 +231,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "block-cipher-trait" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c924d49bd09e7c06003acda26cd9742e796e34282ec6c1189404dee0c1f4774" +dependencies = [ + "generic-array", +] + [[package]] name = "block-padding" version = "0.1.5" @@ -349,6 +429,22 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" +[[package]] +name = "cookie" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5795cda0897252e34380a27baf884c53aa7ad9990329cdad96d4c5d027015d44" +dependencies = [ + "aes-gcm", + "base64 0.12.2", + "hkdf", + "hmac", + "percent-encoding 2.1.0", + "rand 0.7.3", + "sha2", + "time", +] + [[package]] name = "core-foundation" version = "0.6.4" @@ -480,7 +576,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4434400df11d95d556bac068ddfedd482915eb18fe8bea89bc80b6e4b1c179e5" dependencies = [ "generic-array", - "subtle", + "subtle 1.0.0", ] [[package]] @@ -524,6 +620,38 @@ dependencies = [ "byteorder 1.3.4", ] +[[package]] +name = "devise" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74e04ba2d03c5fa0d954c061fc8c9c288badadffc272ebb87679a89846de3ed3" +dependencies = [ + "devise_codegen", + "devise_core", +] + +[[package]] +name = "devise_codegen" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "066ceb7928ca93a9bedc6d0e612a8a0424048b0ab1f75971b203d01420c055d7" +dependencies = [ + "devise_core", + "quote 0.6.13", +] + +[[package]] +name = "devise_core" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf41c59b22b5e3ec0ea55c7847e5f358d340f3a8d6d53a5cf4f1564967f96487" +dependencies = [ + "bitflags", + "proc-macro2 0.4.30", + "quote 0.6.13", + "syn 0.15.44", +] + [[package]] name = "digest" version = "0.8.1" @@ -638,7 +766,7 @@ checksum = "405b19947fc6a4a2c40bb4b47a220d7feba220c888aa160f4ad5c1c673f9062e" dependencies = [ "proc-macro2 1.0.8", "quote 1.0.2", - "syn", + "syn 1.0.14", ] [[package]] @@ -721,7 +849,7 @@ dependencies = [ "proc-macro-hack", "proc-macro2 1.0.8", "quote 1.0.2", - "syn", + "syn 1.0.14", ] [[package]] @@ -788,6 +916,15 @@ dependencies = [ "wasi", ] +[[package]] +name = "ghash" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f0930ed19a7184089ea46d2fedead2f6dc2b674c5db4276b7da336c7cd83252" +dependencies = [ + "polyval", +] + [[package]] name = "gif" version = "0.10.3" @@ -845,7 +982,7 @@ dependencies = [ "proc-macro2 1.0.8", "quote 1.0.2", "serde_derive_internals", - "syn", + "syn 1.0.14", ] [[package]] @@ -944,6 +1081,16 @@ dependencies = [ "sha2", ] +[[package]] +name = "hkdf" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fa08a006102488bd9cd5b8013aabe84955cf5ae22e304c2caf655b633aefae3" +dependencies = [ + "digest", + "hmac", +] + [[package]] name = "hmac" version = "0.7.1" @@ -1013,6 +1160,25 @@ dependencies = [ "quick-error", ] +[[package]] +name = "hyper" +version = "0.10.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a0652d9a2609a968c14be1a9ea00bf4b1d64e2e1f53a1b51b6fff3a6e829273" +dependencies = [ + "base64 0.9.3", + "httparse", + "language-tags", + "log 0.3.9", + "mime 0.2.6", + "num_cpus", + "time", + "traitobject", + "typeable", + "unicase 1.4.2", + "url 1.7.2", +] + [[package]] name = "hyper" version = "0.12.35" @@ -1271,6 +1437,12 @@ dependencies = [ "winapi-build", ] +[[package]] +name = "language-tags" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a" + [[package]] name = "lazy_static" version = "0.2.11" @@ -1757,6 +1929,28 @@ dependencies = [ "winapi 0.3.8", ] +[[package]] +name = "pear" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5320f212db967792b67cfe12bd469d08afd6318a249bd917d5c19bc92200ab8a" +dependencies = [ + "pear_codegen", +] + +[[package]] +name = "pear_codegen" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfc1c836fdc3d1ef87c348b237b5b5c4dff922156fb2d968f57734f9669768ca" +dependencies = [ + "proc-macro2 0.4.30", + "quote 0.6.13", + "syn 0.15.44", + "version_check 0.9.1", + "yansi", +] + [[package]] name = "peeking_take_while" version = "0.1.2" @@ -1833,6 +2027,7 @@ dependencies = [ "prometheus", "regex", "reqwest", + "rocket", "rocksdb", "rust-embed", "serde", @@ -1862,7 +2057,7 @@ checksum = "385322a45f2ecf3410c68d2a549a4a2685e8051d0f278e39743ff4e451cb9b3f" dependencies = [ "proc-macro2 1.0.8", "quote 1.0.2", - "syn", + "syn 1.0.14", ] [[package]] @@ -1907,6 +2102,16 @@ dependencies = [ "inflate", ] +[[package]] +name = "polyval" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ec3341498978de3bfd12d1b22f1af1de22818f5473a11e8a6ef997989e3a212" +dependencies = [ + "cfg-if", + "universal-hash", +] + [[package]] name = "ppv-lite86" version = "0.2.6" @@ -1923,7 +2128,7 @@ dependencies = [ "proc-macro2 1.0.8", "quote 1.0.2", "rustversion", - "syn", + "syn 1.0.14", ] [[package]] @@ -1935,7 +2140,7 @@ dependencies = [ "proc-macro2 1.0.8", "quote 1.0.2", "rustversion", - "syn", + "syn 1.0.14", "syn-mid", ] @@ -1947,7 +2152,7 @@ checksum = "ecd45702f76d6d3c75a80564378ae228a85f0b59d2f3ed43c91b4a69eb2ebfc5" dependencies = [ "proc-macro2 1.0.8", "quote 1.0.2", - "syn", + "syn 1.0.14", ] [[package]] @@ -2354,6 +2559,59 @@ dependencies = [ "winapi 0.3.8", ] +[[package]] +name = "rocket" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6130967b369cfb8411b0b73e96fcba1229c32a9cc6f295d144f879bfced13c6e" +dependencies = [ + "atty", + "base64 0.12.2", + "log 0.4.8", + "memchr", + "num_cpus", + "pear", + "rocket_codegen", + "rocket_http", + "state", + "time", + "toml", + "version_check 0.9.1", + "yansi", +] + +[[package]] +name = "rocket_codegen" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb852e6da168fb948a8f2b798ba2e2f0e4fc860eae0efa9cf2bf0f5466bb0425" +dependencies = [ + "devise", + "glob", + "indexmap", + "quote 0.6.13", + "rocket_http", + "version_check 0.9.1", + "yansi", +] + +[[package]] +name = "rocket_http" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aff5a5480175f2f553a876b251e9350c74196128806d176da3a51c82aab5428" +dependencies = [ + "cookie", + "hyper 0.10.16", + "indexmap", + "pear", + "percent-encoding 1.0.1", + "smallvec 1.2.0", + "state", + "time", + "unicode-xid 0.1.0", +] + [[package]] name = "rocksdb" version = "0.13.0" @@ -2477,7 +2735,7 @@ checksum = "50212d0e652f580e6d297537c31237d4b2f4497e5912eebe25fde97ac06a51df" dependencies = [ "quote 1.0.2", "rust-embed-utils", - "syn", + "syn 1.0.14", "walkdir", ] @@ -2547,7 +2805,7 @@ checksum = "b3bba175698996010c4f6dce5e7f173b6eb781fce25d2cfc45e27091ce0b79f6" dependencies = [ "proc-macro2 1.0.8", "quote 1.0.2", - "syn", + "syn 1.0.14", ] [[package]] @@ -2668,7 +2926,7 @@ checksum = "128f9e303a5a29922045a830221b8f78ec74a5f544944f3d5984f8ec3895ef64" dependencies = [ "proc-macro2 1.0.8", "quote 1.0.2", - "syn", + "syn 1.0.14", ] [[package]] @@ -2679,7 +2937,7 @@ checksum = "1dbab34ca63057a1f15280bdf3c39f2b1eb1b54c17e98360e511637aef7418c6" dependencies = [ "proc-macro2 1.0.8", "quote 1.0.2", - "syn", + "syn 1.0.14", ] [[package]] @@ -2719,9 +2977,9 @@ dependencies = [ [[package]] name = "sha2" -version = "0.8.1" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "27044adfd2e1f077f649f59deb9490d3941d674002f7d062870a60ebe9bd47a0" +checksum = "a256f46ea78a0c0d9ff00077504903ac881a1dafdc20da66545699e7776b3e69" dependencies = [ "block-buffer", "digest", @@ -2796,6 +3054,12 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" +[[package]] +name = "state" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7345c971d1ef21ffdbd103a75990a15eb03604fc8b8852ca8cb418ee1a099028" + [[package]] name = "stb_image" version = "0.2.2" @@ -2855,7 +3119,7 @@ dependencies = [ "proc-macro-error", "proc-macro2 1.0.8", "quote 1.0.2", - "syn", + "syn 1.0.14", ] [[package]] @@ -2864,6 +3128,23 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee" +[[package]] +name = "subtle" +version = "2.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "502d53007c02d7605a05df1c1a73ee436952781653da5d0bf57ad608f66932c1" + +[[package]] +name = "syn" +version = "0.15.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" +dependencies = [ + "proc-macro2 0.4.30", + "quote 0.6.13", + "unicode-xid 0.1.0", +] + [[package]] name = "syn" version = "1.0.14" @@ -2883,7 +3164,7 @@ checksum = "7be3539f6c128a931cf19dcee741c1af532c7fd387baa739c03dd2e96479338a" dependencies = [ "proc-macro2 1.0.8", "quote 1.0.2", - "syn", + "syn 1.0.14", ] [[package]] @@ -3301,12 +3582,27 @@ dependencies = [ "tokio 0.2.11", ] +[[package]] +name = "toml" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f" +dependencies = [ + "serde", +] + [[package]] name = "tower-service" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e987b6bf443f4b5b3b6f38704195592cca41c5bb7aedd3c3693c7081f8289860" +[[package]] +name = "traitobject" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079" + [[package]] name = "try-lock" version = "0.2.2" @@ -3341,6 +3637,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "typeable" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1410f6f91f21d1612654e7cc69193b0334f909dcf2c790c4826254fbb86f8887" + [[package]] name = "typenum" version = "1.11.2" @@ -3407,6 +3709,16 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" +[[package]] +name = "universal-hash" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df0c900f2f9b4116803415878ff48b63da9edb268668e08cf9292d7503114a01" +dependencies = [ + "generic-array", + "subtle 2.2.3", +] + [[package]] name = "unreachable" version = "1.0.0" @@ -3573,7 +3885,7 @@ dependencies = [ "log 0.4.8", "proc-macro2 1.0.8", "quote 1.0.2", - "syn", + "syn 1.0.14", "wasm-bindgen-shared", ] @@ -3607,7 +3919,7 @@ checksum = "e85031354f25eaebe78bb7db1c3d86140312a911a106b2e29f9cc440ce3e7668" dependencies = [ "proc-macro2 1.0.8", "quote 1.0.2", - "syn", + "syn 1.0.14", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -3629,7 +3941,7 @@ dependencies = [ "log 0.4.8", "proc-macro2 1.0.8", "quote 1.0.2", - "syn", + "syn 1.0.14", "wasm-bindgen-backend", "weedle", ] @@ -3753,6 +4065,12 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b07db065a5cf61a7e4ba64f29e67db906fb1787316516c4e6e5ff0fea1efcd8a" +[[package]] +name = "yansi" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fc79f4a1e39857fc00c3f662cbf2651c771f00e9c15fe2abc341806bd46bd71" + [[package]] name = "yup-oauth2" version = "3.1.1" @@ -3776,3 +4094,9 @@ dependencies = [ "tokio-timer", "url 1.7.2", ] + +[[package]] +name = "zeroize" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3cbac2ed2ba24cc90f5e06485ac8c7c1e5449fe8911aef4d8877218af021a5b8" diff --git a/Cargo.toml b/Cargo.toml index 339fa88..79cd77d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ rocksdb = "0.13.0" jpeg-decoder = "0.1.18" imageutils = { git = "https://git.z.xinu.tv/wathiede/imageutils" } cacher = { path = "../cacher" } +rocket = "0.4.5" [dependencies.prometheus] features = ["process"] diff --git a/Rocket.toml b/Rocket.toml new file mode 100644 index 0000000..a770b61 --- /dev/null +++ b/Rocket.toml @@ -0,0 +1,3 @@ +[development] +address = "0.0.0.0" +port = 8000 diff --git a/react-slideshow/package.json b/react-slideshow/package.json index d3b3d34..233feb3 100644 --- a/react-slideshow/package.json +++ b/react-slideshow/package.json @@ -1,7 +1,7 @@ { "name": "react-slideshow", "version": "0.1.0", - "proxy": "http://localhost:4000", + "proxy": "http://sky.h:8000", "private": true, "dependencies": { "@testing-library/jest-dom": "^4.2.4", diff --git a/src/lib.rs b/src/lib.rs index 07f3391..e07d4d7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,7 @@ +#![feature(proc_macro_hygiene, decl_macro)] + +#[macro_use] +extern crate rocket; + pub mod library; -pub mod web; +pub mod rweb; diff --git a/src/library.rs b/src/library.rs index 4858167..df132aa 100644 --- a/src/library.rs +++ b/src/library.rs @@ -22,21 +22,18 @@ use rocksdb::DB; const LIBRARY_GENERATION: &'static str = "14"; #[derive(Clone)] -pub struct Library -where - C: Cacher, -{ +pub struct Library { root: PathBuf, originals_dir: PathBuf, cache_db: Arc, - image_cache: C, + image_cache: Arc>, } -impl Library -where - C: Cacher, -{ - pub fn new(root: PathBuf, image_cache: C) -> Result, Box> { +impl Library { + pub fn new( + root: PathBuf, + image_cache: Arc>, + ) -> Result> { let db = DB::open_default(root.join("cache"))?; let cache_db = Arc::new(db); let lib = Library { @@ -60,7 +57,7 @@ where } // Removes all data in the database from older schema. pub fn clean_db(&self) -> Result { - Library::::gc(LIBRARY_GENERATION, &self.cache_db) + Library::gc(LIBRARY_GENERATION, &self.cache_db) } fn gc(generation: &str, db: &DB) -> Result { let gen = format!("{}/", generation); @@ -193,22 +190,16 @@ where dimensions: (Option, Option), fill: bool, ) -> Option> { - fn cache_key( - media_items_id: &str, - dimensions: (Option, Option), - ) -> String { + fn cache_key(media_items_id: &str, dimensions: (Option, Option)) -> String { let dim = match dimensions { (Some(w), Some(h)) => format!("-w={}-h={}", w, h), (Some(w), None) => format!("-w={}", w), (None, Some(h)) => format!("-h={}", h), (None, None) => "".to_string(), }; - Library::::generational_key( - LIBRARY_GENERATION, - &format!("{}{}", media_items_id, dim), - ) + Library::generational_key(LIBRARY_GENERATION, &format!("{}{}", media_items_id, dim)) } - let key = cache_key::(media_items_id, dimensions); + let key = cache_key(media_items_id, dimensions); let db = self.cache_db.clone(); match db.get(key.as_bytes()) { // Cache hit, return bytes as-is. diff --git a/src/main.rs b/src/main.rs index 6636d36..73f5645 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,11 @@ use std::collections::HashMap; use std::error::Error; use std::net::SocketAddr; use std::path::PathBuf; +use std::sync::Arc; use std::thread; use std::time; -use cacher::S3Cacher; +use cacher::{Cacher, S3Cacher}; use google_api_auth; use google_photoslibrary1 as photos; use hexihasher; @@ -17,7 +18,7 @@ use structopt::StructOpt; use yup_oauth2::{Authenticator, InstalledFlow}; use photosync::library::Library; -use photosync::web; +use photosync::rweb; fn parse_duration(src: &str) -> Result { let secs = str::parse::(src)?; @@ -310,7 +311,7 @@ fn background_sync( } pub fn serve(addr: SocketAddr, lib: Library) -> Result<(), Box> { - web::run(addr, lib) + rweb::run(addr, lib) } fn main() -> Result<(), Box> { @@ -321,7 +322,8 @@ fn main() -> Result<(), Box> { .init() .unwrap(); debug!("opt: {:?}", opt); - let image_cache = S3Cacher::new("photosync")?; + let image_cache: Box = Box::new(S3Cacher::new("photosync".to_string())?); + let image_cache = Arc::new(image_cache); match opt.cmd { Command::ListAlbums { auth, title_filter } => { let client = new_client(&auth.credentials, &auth.token_cache)?; diff --git a/src/rweb.rs b/src/rweb.rs new file mode 100644 index 0000000..1902692 --- /dev/null +++ b/src/rweb.rs @@ -0,0 +1,79 @@ +use std::error::Error; +use std::net::SocketAddr; +use std::path::PathBuf; + +use cacher::Cacher; +use rocket::http::ContentType; +use rocket::response::content::Html; +use rocket::response::status::NotFound; +use rocket::response::Content; +use rocket::{Request, State}; +use rust_embed::RustEmbed; + +use crate::library::Library; + +#[derive(RustEmbed)] +#[folder = "react-slideshow/build/"] +struct Asset; + +/* +fn embedz() -> Result { + let mut w = Vec::new(); + write!( + w, + r#""# + ) + .unwrap(); + for path in Asset::iter() { + write!( + w, + r#"
sizepath
{0}{1}"#, + Asset::get(&path).unwrap().len(), + path + ) + .unwrap(); + } + Ok(warp::http::Response::builder() + .header("Content-Type", "text/html") + .body(w)) +} +*/ + +#[get("/")] +fn index(lib: State) -> Result>, NotFound> { + dbg!("index called"); + file("index.html", lib) +} + +#[get("/")] +fn path(path: PathBuf, lib: State) -> Result>, NotFound> { + dbg!(&path); + let path = path.to_str().unwrap(); + let path = if path.ends_with("/") { + format!("{}index.html", path.to_string()) + } else { + path.to_string() + }; + file(&path, lib) +} + +fn file(path: &str, lib: State) -> Result>, NotFound> { + match Asset::get(path) { + Some(bytes) => { + dbg!(path); + let mime = mime_guess::from_path(path).first_or_octet_stream(); + let ct = ContentType::parse_flexible(mime.essence_str()).unwrap_or(ContentType::Binary); + + Ok(Content(ct, bytes.into())) + } + None => Err(NotFound(path.to_string())), + } +} + +pub fn run(addr: SocketAddr, lib: Library) -> Result<(), Box> { + rocket::ignite() + .manage(lib) + .mount("/", routes![index, path]) + .launch(); + Ok(()) +} diff --git a/src/web.rs b/src/web.rs index fcac90d..67dbb38 100644 --- a/src/web.rs +++ b/src/web.rs @@ -54,7 +54,7 @@ fn index(path: warp::path::FullPath) -> Result(lib: Library) -> Result { +fn albums(lib: Library) -> Result { let albums = lib.albums().map_err(|e| { warn!("Couldn't find albums: {}", e); warp::reject::not_found() @@ -62,7 +62,7 @@ fn albums(lib: Library) -> Result(lib: Library, id: String) -> Result { +fn album(lib: Library, id: String) -> Result { let album = lib.album(&id).map_err(|e| { warn!("Couldn't find album {}: {}", id, e); warp::reject::not_found() @@ -77,8 +77,8 @@ struct ImageParams { fill: Option, } -fn image( - lib: Library, +fn image( + lib: Library, media_items_id: String, params: ImageParams, ) -> Result { @@ -123,24 +123,24 @@ fn embedz() -> Result { .body(w)) } -pub fn run(addr: SocketAddr, lib: Library) -> Result<(), Box> { +pub fn run(addr: SocketAddr, lib: Library) -> Result<(), Box> { let lib = warp::any().map(move || lib.clone()); let index = warp::get2().and(warp::path::full()).and_then(index); - let albums = warp::path("albums").and(lib.clone()).and_then(index); + let albums = warp::path("albums").and(lib.clone()).and_then(albums); let embedz = warp::path("embedz").and_then(embedz); let album = warp::path("album") .and(lib.clone()) .and(warp::path::param()) - .and_then(index); + .and_then(album); let image = warp::path("image") .and(lib.clone()) .and(warp::path::param()) .and(warp::query::()) - .and_then(index); + .and_then(image); let api = albums.or(album).or(image); let api = warp::path("api").and(api);