diff options
-rw-r--r-- | Cargo.lock | 261 | ||||
-rw-r--r-- | Cargo.toml | 6 | ||||
-rw-r--r-- | src/main.rs | 75 |
3 files changed, 242 insertions, 100 deletions
@@ -16,10 +16,60 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
16 | checksum = "6b602bfe940d21c130f3895acd65221e8a61270debe89d628b9cb4e3ccb8569b" | 16 | checksum = "6b602bfe940d21c130f3895acd65221e8a61270debe89d628b9cb4e3ccb8569b" |
17 | 17 | ||
18 | [[package]] | 18 | [[package]] |
19 | name = "arc-swap" | 19 | name = "async-channel" |
20 | version = "0.4.7" | 20 | version = "1.4.0" |
21 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
22 | checksum = "43de69555a39d52918e2bc33a408d3c0a86c829b212d898f4ca25d21a6387478" | ||
23 | dependencies = [ | ||
24 | "concurrent-queue", | ||
25 | "event-listener", | ||
26 | "futures-core", | ||
27 | ] | ||
28 | |||
29 | [[package]] | ||
30 | name = "async-executor" | ||
31 | version = "0.1.2" | ||
21 | source = "registry+https://github.com/rust-lang/crates.io-index" | 32 | source = "registry+https://github.com/rust-lang/crates.io-index" |
22 | checksum = "4d25d88fd6b8041580a654f9d0c581a047baee2b3efee13275f2fc392fc75034" | 33 | checksum = "90f47c78ea98277cb1f5e6f60ba4fc762f5eafe9f6511bc2f7dfd8b75c225650" |
34 | dependencies = [ | ||
35 | "async-io", | ||
36 | "futures-lite", | ||
37 | "multitask", | ||
38 | "parking 1.0.6", | ||
39 | "scoped-tls", | ||
40 | "waker-fn", | ||
41 | ] | ||
42 | |||
43 | [[package]] | ||
44 | name = "async-io" | ||
45 | version = "0.1.11" | ||
46 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
47 | checksum = "3ae22a338d28c75b53702b66f77979062cb29675db376d99e451af4fa79dedb3" | ||
48 | dependencies = [ | ||
49 | "cfg-if", | ||
50 | "concurrent-queue", | ||
51 | "futures-lite", | ||
52 | "libc", | ||
53 | "once_cell", | ||
54 | "parking 2.0.0", | ||
55 | "polling", | ||
56 | "socket2", | ||
57 | "vec-arena", | ||
58 | "wepoll-sys-stjepang", | ||
59 | "winapi 0.3.9", | ||
60 | ] | ||
61 | |||
62 | [[package]] | ||
63 | name = "async-task" | ||
64 | version = "3.0.0" | ||
65 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
66 | checksum = "c17772156ef2829aadc587461c7753af20b7e8db1529bc66855add962a3b35d3" | ||
67 | |||
68 | [[package]] | ||
69 | name = "atomic-waker" | ||
70 | version = "1.0.0" | ||
71 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
72 | checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a" | ||
23 | 73 | ||
24 | [[package]] | 74 | [[package]] |
25 | name = "autocfg" | 75 | name = "autocfg" |
@@ -40,12 +90,37 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
40 | checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" | 90 | checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" |
41 | 91 | ||
42 | [[package]] | 92 | [[package]] |
93 | name = "blocking" | ||
94 | version = "0.5.2" | ||
95 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
96 | checksum = "ea5800d29218fea137b0880387e5948694a23c93fcdde157006966693a865c7c" | ||
97 | dependencies = [ | ||
98 | "async-channel", | ||
99 | "atomic-waker", | ||
100 | "futures-lite", | ||
101 | "once_cell", | ||
102 | "waker-fn", | ||
103 | ] | ||
104 | |||
105 | [[package]] | ||
43 | name = "bytes" | 106 | name = "bytes" |
44 | version = "0.5.6" | 107 | version = "0.5.6" |
45 | source = "registry+https://github.com/rust-lang/crates.io-index" | 108 | source = "registry+https://github.com/rust-lang/crates.io-index" |
46 | checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" | 109 | checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" |
47 | 110 | ||
48 | [[package]] | 111 | [[package]] |
112 | name = "cache-padded" | ||
113 | version = "1.1.1" | ||
114 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
115 | checksum = "631ae5198c9be5e753e5cc215e1bd73c2b466a3565173db433f52bb9d3e66dba" | ||
116 | |||
117 | [[package]] | ||
118 | name = "cc" | ||
119 | version = "1.0.59" | ||
120 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
121 | checksum = "66120af515773fb005778dc07c261bd201ec8ce50bd6e7144c927753fe013381" | ||
122 | |||
123 | [[package]] | ||
49 | name = "cfg-if" | 124 | name = "cfg-if" |
50 | version = "0.1.10" | 125 | version = "0.1.10" |
51 | source = "registry+https://github.com/rust-lang/crates.io-index" | 126 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -61,6 +136,15 @@ dependencies = [ | |||
61 | ] | 136 | ] |
62 | 137 | ||
63 | [[package]] | 138 | [[package]] |
139 | name = "concurrent-queue" | ||
140 | version = "1.2.2" | ||
141 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
142 | checksum = "30ed07550be01594c6026cff2a1d7fe9c8f683caa798e12b68694ac9e88286a3" | ||
143 | dependencies = [ | ||
144 | "cache-padded", | ||
145 | ] | ||
146 | |||
147 | [[package]] | ||
64 | name = "derive_more" | 148 | name = "derive_more" |
65 | version = "0.99.9" | 149 | version = "0.99.9" |
66 | source = "registry+https://github.com/rust-lang/crates.io-index" | 150 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -72,6 +156,12 @@ dependencies = [ | |||
72 | ] | 156 | ] |
73 | 157 | ||
74 | [[package]] | 158 | [[package]] |
159 | name = "easy-parallel" | ||
160 | version = "3.1.0" | ||
161 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
162 | checksum = "1dd4afd79212583ff429b913ad6605242ed7eec277e950b1438f300748f948f4" | ||
163 | |||
164 | [[package]] | ||
75 | name = "encoding_rs" | 165 | name = "encoding_rs" |
76 | version = "0.8.23" | 166 | version = "0.8.23" |
77 | source = "registry+https://github.com/rust-lang/crates.io-index" | 167 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -81,6 +171,12 @@ dependencies = [ | |||
81 | ] | 171 | ] |
82 | 172 | ||
83 | [[package]] | 173 | [[package]] |
174 | name = "event-listener" | ||
175 | version = "2.4.0" | ||
176 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
177 | checksum = "e1cd41440ae7e4734bbd42302f63eaba892afc93a3912dad84006247f0dedb0e" | ||
178 | |||
179 | [[package]] | ||
84 | name = "fallible-iterator" | 180 | name = "fallible-iterator" |
85 | version = "0.2.0" | 181 | version = "0.2.0" |
86 | source = "registry+https://github.com/rust-lang/crates.io-index" | 182 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -93,6 +189,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
93 | checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" | 189 | checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" |
94 | 190 | ||
95 | [[package]] | 191 | [[package]] |
192 | name = "fastrand" | ||
193 | version = "1.3.4" | ||
194 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
195 | checksum = "4bd3bdaaf0a72155260a1c098989b60db1cbb22d6a628e64f16237aa4da93cc7" | ||
196 | |||
197 | [[package]] | ||
96 | name = "fnv" | 198 | name = "fnv" |
97 | version = "1.0.7" | 199 | version = "1.0.7" |
98 | source = "registry+https://github.com/rust-lang/crates.io-index" | 200 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -169,6 +271,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
169 | checksum = "de27142b013a8e869c14957e6d2edeef89e97c289e69d042ee3a49acd8b51789" | 271 | checksum = "de27142b013a8e869c14957e6d2edeef89e97c289e69d042ee3a49acd8b51789" |
170 | 272 | ||
171 | [[package]] | 273 | [[package]] |
274 | name = "futures-lite" | ||
275 | version = "0.1.11" | ||
276 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
277 | checksum = "97999970129b808f0ccba93211201d431fcc12d7e1ffae03a61b5cedd1a7ced2" | ||
278 | dependencies = [ | ||
279 | "fastrand", | ||
280 | "futures-core", | ||
281 | "futures-io", | ||
282 | "memchr", | ||
283 | "parking 2.0.0", | ||
284 | "pin-project-lite", | ||
285 | "waker-fn", | ||
286 | ] | ||
287 | |||
288 | [[package]] | ||
172 | name = "futures-macro" | 289 | name = "futures-macro" |
173 | version = "0.3.5" | 290 | version = "0.3.5" |
174 | source = "registry+https://github.com/rust-lang/crates.io-index" | 291 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -248,12 +365,11 @@ name = "hedge" | |||
248 | version = "0.1.0" | 365 | version = "0.1.0" |
249 | dependencies = [ | 366 | dependencies = [ |
250 | "anyhow", | 367 | "anyhow", |
251 | "futures", | ||
252 | "hyper", | 368 | "hyper", |
253 | "multer", | 369 | "multer", |
254 | "nanoid", | 370 | "nanoid", |
255 | "rusqlite", | 371 | "rusqlite", |
256 | "tokio", | 372 | "smol", |
257 | "url", | 373 | "url", |
258 | ] | 374 | ] |
259 | 375 | ||
@@ -440,36 +556,13 @@ dependencies = [ | |||
440 | "kernel32-sys", | 556 | "kernel32-sys", |
441 | "libc", | 557 | "libc", |
442 | "log", | 558 | "log", |
443 | "miow 0.2.1", | 559 | "miow", |
444 | "net2", | 560 | "net2", |
445 | "slab", | 561 | "slab", |
446 | "winapi 0.2.8", | 562 | "winapi 0.2.8", |
447 | ] | 563 | ] |
448 | 564 | ||
449 | [[package]] | 565 | [[package]] |
450 | name = "mio-named-pipes" | ||
451 | version = "0.1.7" | ||
452 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
453 | checksum = "0840c1c50fd55e521b247f949c241c9997709f23bd7f023b9762cd561e935656" | ||
454 | dependencies = [ | ||
455 | "log", | ||
456 | "mio", | ||
457 | "miow 0.3.5", | ||
458 | "winapi 0.3.9", | ||
459 | ] | ||
460 | |||
461 | [[package]] | ||
462 | name = "mio-uds" | ||
463 | version = "0.6.8" | ||
464 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
465 | checksum = "afcb699eb26d4332647cc848492bbc15eafb26f08d0304550d5aa1f612e066f0" | ||
466 | dependencies = [ | ||
467 | "iovec", | ||
468 | "libc", | ||
469 | "mio", | ||
470 | ] | ||
471 | |||
472 | [[package]] | ||
473 | name = "miow" | 566 | name = "miow" |
474 | version = "0.2.1" | 567 | version = "0.2.1" |
475 | source = "registry+https://github.com/rust-lang/crates.io-index" | 568 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -482,16 +575,6 @@ dependencies = [ | |||
482 | ] | 575 | ] |
483 | 576 | ||
484 | [[package]] | 577 | [[package]] |
485 | name = "miow" | ||
486 | version = "0.3.5" | ||
487 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
488 | checksum = "07b88fb9795d4d36d62a012dfbf49a8f5cf12751f36d31a9dbe66d528e58979e" | ||
489 | dependencies = [ | ||
490 | "socket2", | ||
491 | "winapi 0.3.9", | ||
492 | ] | ||
493 | |||
494 | [[package]] | ||
495 | name = "multer" | 578 | name = "multer" |
496 | version = "1.2.2" | 579 | version = "1.2.2" |
497 | source = "registry+https://github.com/rust-lang/crates.io-index" | 580 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -511,6 +594,17 @@ dependencies = [ | |||
511 | ] | 594 | ] |
512 | 595 | ||
513 | [[package]] | 596 | [[package]] |
597 | name = "multitask" | ||
598 | version = "0.2.0" | ||
599 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
600 | checksum = "c09c35271e7dcdb5f709779111f2c8e8ab8e06c1b587c1c6a9e179d865aaa5b4" | ||
601 | dependencies = [ | ||
602 | "async-task", | ||
603 | "concurrent-queue", | ||
604 | "fastrand", | ||
605 | ] | ||
606 | |||
607 | [[package]] | ||
514 | name = "nanoid" | 608 | name = "nanoid" |
515 | version = "0.3.0" | 609 | version = "0.3.0" |
516 | source = "registry+https://github.com/rust-lang/crates.io-index" | 610 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -547,6 +641,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
547 | checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad" | 641 | checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad" |
548 | 642 | ||
549 | [[package]] | 643 | [[package]] |
644 | name = "parking" | ||
645 | version = "1.0.6" | ||
646 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
647 | checksum = "6cb300f271742d4a2a66c01b6b2fa0c83dfebd2e0bf11addb879a3547b4ed87c" | ||
648 | |||
649 | [[package]] | ||
650 | name = "parking" | ||
651 | version = "2.0.0" | ||
652 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
653 | checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72" | ||
654 | |||
655 | [[package]] | ||
550 | name = "percent-encoding" | 656 | name = "percent-encoding" |
551 | version = "2.1.0" | 657 | version = "2.1.0" |
552 | source = "registry+https://github.com/rust-lang/crates.io-index" | 658 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -591,6 +697,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
591 | checksum = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33" | 697 | checksum = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33" |
592 | 698 | ||
593 | [[package]] | 699 | [[package]] |
700 | name = "polling" | ||
701 | version = "0.1.5" | ||
702 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
703 | checksum = "9e09dffb745feffca5be3dea51c02b7b368c4597ab0219a82acaf9799ab3e0d1" | ||
704 | dependencies = [ | ||
705 | "cfg-if", | ||
706 | "libc", | ||
707 | "wepoll-sys-stjepang", | ||
708 | "winapi 0.3.9", | ||
709 | ] | ||
710 | |||
711 | [[package]] | ||
594 | name = "proc-macro-hack" | 712 | name = "proc-macro-hack" |
595 | version = "0.5.18" | 713 | version = "0.5.18" |
596 | source = "registry+https://github.com/rust-lang/crates.io-index" | 714 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -775,14 +893,10 @@ dependencies = [ | |||
775 | ] | 893 | ] |
776 | 894 | ||
777 | [[package]] | 895 | [[package]] |
778 | name = "signal-hook-registry" | 896 | name = "scoped-tls" |
779 | version = "1.2.1" | 897 | version = "1.0.0" |
780 | source = "registry+https://github.com/rust-lang/crates.io-index" | 898 | source = "registry+https://github.com/rust-lang/crates.io-index" |
781 | checksum = "a3e12110bc539e657a646068aaf5eb5b63af9d0c1f7b29c97113fad80e15f035" | 899 | checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" |
782 | dependencies = [ | ||
783 | "arc-swap", | ||
784 | "libc", | ||
785 | ] | ||
786 | 900 | ||
787 | [[package]] | 901 | [[package]] |
788 | name = "slab" | 902 | name = "slab" |
@@ -797,6 +911,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
797 | checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252" | 911 | checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252" |
798 | 912 | ||
799 | [[package]] | 913 | [[package]] |
914 | name = "smol" | ||
915 | version = "0.3.3" | ||
916 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
917 | checksum = "67583f4ccc13bbb105a0752058d8ad66c47753d85445952809bcaca891954f83" | ||
918 | dependencies = [ | ||
919 | "async-channel", | ||
920 | "async-executor", | ||
921 | "async-io", | ||
922 | "blocking", | ||
923 | "cfg-if", | ||
924 | "easy-parallel", | ||
925 | "futures-lite", | ||
926 | "num_cpus", | ||
927 | "tokio", | ||
928 | ] | ||
929 | |||
930 | [[package]] | ||
800 | name = "socket2" | 931 | name = "socket2" |
801 | version = "0.3.12" | 932 | version = "0.3.12" |
802 | source = "registry+https://github.com/rust-lang/crates.io-index" | 933 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -855,28 +986,11 @@ dependencies = [ | |||
855 | "futures-core", | 986 | "futures-core", |
856 | "iovec", | 987 | "iovec", |
857 | "lazy_static", | 988 | "lazy_static", |
858 | "libc", | ||
859 | "memchr", | 989 | "memchr", |
860 | "mio", | 990 | "mio", |
861 | "mio-named-pipes", | ||
862 | "mio-uds", | ||
863 | "num_cpus", | 991 | "num_cpus", |
864 | "pin-project-lite", | 992 | "pin-project-lite", |
865 | "signal-hook-registry", | ||
866 | "slab", | 993 | "slab", |
867 | "tokio-macros", | ||
868 | "winapi 0.3.9", | ||
869 | ] | ||
870 | |||
871 | [[package]] | ||
872 | name = "tokio-macros" | ||
873 | version = "0.2.5" | ||
874 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
875 | checksum = "f0c3acc6aa564495a0f2e1d59fab677cd7f81a19994cfc7f3ad0e64301560389" | ||
876 | dependencies = [ | ||
877 | "proc-macro2", | ||
878 | "quote", | ||
879 | "syn", | ||
880 | ] | 994 | ] |
881 | 995 | ||
882 | [[package]] | 996 | [[package]] |
@@ -983,6 +1097,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
983 | checksum = "6454029bf181f092ad1b853286f23e2c507d8e8194d01d92da4a55c274a5508c" | 1097 | checksum = "6454029bf181f092ad1b853286f23e2c507d8e8194d01d92da4a55c274a5508c" |
984 | 1098 | ||
985 | [[package]] | 1099 | [[package]] |
1100 | name = "vec-arena" | ||
1101 | version = "0.5.2" | ||
1102 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1103 | checksum = "8cb18268690309760d59ee1a9b21132c126ba384f374c59a94db4bc03adeb561" | ||
1104 | |||
1105 | [[package]] | ||
1106 | name = "waker-fn" | ||
1107 | version = "1.0.0" | ||
1108 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1109 | checksum = "9571542c2ce85ce642e6b58b3364da2fb53526360dfb7c211add4f5c23105ff7" | ||
1110 | |||
1111 | [[package]] | ||
986 | name = "want" | 1112 | name = "want" |
987 | version = "0.3.0" | 1113 | version = "0.3.0" |
988 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1114 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -993,6 +1119,15 @@ dependencies = [ | |||
993 | ] | 1119 | ] |
994 | 1120 | ||
995 | [[package]] | 1121 | [[package]] |
1122 | name = "wepoll-sys-stjepang" | ||
1123 | version = "1.0.6" | ||
1124 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1125 | checksum = "6fd319e971980166b53e17b1026812ad66c6b54063be879eb182342b55284694" | ||
1126 | dependencies = [ | ||
1127 | "cc", | ||
1128 | ] | ||
1129 | |||
1130 | [[package]] | ||
996 | name = "winapi" | 1131 | name = "winapi" |
997 | version = "0.2.8" | 1132 | version = "0.2.8" |
998 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1133 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -8,11 +8,13 @@ edition = "2018" | |||
8 | 8 | ||
9 | [dependencies] | 9 | [dependencies] |
10 | hyper = "0.13" | 10 | hyper = "0.13" |
11 | tokio = { version = "0.2", features = ["full"] } | ||
12 | rusqlite = "0.24.0" | 11 | rusqlite = "0.24.0" |
13 | nanoid = "0.3.0" | 12 | nanoid = "0.3.0" |
14 | url = "2.1.1" | 13 | url = "2.1.1" |
15 | anyhow = "1.0" | 14 | anyhow = "1.0" |
16 | futures = "0.3.5" | ||
17 | multer = "1.2" | 15 | multer = "1.2" |
18 | 16 | ||
17 | |||
18 | [dependencies.smol] | ||
19 | version = "0.3.3" | ||
20 | features = [ "tokio02" ] | ||
diff --git a/src/main.rs b/src/main.rs index 769ed03..86af042 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -10,6 +10,18 @@ use url::form_urlencoded; | |||
10 | use std::collections::HashMap; | 10 | use std::collections::HashMap; |
11 | use std::path::Path; | 11 | use std::path::Path; |
12 | 12 | ||
13 | fn respond_with_shortlink<S: AsRef<str>>(shortlink: S) -> Response<Body> { | ||
14 | Response::builder() | ||
15 | .status(StatusCode::OK) | ||
16 | .header("content-type", "text/html") | ||
17 | .body(Body::from(shortlink.as_ref().to_string())) | ||
18 | .unwrap() | ||
19 | } | ||
20 | |||
21 | fn respond_with_status(s: StatusCode) -> Response<Body> { | ||
22 | Response::builder().status(s).body(Body::empty()).unwrap() | ||
23 | } | ||
24 | |||
13 | fn shorten<S: AsRef<str>>(url: S, conn: &mut Connection) -> Result<String> { | 25 | fn shorten<S: AsRef<str>>(url: S, conn: &mut Connection) -> Result<String> { |
14 | let mut stmt = conn.prepare("select * from urls where link = ?1")?; | 26 | let mut stmt = conn.prepare("select * from urls where link = ?1")?; |
15 | let mut rows = stmt.query(params![url.as_ref().to_string()])?; | 27 | let mut rows = stmt.query(params![url.as_ref().to_string()])?; |
@@ -36,14 +48,22 @@ fn get_link<S: AsRef<str>>(url: S, conn: &mut Connection) -> Result<Option<Strin | |||
36 | } | 48 | } |
37 | } | 49 | } |
38 | 50 | ||
39 | async fn process_multipart(body: Body, boundary: String) -> Result<Response<Body>> { | 51 | async fn process_multipart( |
52 | body: Body, | ||
53 | boundary: String, | ||
54 | conn: &mut Connection, | ||
55 | ) -> Result<Response<Body>> { | ||
40 | let mut m = Multipart::new(body, boundary); | 56 | let mut m = Multipart::new(body, boundary); |
41 | if let Some(field) = m.next_field().await? { | 57 | if let Some(field) = m.next_field().await? { |
42 | let content = field | 58 | if field.name() == Some("shorten") { |
43 | .text() | 59 | let content = field |
44 | .await | 60 | .text() |
45 | .with_context(|| format!("Expected field name"))?; | 61 | .await |
46 | eprintln!("{}", content); | 62 | .with_context(|| format!("Expected field name"))?; |
63 | |||
64 | let shortlink = shorten(content, conn)?; | ||
65 | return Ok(respond_with_shortlink(shortlink)); | ||
66 | } | ||
47 | } | 67 | } |
48 | Ok(Response::builder() | 68 | Ok(Response::builder() |
49 | .status(StatusCode::OK) | 69 | .status(StatusCode::OK) |
@@ -61,7 +81,6 @@ async fn shortner_service(req: Request<Body>) -> Result<Response<Body>> { | |||
61 | .and_then(|ct| ct.to_str().ok()) | 81 | .and_then(|ct| ct.to_str().ok()) |
62 | .and_then(|ct| multer::parse_boundary(ct).ok()); | 82 | .and_then(|ct| multer::parse_boundary(ct).ok()); |
63 | 83 | ||
64 | // Send `BAD_REQUEST` status if the content-type is not multipart/form-data. | ||
65 | if boundary.is_none() { | 84 | if boundary.is_none() { |
66 | let b = hyper::body::to_bytes(req) | 85 | let b = hyper::body::to_bytes(req) |
67 | .await | 86 | .await |
@@ -72,27 +91,14 @@ async fn shortner_service(req: Request<Body>) -> Result<Response<Body>> { | |||
72 | .collect::<HashMap<String, String>>(); | 91 | .collect::<HashMap<String, String>>(); |
73 | 92 | ||
74 | if let Some(n) = params.get("shorten") { | 93 | if let Some(n) = params.get("shorten") { |
75 | let shortlink = shorten(n, &mut conn)?; | 94 | let s = shorten(n, &mut conn)?; |
76 | eprintln!("{}", shortlink); | 95 | return Ok(respond_with_shortlink(s)); |
77 | let res = Response::builder() | ||
78 | .status(StatusCode::OK) | ||
79 | .header("content-type", "text/html") | ||
80 | .body(Body::from(shortlink))?; | ||
81 | return Ok(res); | ||
82 | } else { | 96 | } else { |
83 | eprintln!("hello world"); | 97 | return Ok(respond_with_status(StatusCode::UNPROCESSABLE_ENTITY)); |
84 | let res = Response::builder() | ||
85 | .status(StatusCode::UNPROCESSABLE_ENTITY) | ||
86 | .body(Body::empty())?; | ||
87 | return Ok(res); | ||
88 | } | 98 | } |
89 | } | 99 | } |
90 | 100 | ||
91 | if let Ok(res) = process_multipart(req.into_body(), boundary.unwrap()).await { | 101 | return process_multipart(req.into_body(), boundary.unwrap(), &mut conn).await; |
92 | return Ok(res); | ||
93 | } else { | ||
94 | panic!("nani"); | ||
95 | } | ||
96 | } | 102 | } |
97 | &Method::GET => { | 103 | &Method::GET => { |
98 | let shortlink = req.uri().path().to_string(); | 104 | let shortlink = req.uri().path().to_string(); |
@@ -135,15 +141,14 @@ fn init_db<P: AsRef<Path>>(p: P) -> Result<Connection> { | |||
135 | return Ok(conn); | 141 | return Ok(conn); |
136 | } | 142 | } |
137 | 143 | ||
138 | #[tokio::main] | 144 | fn main() -> Result<()> { |
139 | async fn main() -> Result<()> { | 145 | smol::run(async { |
140 | let addr = ([127, 0, 0, 1], 3000).into(); | 146 | let addr = ([127, 0, 0, 1], 3000).into(); |
141 | 147 | let service = | |
142 | let service = | 148 | make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(shortner_service)) }); |
143 | make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(shortner_service)) }); | 149 | let server = Server::bind(&addr).serve(service); |
144 | 150 | println!("Listening on http://{}", addr); | |
145 | let server = Server::bind(&addr).serve(service); | 151 | server.await.unwrap(); |
146 | println!("Listening on http://{}", addr); | 152 | Ok(()) |
147 | server.await.unwrap(); | 153 | }) |
148 | Ok(()) | ||
149 | } | 154 | } |