diff --git a/.gitignore b/.gitignore index ea8c4bf..bf670f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/working diff --git a/Cargo.lock b/Cargo.lock index e20253c..e5ba1e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18,6 +18,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "argon2" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ee15f9f5e0f846cab0aa13d5dd1edbc49331dcae95a2e43b84ccc0b406dcda4" +dependencies = [ + "blake2", + "password-hash", +] + [[package]] name = "atty" version = "0.2.14" @@ -36,10 +46,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] -name = "base64" -version = "0.13.0" +name = "base64ct" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" +checksum = "d0d27fb6b6f1e43147af148af49d49329413ba781aa0d5e10979831c210173b5" [[package]] name = "bitflags" @@ -47,6 +57,17 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "blake2" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a5720225ef5daecf08657f23791354e1685a8c91a4c60c7f3d3b2892f978f4" +dependencies = [ + "crypto-mac", + "digest 0.9.0", + "opaque-debug 0.3.0", +] + [[package]] name = "block-buffer" version = "0.7.3" @@ -59,15 +80,6 @@ dependencies = [ "generic-array 0.12.4", ] -[[package]] -name = "block-buffer" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" -dependencies = [ - "generic-array 0.14.4", -] - [[package]] name = "block-padding" version = "0.1.5" @@ -86,16 +98,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "buf_redux" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b953a6887648bb07a535631f2bc00fbdb2a2216f135552cb3f534ed136b9c07f" -dependencies = [ - "memchr", - "safemem", -] - [[package]] name = "byte-tools" version = "0.3.1" @@ -118,12 +120,21 @@ checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" name = "caty_blog" version = "0.1.0" dependencies = [ + "argon2", "clap", + "data-encoding", "glob", + "hyper", + "log", "pulldown-cmark", + "rand", + "routerify", + "serde", + "simplelog", + "tempfile", "tera", "tokio", - "warp", + "toml", ] [[package]] @@ -170,12 +181,6 @@ dependencies = [ "vec_map", ] -[[package]] -name = "cpuid-bool" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634" - [[package]] name = "crossbeam-utils" version = "0.8.3" @@ -187,6 +192,22 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "crypto-mac" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b584a330336237c1eecd3e94266efb216c56ed91225d634cb2991c5f3fd1aeab" +dependencies = [ + "generic-array 0.14.4", + "subtle", +] + +[[package]] +name = "data-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ee2393c4a91429dffb4bedf19f4d6abf27d8a732c8ce4980305d782e5426d57" + [[package]] name = "deunicode" version = "0.4.3" @@ -223,30 +244,6 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" -[[package]] -name = "form_urlencoded" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191" -dependencies = [ - "matches", - "percent-encoding", -] - -[[package]] -name = "futures" -version = "0.3.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f55667319111d593ba876406af7c409c0ebb44dc4be6132a783ccf163ea14c1" -dependencies = [ - "futures-channel", - "futures-core", - "futures-io", - "futures-sink", - "futures-task", - "futures-util", -] - [[package]] name = "futures-channel" version = "0.3.13" @@ -254,7 +251,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c2dd2df839b57db9ab69c2c9d8f3e8c81984781937fe2807dc6dcf3b2ad2939" dependencies = [ "futures-core", - "futures-sink", ] [[package]] @@ -263,12 +259,6 @@ version = "0.3.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "15496a72fabf0e62bdc3df11a59a3787429221dd0710ba8ef163d6f7a9112c94" -[[package]] -name = "futures-io" -version = "0.3.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d71c2c65c57704c32f5241c1223167c2c3294fd34ac020c807ddbe6db287ba59" - [[package]] name = "futures-sink" version = "0.3.13" @@ -288,11 +278,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1812c7ab8aedf8d6f2701a43e1243acdbcc2b36ab26e2ad421eb99ac963d96d1" dependencies = [ "futures-core", - "futures-sink", "futures-task", "pin-project-lite", "pin-utils", - "slab", ] [[package]] @@ -314,17 +302,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "getrandom" -version = "0.1.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" -dependencies = [ - "cfg-if", - "libc", - "wasi 0.9.0+wasi-snapshot-preview1", -] - [[package]] name = "getrandom" version = "0.2.2" @@ -333,7 +310,7 @@ checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" dependencies = [ "cfg-if", "libc", - "wasi 0.10.2+wasi-snapshot-preview1", + "wasi", ] [[package]] @@ -368,9 +345,9 @@ dependencies = [ [[package]] name = "h2" -version = "0.3.1" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d832b01df74254fe364568d6ddc294443f61cbec82816b60904303af87efae78" +checksum = "fc018e188373e2777d0ef2467ebff62a08e66c3f5857b23c8fbec3018210dc00" dependencies = [ "bytes", "fnv", @@ -391,31 +368,6 @@ version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" -[[package]] -name = "headers" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0b7591fb62902706ae8e7aaff416b1b0fa2c0fd0878b46dc13baa3712d8a855" -dependencies = [ - "base64", - "bitflags", - "bytes", - "headers-core", - "http", - "mime", - "sha-1 0.9.4", - "time", -] - -[[package]] -name = "headers-core" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429" -dependencies = [ - "http", -] - [[package]] name = "hermit-abi" version = "0.1.18" @@ -467,9 +419,9 @@ checksum = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e" [[package]] name = "hyper" -version = "0.14.4" +version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8e946c2b1349055e0b72ae281b238baf1a3ea7307c7e9f9d64673bdd9c26ac7" +checksum = "8bf09f61b52cfcf4c00de50df88ae423d6c02354e385a86341133b5338630ad1" dependencies = [ "bytes", "futures-channel", @@ -489,17 +441,6 @@ dependencies = [ "want", ] -[[package]] -name = "idna" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89829a5d69c23d348314a7ac337fe39173b61149a9864deabd260983aed48c21" -dependencies = [ - "matches", - "unicode-bidi", - "unicode-normalization", -] - [[package]] name = "ignore" version = "0.4.17" @@ -528,15 +469,6 @@ dependencies = [ "hashbrown", ] -[[package]] -name = "input_buffer" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f97967975f448f1a7ddb12b0bc41069d09ed6a1c161a92687e057325db35d413" -dependencies = [ - "bytes", -] - [[package]] name = "instant" version = "0.1.9" @@ -560,15 +492,15 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.90" +version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba4aede83fc3617411dc6993bc8c70919750c1c257c6ca6a502aed6e0e2394ae" +checksum = "56d855069fafbb9b344c0f962150cd2c1187975cb1c22c1522c240d8c4986714" [[package]] name = "lock_api" -version = "0.4.2" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd96ffd135b2fd7b973ac026d28085defbe8983df057ced3eb4f2130b0831312" +checksum = "5a3c91c24eae6777794bb1997ad98bbb87daf92890acab859f7eaa4320333176" dependencies = [ "scopeguard", ] @@ -588,39 +520,17 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" -[[package]] -name = "matches" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" - [[package]] name = "memchr" version = "2.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" -[[package]] -name = "mime" -version = "0.3.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" - -[[package]] -name = "mime_guess" -version = "2.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2684d4c2e97d99848d30b324b00c8fcc7e5c897b7cbb5819b09e7c90e8baf212" -dependencies = [ - "mime", - "unicase", -] - [[package]] name = "mio" -version = "0.7.10" +version = "0.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2182a122f3b7f3f5329cb1972cee089ba2459a0a80a56935e6e674f096f8d839" +checksum = "cf80d3e903b34e0bd7282b218398aec54e082c840d9baf8339e0080a0c542956" dependencies = [ "libc", "log", @@ -631,32 +541,13 @@ dependencies = [ [[package]] name = "miow" -version = "0.3.6" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a33c1b55807fbed163481b5ba66db4b2fa6cde694a5027be10fb724206c5897" +checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21" dependencies = [ - "socket2", "winapi", ] -[[package]] -name = "multipart" -version = "0.17.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d050aeedc89243f5347c3e237e3e13dc76fbe4ae3742a57b94dc14f69acf76d4" -dependencies = [ - "buf_redux", - "httparse", - "log", - "mime", - "mime_guess", - "quick-error", - "rand 0.7.3", - "safemem", - "tempfile", - "twoway", -] - [[package]] name = "ntapi" version = "0.3.6" @@ -747,6 +638,16 @@ dependencies = [ "regex", ] +[[package]] +name = "password-hash" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85d8faea6c018131952a192ee55bd9394c51fc6f63294b668d97636e6f842d40" +dependencies = [ + "base64ct", + "rand_core", +] + [[package]] name = "percent-encoding" version = "2.1.0" @@ -793,23 +694,23 @@ checksum = "54be6e404f5317079812fc8f9f5279de376d8856929e21c184ecf6bbd692a11d" dependencies = [ "maplit", "pest", - "sha-1 0.8.2", + "sha-1", ] [[package]] name = "pin-project" -version = "1.0.5" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96fa8ebb90271c4477f144354485b8068bd8f6b78b428b01ba892ca26caf0b63" +checksum = "bc174859768806e91ae575187ada95c91a29e96a98dc5d2cd9a1fed039501ba6" dependencies = [ "pin-project-internal", ] [[package]] name = "pin-project-internal" -version = "1.0.5" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "758669ae3558c6f74bd2a18b41f7ac0b5a195aea6639d6a9b5e5d1ad5ba24c0b" +checksum = "a490329918e856ed1b083f244e3bfe2d8c4f336407e4ea9e1a9f479ff09049e5" dependencies = [ "proc-macro2", "quote", @@ -836,9 +737,9 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" [[package]] name = "proc-macro2" -version = "1.0.24" +version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec" dependencies = [ "unicode-xid", ] @@ -854,12 +755,6 @@ dependencies = [ "unicase", ] -[[package]] -name = "quick-error" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" - [[package]] name = "quote" version = "1.0.9" @@ -869,19 +764,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "rand" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" -dependencies = [ - "getrandom 0.1.16", - "libc", - "rand_chacha 0.2.2", - "rand_core 0.5.1", - "rand_hc 0.2.0", -] - [[package]] name = "rand" version = "0.8.3" @@ -889,19 +771,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" dependencies = [ "libc", - "rand_chacha 0.3.0", - "rand_core 0.6.2", - "rand_hc 0.3.0", -] - -[[package]] -name = "rand_chacha" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" -dependencies = [ - "ppv-lite86", - "rand_core 0.5.1", + "rand_chacha", + "rand_core", + "rand_hc", ] [[package]] @@ -911,16 +783,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" dependencies = [ "ppv-lite86", - "rand_core 0.6.2", -] - -[[package]] -name = "rand_core" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" -dependencies = [ - "getrandom 0.1.16", + "rand_core", ] [[package]] @@ -929,16 +792,7 @@ version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" dependencies = [ - "getrandom 0.2.2", -] - -[[package]] -name = "rand_hc" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" -dependencies = [ - "rand_core 0.5.1", + "getrandom", ] [[package]] @@ -947,7 +801,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" dependencies = [ - "rand_core 0.6.2", + "rand_core", ] [[package]] @@ -986,16 +840,23 @@ dependencies = [ ] [[package]] -name = "ryu" -version = "1.0.5" +name = "routerify" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" +checksum = "c18f54182904acfbd8082adb04985c59eb037027105631e3a68a5a51de31294d" +dependencies = [ + "http", + "hyper", + "lazy_static", + "percent-encoding", + "regex", +] [[package]] -name = "safemem" -version = "0.3.3" +name = "ryu" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" [[package]] name = "same-file" @@ -1006,12 +867,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "scoped-tls" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" - [[package]] name = "scopeguard" version = "1.1.0" @@ -1020,28 +875,30 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "serde" -version = "1.0.124" +version = "1.0.125" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd761ff957cb2a45fbb9ab3da6512de9de55872866160b23c25f1a841e99d29f" +checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171" +dependencies = [ + "serde_derive", +] [[package]] -name = "serde_json" -version = "1.0.64" +name = "serde_derive" +version = "1.0.125" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79" +checksum = "b093b7a2bb58203b5da3056c05b4ec1fed827dcfdb37347a8841695263b3d06d" dependencies = [ - "itoa", - "ryu", - "serde", + "proc-macro2", + "quote", + "syn", ] [[package]] -name = "serde_urlencoded" -version = "0.7.0" +name = "serde_json" +version = "1.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edfa57a7f8d9c1d260a549e7224100f6c43d43f9103e06dd8b4095a9b2b43ce9" +checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79" dependencies = [ - "form_urlencoded", "itoa", "ryu", "serde", @@ -1053,32 +910,30 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7d94d0bede923b3cea61f3f1ff57ff8cdfd77b400fb8f9998949e0cf04163df" dependencies = [ - "block-buffer 0.7.3", + "block-buffer", "digest 0.8.1", "fake-simd", "opaque-debug 0.2.3", ] [[package]] -name = "sha-1" -version = "0.9.4" +name = "signal-hook-registry" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfebf75d25bd900fd1e7d11501efab59bc846dbc76196839663e6637bba9f25f" +checksum = "16f1d0fef1604ba8f7a073c7e701f213e056707210e9020af4528e0101ce11a6" dependencies = [ - "block-buffer 0.9.0", - "cfg-if", - "cpuid-bool", - "digest 0.9.0", - "opaque-debug 0.3.0", + "libc", ] [[package]] -name = "signal-hook-registry" -version = "1.3.0" +name = "simplelog" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16f1d0fef1604ba8f7a073c7e701f213e056707210e9020af4528e0101ce11a6" +checksum = "59d0fe306a0ced1c88a58042dc22fc2ddd000982c26d75f6aa09a394547c41e0" dependencies = [ - "libc", + "chrono", + "log", + "termcolor", ] [[package]] @@ -1104,11 +959,10 @@ checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" [[package]] name = "socket2" -version = "0.3.19" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "122e570113d28d773067fab24266b66753f6ea915758651696b6e35e49f88d6e" +checksum = "9e3dfc207c526015c632472a77be09cf1b6e46866581aecae5cc38fb4235dea2" dependencies = [ - "cfg-if", "libc", "winapi", ] @@ -1119,11 +973,17 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +[[package]] +name = "subtle" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e81da0851ada1f3e9d4312c704aa4f8806f0f9d69faaf8df2f3464b4a9437c2" + [[package]] name = "syn" -version = "1.0.64" +version = "1.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fd9d1e9976102a03c542daa2eff1b43f9d72306342f3f8b3ed5fb8908195d6f" +checksum = "3ce15dd3ed8aa2f8eeac4716d6ef5ab58b6b9256db41d7e1a0224c2788e8fd87" dependencies = [ "proc-macro2", "quote", @@ -1138,7 +998,7 @@ checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" dependencies = [ "cfg-if", "libc", - "rand 0.8.3", + "rand", "redox_syscall", "remove_dir_all", "winapi", @@ -1158,7 +1018,7 @@ dependencies = [ "percent-encoding", "pest", "pest_derive", - "rand 0.8.3", + "rand", "regex", "serde", "serde_json", @@ -1166,6 +1026,15 @@ dependencies = [ "unic-segment", ] +[[package]] +name = "termcolor" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +dependencies = [ + "winapi-util", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -1194,21 +1063,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "tinyvec" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "317cca572a0e89c3ce0ca1f1bdc9369547fe318a683418e42ac8f59d14701023" -dependencies = [ - "tinyvec_macros", -] - -[[package]] -name = "tinyvec_macros" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" - [[package]] name = "tokio" version = "1.4.0" @@ -1240,30 +1094,6 @@ dependencies = [ "syn", ] -[[package]] -name = "tokio-stream" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e177a5d8c3bf36de9ebe6d58537d8879e964332f93fb3339e43f618c81361af0" -dependencies = [ - "futures-core", - "pin-project-lite", - "tokio", -] - -[[package]] -name = "tokio-tungstenite" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1a5f475f1b9d077ea1017ecbc60890fda8e54942d680ca0b1d2b47cfa2d861b" -dependencies = [ - "futures-util", - "log", - "pin-project", - "tokio", - "tungstenite", -] - [[package]] name = "tokio-util" version = "0.6.5" @@ -1278,6 +1108,15 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" +dependencies = [ + "serde", +] + [[package]] name = "tower-service" version = "0.3.1" @@ -1291,7 +1130,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "01ebdc2bb4498ab1ab5f5b73c5803825e60199229ccba0698170e3be0e7f959f" dependencies = [ "cfg-if", - "log", "pin-project-lite", "tracing-core", ] @@ -1305,50 +1143,12 @@ dependencies = [ "lazy_static", ] -[[package]] -name = "tracing-futures" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97d095ae15e245a057c8e8451bab9b3ee1e1f68e9ba2b4fbc18d0ac5237835f2" -dependencies = [ - "pin-project", - "tracing", -] - [[package]] name = "try-lock" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" -[[package]] -name = "tungstenite" -version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ada8297e8d70872fa9a551d93250a9f407beb9f37ef86494eb20012a2ff7c24" -dependencies = [ - "base64", - "byteorder", - "bytes", - "http", - "httparse", - "input_buffer", - "log", - "rand 0.8.3", - "sha-1 0.9.4", - "url", - "utf-8", -] - -[[package]] -name = "twoway" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59b11b2b5241ba34be09c3cc85a36e56e48f9888862e19cedf23336d35316ed1" -dependencies = [ - "memchr", -] - [[package]] name = "typenum" version = "1.13.0" @@ -1420,24 +1220,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "unicode-bidi" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" -dependencies = [ - "matches", -] - -[[package]] -name = "unicode-normalization" -version = "0.1.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07fbfce1c8a97d547e8b5334978438d9d6ec8c20e38f56d4a4374d181493eaef" -dependencies = [ - "tinyvec", -] - [[package]] name = "unicode-width" version = "0.1.8" @@ -1450,24 +1232,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" -[[package]] -name = "url" -version = "2.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ccd964113622c8e9322cfac19eb1004a07e636c545f325da085d5cdde6f1f8b" -dependencies = [ - "form_urlencoded", - "idna", - "matches", - "percent-encoding", -] - -[[package]] -name = "utf-8" -version = "0.7.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05e42f7c18b8f902290b009cde6d651262f956c98bc51bca4cd1d511c9cd85c7" - [[package]] name = "vec_map" version = "0.8.2" @@ -1482,9 +1246,9 @@ checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" [[package]] name = "walkdir" -version = "2.3.1" +version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d" +checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" dependencies = [ "same-file", "winapi", @@ -1501,42 +1265,6 @@ dependencies = [ "try-lock", ] -[[package]] -name = "warp" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3dafd0aac2818a94a34df0df1100a7356c493d8ede4393875fd0b5c51bb6bc80" -dependencies = [ - "bytes", - "futures", - "headers", - "http", - "hyper", - "log", - "mime", - "mime_guess", - "multipart", - "percent-encoding", - "pin-project", - "scoped-tls", - "serde", - "serde_json", - "serde_urlencoded", - "tokio", - "tokio-stream", - "tokio-tungstenite", - "tokio-util", - "tower-service", - "tracing", - "tracing-futures", -] - -[[package]] -name = "wasi" -version = "0.9.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" - [[package]] name = "wasi" version = "0.10.2+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 194c70f..1671743 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,12 +7,23 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +argon2 = "0.1" clap = "2.33.3" +data-encoding = "2.3.2" glob = "0.3.0" -tera = "1.7.0" +hyper = "0.14" +log = "0.4.14" pulldown-cmark = { version = "0.8", default-features = false, features = ["simd"] } +rand = "0.8" +routerify = "2.0.0" +serde = { version = "1", features = ["derive"] } +simplelog = "0.10.0" +tera = "1.7.0" tokio = { version = "1", features = ["full"] } -warp = "0.3" +toml = "0.5" + +[dev-dependencies] +tempfile = "3" [profile.release] panic = "abort" diff --git a/src/auth.rs b/src/auth.rs new file mode 100644 index 0000000..7b871a6 --- /dev/null +++ b/src/auth.rs @@ -0,0 +1,63 @@ +/// TODO Document +pub fn generate_secret(len: usize) -> Result> { + use rand::{thread_rng, Rng, + distributions::Alphanumeric}; + + if len < 32 { + Err(From::from("Random passwords shorter than 32ch are useless")) + } else { + let pass: String = thread_rng() + .sample_iter(&Alphanumeric) + .take(len) + .map(char::from) + .collect(); + Ok(pass) + } +} + +/// TODO Document +pub fn generate_argon2_phc(secret: &str) -> Result> { + use rand::rngs::OsRng; + use argon2::{Argon2, password_hash::{SaltString, PasswordHasher}}; + + let secret = secret.as_bytes(); + let salt = SaltString::generate(&mut OsRng); + let argon2 = Argon2::default(); + let argon2_phc: Result>; + if let Ok(phc) = argon2.hash_password_simple(secret, salt.as_ref()) { + argon2_phc = Ok(phc.to_string()); + } else { + argon2_phc = Err(From::from("Failed to hash password")); + } + + argon2_phc +} + +pub use data_encoding::BASE32_NOPAD; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn check_secret_len() { + const SECLEN: usize = 32; + let secret = generate_secret(SECLEN).unwrap(); + assert_eq!(SECLEN, secret.len()) + } + + #[test] + fn check_short_secret() { + const SECLEN: usize = 12; + let secret = generate_secret(SECLEN); + assert!(secret.is_err()) + } + + #[test] + fn check_argon2_hasher() { + const SECLEN: usize = 32; + let secret = generate_secret(SECLEN).unwrap(); + let phc = generate_argon2_phc(&secret); + assert!(phc.is_ok()) + } +} diff --git a/src/bin/caty-blog.rs b/src/bin/caty-blog.rs index 0af6089..c4914be 100644 --- a/src/bin/caty-blog.rs +++ b/src/bin/caty-blog.rs @@ -1,25 +1,25 @@ -use caty_blog::*; -use warp::Filter; +use std::net::SocketAddr; +use std::sync::Arc; +use caty_blog::*; +use hyper::Server; +use routerify::RouterService; #[tokio::main] async fn main() -> Result<(), Box> { - // TODO: Load configuration file defining paths to stuff - // Load into memory as a Struct in an Arc(Mutex(something)) - // This should also include the admin username/hash(password) - - write_main(&render_main())?; - - let static_files = warp::path("static") - .and(warp::fs::dir("webroot/static")); - - let index = warp::get() - .and(warp::path::end()) - .and(warp::fs::file("webroot/index.html")); - - let routes = static_files.or(index); - - warp::serve(routes).run(([127,0,0,1], 3030)).await; + let config = config::load()?; + let app = Arc::new(config); + + // TODO: Configure logging + + let router = routes::router(app.clone()); + let service = RouterService::new(router).unwrap(); + let addr: SocketAddr = "0.0.0.0:9090".parse().unwrap(); + let server = Server::bind(&addr).serve(service); + + if let Err(err) = server.await { + eprintln!("Server error: {}", err) + } Ok(()) } diff --git a/src/common.rs b/src/common.rs new file mode 100644 index 0000000..7c79b45 --- /dev/null +++ b/src/common.rs @@ -0,0 +1,52 @@ +use std::fs::OpenOptions; +use std::io::prelude::*; +use std::path::{Path, PathBuf}; + +use glob::glob; + +#[cfg(target_family = "unix")] +pub fn str_to_ro_file>(content: &str, dest: P) -> Result<(), Box> { + use std::os::unix::fs::OpenOptionsExt; + let mut options = OpenOptions::new(); + options.create(true); + options.write(true); + options.mode(0o600); + let mut ro_file = options.open(dest)?; + ro_file.write_all(content.as_bytes())?; + if !content.ends_with("\n") { + ro_file.write(b"\n")?; + } + Ok(()) +} + +#[cfg(target_family = "windows")] +pub fn str_to_ro_file>(content: &str, dest: P) -> Result<(), Box> { + let mut ro_file = File::create(dest)?; + ro_file.write_all(content.as_bytes())?; + let metadata = secret_file.metadata()?; + let mut perms = metadata.permissions(); + if !content.ends_with("\n") { + ro_file.write(b"\n")?; + } + perms.set_readonly(true); + Ok(()) +} + +pub fn path_matches(pat: &str) -> Result, Box> { + let mut path_vec: Vec = Vec::new(); + let entries = glob(pat)?; + for entry in entries.filter_map(Result::ok) { + path_vec.push(entry); + } + + path_vec.reverse(); + Ok(path_vec) +} + +pub fn slugify(topic: &str) -> String { + let topic = topic + .to_ascii_lowercase() + .replace(char::is_whitespace, "-"); + topic +} + diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..766d024 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,271 @@ +use std::fs::create_dir_all; +use std::{io::BufRead, usize}; +use std::path::Path; + +use clap::{App, AppSettings, Arg, ArgMatches, SubCommand}; + +use serde::{Serialize, Deserialize}; +use toml; + +use super::auth; +use super::common; + +fn args() -> App<'static, 'static> { + App::new("Caty's Blog") + .version("1.0") + .author("Anthony Martinez") + .setting(AppSettings::ArgRequiredElseHelp) + .subcommand(SubCommand::with_name("run") + .about("Run the blog server") + .arg(Arg::with_name("config") + .help("Provides the path to the server configuration file.") + .required(true) + .takes_value(true) + .index(1))) + .subcommand(SubCommand::with_name("new") + .about("Generates a base directory structure and configuration file for a new blog") + ) +} + +/// TODO Document this public function +/// And Include an Example of its Use +pub fn load() -> Result> { + let matches = args().get_matches(); + let config: Result>; + if matches.is_present("run") { + config = runner_config(matches); + } else if matches.is_present("new") { + let reader = std::io::stdin(); + let mut reader = reader.lock(); + let current_path = std::env::current_dir()?; + config = AppConfig::generate(current_path, &mut reader); + } else { + let msg = format!("Unable to load configuration"); + config = Err(From::from(msg)); + } + + config +} + +fn runner_config(m: ArgMatches) -> Result> { + if let Some(run) = m.subcommand_matches("run") { + let value = run.value_of("config").unwrap(); + let config = AppConfig::new(value)?; + Ok(config) + } else { + let msg = format!("Failed to read arguments for 'run' subcommand"); + Err(From::from(msg)) + } +} + +fn get_input(prompt: &str, reader: &mut R) -> Result> { + let mut buf = String::new(); + println!("{}", prompt); + reader.read_line(&mut buf)?; + let buf = String::from(buf + .trim_start_matches(char::is_whitespace) + .trim_end_matches(char::is_whitespace)); + Ok(buf) +} + +fn csv_to_vec(csv: &str) -> Vec { + let val_vec: Vec = csv.split(",") + .map(|s| s + .trim_start_matches(char::is_whitespace) + .trim_end_matches(char::is_whitespace) + .to_string()) + .collect(); + + val_vec +} + +/// TODO Document +#[derive(Debug, Deserialize, Serialize, PartialEq)] +pub struct Blog { + pub name: String, + pub author: String, + pub topics: Vec, +} + +impl Blog { + pub fn new_from_input(reader: &mut R) -> Result> { + let name = get_input("Please enter a name for the blog: ", reader)?; + let author = get_input("Please enter the blog author's name: ", reader)?; + let topics = get_input("Please enter comma-separated blog topics: ", reader)?; + let topics = csv_to_vec(&topics); + let blog = Blog { name, author, topics }; + + Ok(blog) + } +} + +/// TODO Document +#[derive(Debug, Deserialize, Serialize, PartialEq)] +pub struct Credentials { + pub user: String, + pub password: String, + pub token: String, +} + +impl Credentials { + pub fn new_from_input, R: BufRead>(dir: P, reader: &mut R) -> Result> { + let user = get_input("Please enter an username for the blog admin: ", reader)?; + const PASSWORD_LEN: usize = 32; + let password = auth::generate_secret(PASSWORD_LEN)?; + let password_file = dir.as_ref().join("admin.pass"); + common::str_to_ro_file(&password, password_file)?; + let password = auth::generate_argon2_phc(&password)?; + + const TOKEN_LEN: usize = 34; + let token = auth::generate_secret(TOKEN_LEN)?; + let token = token.as_bytes(); + let token = auth::BASE32_NOPAD.encode(token); + let token_file = dir.as_ref().join("admin.totp"); + common::str_to_ro_file(&token, token_file)?; + + let creds = Credentials { user, password, token }; + + Ok(creds) + } +} + +/// TODO Document +#[derive(Debug, Deserialize, Serialize, PartialEq)] +pub struct DocPaths { + pub templates: String, + pub webroot: String, +} + +impl DocPaths { + pub fn new>(dir: P) -> Result> { + let dir = dir.as_ref().display(); + let templates = format!("{}/blog/templates", dir); + let webroot = format!("{}/blog/webroot", dir); + let docpaths = DocPaths { templates, webroot }; + + Ok(docpaths) + } +} + +/// TODO Document +#[derive(Debug, Deserialize, Serialize, PartialEq)] +pub struct LogConfig { + level: String +} + +/// TODO Document +#[derive(Debug, Deserialize, Serialize, PartialEq)] +pub struct AppConfig { + pub blog: Blog, + pub creds: Credentials, + pub logging: LogConfig, + pub docpaths: DocPaths, +} + +impl AppConfig { + pub fn new(config: T) -> Result> + where T: AsRef { + let config = std::fs::read_to_string(config)?; + let app_config: AppConfig = toml::from_str(&config)?; + Ok(app_config) + } + + pub fn generate, R: BufRead>(dir: P, reader: &mut R) -> Result> { + let docpaths = DocPaths::new(&dir)?; + let blog = Blog::new_from_input(reader)?; + let creds = Credentials::new_from_input(&dir, reader)?; + let level = format!("INFO"); + let logging = LogConfig { level }; + + let config = AppConfig { + blog, + creds, + logging, + docpaths, + }; + + config.create_paths()?; + config.write(&dir)?; + + Ok(config) + } + + fn create_paths(&self) -> Result<(), Box> { + create_dir_all(&self.docpaths.templates)?; + create_dir_all(format!("{}/static/ext", &self.docpaths.webroot))?; + create_dir_all(format!("{}/main/ext", &self.docpaths.webroot))?; + create_dir_all(format!("{}/main/posts", &self.docpaths.webroot))?; + + for topic in &self.blog.topics { + let topic = common::slugify(&topic); + + create_dir_all(format!("{}/{}/ext", &self.docpaths.webroot, &topic))?; + create_dir_all(format!("{}/{}/posts", &self.docpaths.webroot, &topic))?; + } + Ok(()) + } + + fn write>(&self, dir: P) -> Result<(), Box> { + let config = toml::to_string_pretty(&self)?; + let conf_path = &dir.as_ref().join("config.toml"); + common::str_to_ro_file(&config, &conf_path)?; + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use tempfile; + + #[test] + fn build_run_config() { + let arg_vec = vec!["caty-blog", "run", "./test_files/test-config.toml"]; + let matches = args().get_matches_from(arg_vec); + let config = runner_config(matches); + assert!(config.is_ok()); + } + + #[test] + fn build_config_from_input() { + let dir = tempfile::tempdir().unwrap(); + // Setup all target fields + let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\n"; + let config = AppConfig::generate(&dir, &mut src); + assert!(config.is_ok()); + + let tmp_dir = &dir.path(); + let config_path = &tmp_dir.join("config.toml"); + let admin = &tmp_dir.join("admin.pass"); + let token = &tmp_dir.join("admin.totp"); + let blog = &tmp_dir.join("blog"); + let templates = &tmp_dir.join("blog/templates"); + let webroot = &tmp_dir.join("blog/webroot"); + let static_ext = &tmp_dir.join("blog/webroot/static/ext"); + let main_ext = &tmp_dir.join("blog/webroot/main/ext"); + let main_posts = &tmp_dir.join("blog/webroot/main/posts"); + let one_ext = &tmp_dir.join("blog/webroot/one/ext"); + let one_posts = &tmp_dir.join("blog/webroot/one/posts"); + let two_ext = &tmp_dir.join("blog/webroot/two/ext"); + let two_posts = &tmp_dir.join("blog/webroot/two/posts"); + let three_ext = &tmp_dir.join("blog/webroot/three/ext"); + let three_posts = &tmp_dir.join("blog/webroot/three/posts"); + let and_more_ext = &tmp_dir.join("blog/webroot/and-more/ext"); + let and_more_posts = &tmp_dir.join("blog/webroot/and-more/posts"); + let core = vec![config_path, admin, token, blog, templates, + webroot, static_ext, main_ext, main_posts, + one_ext, one_posts, two_ext, two_posts, + three_ext, three_posts, and_more_ext, and_more_posts]; + for p in core { + assert!(Path::new(p).exists()) + } + } + + #[test] + fn handle_csv_topics() { + let reference_topics: Vec = vec![format!("One"), format!("Two"), format!("Three"), format!("And More")]; + let topics = format!("One, Two, Three, And More"); + assert_eq!(reference_topics, csv_to_vec(&topics)) + } + +} diff --git a/src/lib.rs b/src/lib.rs index 87ca204..b59d317 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,86 +1,5 @@ -use std::fs::File; -use std::path::PathBuf; -use std::io::prelude::*; - -use clap::{App, Arg}; -use glob::glob; -use pulldown_cmark::{Parser, html}; -use tera::{Tera, Context}; - - -// Todo expand parsing to include a path for loading config -pub fn parse_args() -> String { - let mut pat_string = String::new(); - let matches = App::new("Make Blog Posts") - .version("1.0") - .author("Anthony Martinez") - .about("I made it of course it rocks") - .arg(Arg::with_name("glob_pattern") - .short("p") - .long("--pattern") - .help("Provides the input file glob pattern") - .takes_value(true) - .required(true)) - .get_matches(); - - if let Some(pattern) = matches.value_of("glob_pattern") { - pat_string = String::from(pattern); - } - - pat_string -} - -pub fn path_matches(pat: &str) -> Vec { - let mut path_vec: Vec = Vec::new(); - for path in glob(pat).unwrap().filter_map(Result::ok) { - path_vec.push(path) - } - - path_vec -} - -pub fn read_to_html(paths: Vec) -> Vec { - let mut contents: Vec = Vec::new(); - for path in paths { - let buffer = std::fs::read_to_string(path).unwrap(); - let parser = Parser::new(&buffer); - let mut html_output = String::new(); - html::push_html(&mut html_output, parser); - contents.push(html_output); - } - contents -} - -pub fn load_templates(dir: &str) -> Tera { - match Tera::new(dir) { - Ok(t) => t, - Err(e) => panic!("Failed with {}", e) - } -} - -pub fn render_main() -> String { - let pattern = parse_args(); - let path_vec = path_matches(&pattern); - let mut data = read_to_html(path_vec); - data.reverse(); - - let tera = load_templates("templates/*.tmpl"); - let mut context = Context::new(); - - context.insert("posts", &data); - - let output = tera.render("main.tmpl", &context); - - if let Ok(output) = output { - output - } else { - String::new() - } -} - -pub fn write_main(rendered: &str) -> Result<(), Box> { - let buf = rendered.as_bytes(); - let mut f = File::create("webroot/index.html")?; - f.write_all(buf)?; - Ok(()) -} +pub mod auth; +pub mod config; +pub mod common; +pub mod render; +pub mod routes; diff --git a/src/logging.rs b/src/logging.rs new file mode 100644 index 0000000..5bfec07 --- /dev/null +++ b/src/logging.rs @@ -0,0 +1 @@ +// Placeholder for logging setup diff --git a/src/render.rs b/src/render.rs new file mode 100644 index 0000000..9590de6 --- /dev/null +++ b/src/render.rs @@ -0,0 +1,111 @@ +use std::path::{Path, PathBuf}; +use std::sync::Arc; + +use super::config::AppConfig; +use super::common; + +pub use pulldown_cmark::{Parser, html}; +pub use tera::{Tera, Context}; + +mod default; + +pub struct Engine { + pub app: Arc, + pub topic_slug: String, + pub template: String, + pub instance: Tera +} + +impl Engine { + pub fn new(a: Arc, ts: &str, tmpl: &str, inst: Tera) -> Engine { + Engine { + app: a.clone(), + topic_slug: ts.to_owned(), + template: tmpl.to_owned(), + instance: inst + } + } +} + +pub fn load_default_template() -> Result> { + let mut tera = Tera::default(); + tera.add_raw_template("default.tmpl", default::TEMPLATE)?; + Ok(tera) +} + +pub fn render_topic(engine: Engine) -> Result> { + let blog = &engine.app.blog; + let topic_data = load_topic(&engine)?; + let mut context = Context::new(); + context.insert("blog", blog); + context.insert("posts", &topic_data); + let output = engine.instance.render(&engine.template, &context)?; + + Ok(output) +} + +fn load_topic(engine: &Engine) -> Result, Box> { + let topic_path = Path::new(&engine.app.docpaths.webroot).join(&engine.topic_slug).join("posts"); + let pat = format!("{}/*.md", topic_path.display()); + let paths = common::path_matches(&pat)?; + read_to_html(paths) +} + +fn read_to_html(paths: Vec) -> Result, Box> { + let mut contents: Vec = Vec::new(); + for path in paths { + let buf = std::fs::read_to_string(path)?; + let parser = Parser::new(&buf); + let mut html_output = String::new(); + html::push_html(&mut html_output, parser); + contents.push(html_output); + } + + Ok(contents) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::fs::File; + use std::io::prelude::*; + use tempfile; + + #[test] + fn check_default_template() { + let tera = load_default_template(); + assert!(tera.is_ok()) + } + + #[test] + fn check_render_topic() { + let dir = tempfile::tempdir().unwrap(); + let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\n"; + let instance = load_default_template().unwrap(); + let config = AppConfig::generate(&dir, &mut src).unwrap(); + let config = Arc::new(config); + let engine = Engine::new(config, "one", "default.tmpl", instance); + + let post = r#" +### Something + +Very cool, but maybe not super useful +"#; + let post2 = r#" +### Something Again + +Super Wow! +"#; + + let mut f = File::create(&dir.path().join("blog/webroot/one/posts/post1.md")).unwrap(); + f.write_all(&post.as_bytes()).unwrap(); + + let mut f = File::create(&dir.path().join("blog/webroot/one/posts/post2.md")).unwrap(); + f.write_all(&post2.as_bytes()).unwrap(); + + let page = render_topic(engine).unwrap(); + + assert!(page.contains("super useful")); + assert!(page.contains("Super Wow!")); + } +} diff --git a/src/render/default.rs b/src/render/default.rs new file mode 100644 index 0000000..90d1457 --- /dev/null +++ b/src/render/default.rs @@ -0,0 +1,36 @@ +pub const TEMPLATE: &str = r#" + + + + + + +{{ blog.name }} + + +
+
+

{{ blog.name }}

+ +
+
+
+{% if posts | length == 0 %} +

Coming Soon!

+{% else %} +{%- for post in posts %} +{{ post }} +{%- endfor -%} +{% endif %} +
+
+

© {{ blog.author }}

+
+ + +"#; diff --git a/src/routes.rs b/src/routes.rs new file mode 100644 index 0000000..966fcb6 --- /dev/null +++ b/src/routes.rs @@ -0,0 +1,97 @@ +use std::convert::Infallible; +use std::fs::File; +use std::io::prelude::*; +use std::path::Path; +use std::sync::Arc; + +use hyper::{Body, Request, Response};//, StatusCode}; +use routerify::{prelude::*, Router}; + +use super::config::AppConfig; +use super::render; + + +pub fn router(app: Arc) -> Router { + Router::builder() + .data(app.clone()) + .get("/", index_handler) + .get("/:topic", topic_handler) + .get("/:topic/ext/:fname", topic_assets) + .get("/static/:fname", static_assets) + .build() + .unwrap() +} + +/// Handler for "/" +async fn index_handler(req: Request) -> Result, Infallible> { + let app = req.data::>().unwrap(); + topic_posts(app.clone(), "main".to_owned()).await +} + +/// Handler for "/:topic" +async fn topic_handler(req: Request) -> Result, Infallible> { + let app = req.data::>().unwrap(); + let topic = req.param("topic").unwrap(); + topic_posts(app.clone(), topic.to_owned()).await +} + +/// Called by topic_handler to dynamically generate topic pages +async fn topic_posts(app: Arc, topic: String) -> Result, Infallible> { + let instance = render::load_default_template().unwrap(); + let engine = render::Engine::new(app.clone(), &topic, "default.tmpl", instance); + let output = render::render_topic(engine).unwrap(); + Ok(Response::new(Body::from(output))) +} + +/// Handler for "/static/:fname" +async fn static_assets(req: Request) -> Result, Infallible> { + let app = req.data::>().unwrap(); + let resource = req.param("fname").unwrap(); + let static_path = Path::new(&app.docpaths.webroot).join("static").join(resource); + let mut f = File::open(static_path).unwrap(); + let mut buf = Vec::new(); + f.read_to_end(&mut buf).unwrap(); + Ok(Response::new(Body::from(buf))) +} + +/// Handler for "/:topic/ext/:fname" +async fn topic_assets(req: Request) -> Result, Infallible> { + let app = req.data::>().unwrap(); + let topic = req.param("topic").unwrap(); + let resource = req.param("fname").unwrap(); + let topic_asset_path = Path::new(&app.docpaths.webroot).join(topic).join("ext").join(resource); + let mut f = File::open(topic_asset_path).unwrap(); + let mut buf = Vec::new(); + f.read_to_end(&mut buf).unwrap(); + Ok(Response::new(Body::from(buf))) +} + + +#[cfg(test)] +mod tests { + /* + use std::fs::File; + use std::io::prelude::*; + use std::net::SocketAddr; + + use super::*; + use crate::config::AppConfig; + use routerify::RouterService; + use tempfile; + + // TODO: figure out how to actually test this.... + + #[tokio::test] + async fn router_test() { + let dir = tempfile::tempdir().unwrap(); + let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\n"; + let app = AppConfig::generate(&dir, &mut src).unwrap(); + let app = Arc::new(app); + let router = router(app.clone()); + let service = RouterService::new(router).unwrap(); + let addr: SocketAddr = "0.0.0.0:9999".parse().unwrap(); + + } + */ + +} diff --git a/templates/main.tmpl b/templates/main.tmpl deleted file mode 100644 index 8783611..0000000 --- a/templates/main.tmpl +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - -A Blog Example - - -
-
-

Quite Magical Really

- -
-
- -
-{%- for post in posts %} -{{ post }} -{%- endfor -%} -
- -
-

This brilliance was brought to you by.. ME

-
- - diff --git a/test_files/test-config.toml b/test_files/test-config.toml new file mode 100644 index 0000000..4a34259 --- /dev/null +++ b/test_files/test-config.toml @@ -0,0 +1,16 @@ +[blog] +name = "My Awesome Blog!" +author = "Neo" +topics = ["one", "two", "three"] + +[creds] +user = "admin" +password = "123456" +token = "my very secret token" + +[logging] +level = "INFO" + +[docpaths] +templates = "/var/www/blog/templates" +webroot = "/var/www/blog/webroot" diff --git a/webroot/index.html b/webroot/index.html deleted file mode 100644 index 6ff1072..0000000 --- a/webroot/index.html +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - -A Blog Example - - -
-
-

Quite Magical Really

- -
-
- -
-

WOWWWWWWWWWWWWWW

-

Another one.

- -

Omfg Amazing

-

This is some cool shit here. Like srsly wtf.

-
- - - - diff --git a/webroot/main/post1.md b/webroot/main/post1.md deleted file mode 100644 index eb94f1c..0000000 --- a/webroot/main/post1.md +++ /dev/null @@ -1,3 +0,0 @@ -### Omfg Amazing - -This is some cool shit here. Like srsly wtf. diff --git a/webroot/main/post2.md b/webroot/main/post2.md deleted file mode 100644 index c4a8169..0000000 --- a/webroot/main/post2.md +++ /dev/null @@ -1,3 +0,0 @@ -### WOWWWWWWWWWWWWWW - -Another one. diff --git a/webroot/static/caty.css b/webroot/static/caty.css deleted file mode 100644 index 49d056d..0000000 --- a/webroot/static/caty.css +++ /dev/null @@ -1,384 +0,0 @@ -:root { - --sans-font:-apple-system,BlinkMacSystemFont,"Avenir Next",Avenir,"Nimbus Sans L",Roboto,Noto,"Segoe UI",Arial,Helvetica,"Helvetica Neue",sans-serif; - --mono-font:Consolas,Menlo,Monaco,"Andale Mono","Ubuntu Mono",monospace; - --base-fontsize:1.15rem; - --header-scale:1.25; - --line-height:1.618; - --bg: #135F4Cff; - --accent-bg:#136044ff; - --text: #B2BABBff; - --text-light: #AFB8B9ff; - --border:#829191; - --accent:#79989B; - --accent-light:#90CAF9; - --code: #BABFBFff; - --preformatted: #A4B2B3ff; - --marked: #A0A6BE; - --disabled:#EFEFEF -} -@media (prefers-color-scheme:dark) { - :root { - --bg:#212121; - --accent-bg:#2B2B2B; - --text:#DCDCDC; - --text-light:#ABABAB; - --border:#666; - --accent:#FFB300; - --accent-light:#FFECB3; - --code:#F06292; - --preformatted:#CCC; - --disabled:#111 - } - img, - video { - opacity:.6 - } -} -html { - font-family:var(--sans-font); - font-size:16px -} -body { - color:var(--text); - background:var(--bg); - font-size:var(--base-fontsize); - line-height:var(--line-height); - display:flex; - min-height:100vh; - flex-direction:column; - flex:1; - margin:0 auto; - max-width:45rem; - padding:0 .5rem; - overflow-x:hidden; - word-break:break-word; - overflow-wrap:break-word -} -header { - background:var(--accent-bg); - border-bottom:1px solid var(--border); - text-align:center; - padding:2rem .5rem; - width:100vw; - position:relative; - left:50%; - right:50%; - margin-left:-50vw; - margin-right:-50vw -} -header h1, -header p { - margin:0 -} -header h1 { - line-height:1.1 -} -nav { - font-size:1rem; - line-height:2; - padding:1rem 0 -} -nav a { - margin:1rem 1rem 0 0; - border:1px solid var(--border); - border-radius:5px; - color:var(--text)!important; - display:inline-block; - padding:.1rem 1rem; - text-decoration:none; - transition:.4s -} -nav a:hover { - color:var(--accent)!important; - border-color:var(--accent) -} -nav a.current:hover { - text-decoration:none -} -footer { - margin-top:4rem; - padding:2rem 1rem 1.5rem 1rem; - color:var(--text-light); - font-size:.9rem; - text-align:center; - border-top:1px solid var(--border) -} -h1 { - font-size:calc(var(--base-fontsize) * var(--header-scale) * var(--header-scale) * var(--header-scale) * var(--header-scale)); - margin-top:calc(var(--line-height) * 1.5rem) -} -h2 { - font-size:calc(var(--base-fontsize) * var(--header-scale) * var(--header-scale) * var(--header-scale)); - margin-top:calc(var(--line-height) * 1.5rem) -} -h3 { - font-size:calc(var(--base-fontsize) * var(--header-scale) * var(--header-scale)); - margin-top:calc(var(--line-height) * 1.5rem) -} -h4 { - font-size:calc(var(--base-fontsize) * var(--header-scale)); - margin-top:calc(var(--line-height) * 1.5rem) -} -h5 { - font-size:var(--base-fontsize); - margin-top:calc(var(--line-height) * 1.5rem) -} -h6 { - font-size:calc(var(--base-fontsize)/ var(--header-scale)); - margin-top:calc(var(--line-height) * 1.5rem) -} -a, -a:visited { - color:var(--accent) -} -a:hover { - text-decoration:none -} -a button, -button, -input[type=button], -input[type=reset], -input[type=submit] { - border:none; - border-radius:5px; - background:var(--accent); - font-size:1rem; - color:var(--bg); - padding:.7rem .9rem; - margin:.5rem 0; - transition:.4s -} -a button[disabled], -button[disabled], -input[type=button][disabled], -input[type=checkbox][disabled], -input[type=radio][disabled], -input[type=reset][disabled], -input[type=submit][disabled], -select[disabled] { - cursor:default; - opacity:.5; - cursor:not-allowed -} -input:disabled, -select:disabled, -textarea:disabled { - cursor:not-allowed; - background-color:var(--disabled) -} -input[type=range] { - padding:0 -} -abbr { - cursor:help -} -button:enabled:hover, -button:focus, -input[type=button]:enabled:hover, -input[type=button]:focus, -input[type=checkbox]:enabled:hover, -input[type=checkbox]:focus, -input[type=radio]:enabled:hover, -input[type=radio]:focus, -input[type=reset]:enabled:hover, -input[type=reset]:focus, -input[type=submit]:enabled:hover, -input[type=submit]:focus { - opacity:.8 -} -details { - padding:.6rem 1rem; - background:var(--accent-bg); - border:1px solid var(--border); - border-radius:5px; - margin-bottom:1rem -} -summary { - cursor:pointer; - font-weight:700 -} -details[open] { - padding-bottom:.75rem -} -details[open] summary { - margin-bottom:.5rem -} -details[open]>:last-child { - margin-bottom:0 -} -table { - border-collapse:collapse; - width:100% margin: 1.5rem 0 -} -td, -th { - border:1px solid var(--border); - text-align:left; - padding:.5rem -} -th { - background:var(--accent-bg); - font-weight:700 -} -tr:nth-child(even) { - background:var(--accent-bg) -} -table caption { - font-weight:700; - margin-bottom:.5rem -} -ol, -ul { - padding-left:3rem -} -input, -select, -textarea { - font-size:inherit; - font-family:inherit; - padding:.5rem; - margin-bottom:.5rem; - color:var(--text); - background:var(--bg); - border:1px solid var(--border); - border-radius:5px; - box-shadow:none; - box-sizing:border-box; - width:60%; - appearance:none; - -moz-appearance:none; - -webkit-appearance:none -} -select { - background-image:linear-gradient(45deg,transparent 49%,var(--text) 51%),linear-gradient(135deg,var(--text) 51%,transparent 49%); - background-position:calc(100% - 20px),calc(100% - 15px); - background-size:5px 5px,5px 5px; - background-repeat:no-repeat -} -input[type=checkbox], -input[type=radio] { - vertical-align:bottom; - position:relative -} -input[type=radio] { - border-radius:100% -} -input[type=checkbox]:checked, -input[type=radio]:checked { - background:var(--accent) -} -input[type=checkbox]:checked::after { - content:' '; - width:.1em; - height:.25em; - border-radius:0; - position:absolute; - top:.05em; - left:.18em; - background:0 0; - border-right:solid var(--bg) .08em; - border-bottom:solid var(--bg) .08em; - font-size:1.8em; - transform:rotate(45deg) -} -input[type=radio]:checked::after { - content:' '; - width:.25em; - height:.25em; - border-radius:100%; - position:absolute; - top:.125em; - background:var(--bg); - left:.125em; - font-size:32px -} -textarea { - width:80% -} -@media only screen and (max-width:720px) { - input, - select, - textarea { - width:100% - } -} -input[type=checkbox], -input[type=radio] { - width:auto -} -input[type=file] { - border:0 -} -fieldset { - border:0; - padding:0; - margin:0 -} -hr { - color:var(--border); - border-top:1px; - margin:1rem auto -} -mark { - padding:2px 5px; - border-radius:4px; - background:var(--marked) -} -main img, -main video { - max-width:100%; - border-radius:5px -} -figure { - margin:0 -} -figcaption { - font-size:.9rem; - color:var(--text-light); - text-align:center; - margin-bottom:1rem -} -blockquote { - margin:2rem 0 2rem 2rem; - padding:.4rem .8rem; - border-left:.35rem solid var(--accent); - opacity:.8; - font-style:italic -} -cite { - font-size:.9rem; - color:var(--text-light); - font-style:normal -} -code, -kbd, -pre, -pre span, -samp { - font-size:1.075rem; - font-family:var(--mono-font); - color:var(--code) -} -kbd { - color:var(--preformatted); - border:1px solid var(--preformatted); - border-bottom:3px solid var(--preformatted); - border-radius:5px; - padding:.1rem -} -pre { - padding:1rem 1.4rem; - max-width:100%; - overflow:auto; - overflow-x:auto; - color:var(--preformatted); - background:var(--accent-bg); - border:1px solid var(--border); - border-radius:5px -} -pre code { - color:var(--preformatted); - background:0 0; - margin:0; - padding:0 -}