Compare commits

...

2 commits

Author SHA1 Message Date
Rob Ede
f6ba95c070
feat: migrate server to actix-http 2024-05-26 22:22:06 +01:00
Rob Ede
ebec04d2de
refactor: migrate to tracing for logging 2024-05-26 21:42:38 +01:00
25 changed files with 587 additions and 447 deletions

3
.gitignore vendored
View file

@ -4,3 +4,6 @@
# These are backup files generated by rustfmt # These are backup files generated by rustfmt
**/*.rs.bk **/*.rs.bk
# local environment variables
.env

3
.rustfmt.toml Normal file
View file

@ -0,0 +1,3 @@
imports_granularity = "Crate"
group_imports = "StdExternalCrate"
use_field_init_shorthand = true

427
Cargo.lock generated
View file

@ -8,6 +8,103 @@ version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c71b1793ee61086797f5c80b6efa2b8ffa6d5dd703f118545808a7f2e27f7046" checksum = "c71b1793ee61086797f5c80b6efa2b8ffa6d5dd703f118545808a7f2e27f7046"
[[package]]
name = "actix-codec"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f7b0a21988c1bf877cf4759ef5ddaac04c1c9fe808c9142ecb78ba97d97a28a"
dependencies = [
"bitflags 2.5.0",
"bytes",
"futures-core",
"futures-sink",
"memchr",
"pin-project-lite",
"tokio",
"tokio-util",
"tracing",
]
[[package]]
name = "actix-http"
version = "3.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4eb9843d84c775696c37d9a418bbb01b932629d01870722c0f13eb3f95e2536d"
dependencies = [
"actix-codec",
"actix-rt",
"actix-service",
"actix-utils",
"ahash",
"bitflags 2.5.0",
"bytes",
"bytestring",
"derive_more",
"encoding_rs",
"futures-core",
"h2 0.3.26",
"http 0.2.12",
"httparse",
"httpdate",
"itoa",
"language-tags",
"mime",
"percent-encoding",
"pin-project-lite",
"smallvec",
"tokio",
"tokio-util",
"tracing",
]
[[package]]
name = "actix-rt"
version = "2.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28f32d40287d3f402ae0028a9d54bef51af15c8769492826a69d28f81893151d"
dependencies = [
"futures-core",
"tokio",
]
[[package]]
name = "actix-server"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eb13e7eef0423ea6eab0e59f6c72e7cb46d33691ad56a726b3cd07ddec2c2d4"
dependencies = [
"actix-rt",
"actix-service",
"actix-utils",
"futures-core",
"futures-util",
"mio",
"socket2",
"tokio",
"tracing",
]
[[package]]
name = "actix-service"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b894941f818cfdc7ccc4b9e60fa7e53b5042a2e8567270f9147d5591893373a"
dependencies = [
"futures-core",
"paste",
"pin-project-lite",
]
[[package]]
name = "actix-utils"
version = "3.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88a1dcdff1466e3c2488e1cb5c36a71822750ad43839937f85d2f4d9f8b705d8"
dependencies = [
"local-waker",
"pin-project-lite",
]
[[package]] [[package]]
name = "addr2line" name = "addr2line"
version = "0.21.0" version = "0.21.0"
@ -30,11 +127,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"getrandom",
"once_cell", "once_cell",
"version_check", "version_check",
"zerocopy", "zerocopy",
] ]
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "allocator-api2" name = "allocator-api2"
version = "0.2.18" version = "0.2.18"
@ -144,7 +251,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05efc5cfd9110c8416e471df0e96702d58690178e206e61b7173706673c93706" checksum = "05efc5cfd9110c8416e471df0e96702d58690178e206e61b7173706673c93706"
dependencies = [ dependencies = [
"memchr", "memchr",
"regex-automata", "regex-automata 0.4.6",
"serde", "serde",
] ]
@ -160,6 +267,15 @@ version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9"
[[package]]
name = "bytestring"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74d80203ea6b29df88012294f62733de21cfeab47f17b41af3a38bc30a03ee72"
dependencies = [
"bytes",
]
[[package]] [[package]]
name = "cadence" name = "cadence"
version = "1.4.0" version = "1.4.0"
@ -380,25 +496,10 @@ dependencies = [
] ]
[[package]] [[package]]
name = "dirs-next" name = "dotenvy"
version = "2.0.0" version = "0.15.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b"
dependencies = [
"cfg-if",
"dirs-sys-next",
]
[[package]]
name = "dirs-sys-next"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d"
dependencies = [
"libc",
"redox_users",
"winapi",
]
[[package]] [[package]]
name = "dunce" name = "dunce"
@ -1493,17 +1594,6 @@ dependencies = [
"itoa", "itoa",
] ]
[[package]]
name = "http-body"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2"
dependencies = [
"bytes",
"http 0.2.12",
"pin-project-lite",
]
[[package]] [[package]]
name = "http-body" name = "http-body"
version = "1.0.0" version = "1.0.0"
@ -1523,7 +1613,7 @@ dependencies = [
"bytes", "bytes",
"futures-core", "futures-core",
"http 1.1.0", "http 1.1.0",
"http-body 1.0.0", "http-body",
"pin-project-lite", "pin-project-lite",
] ]
@ -1539,30 +1629,6 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
[[package]]
name = "hyper"
version = "0.14.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf96e135eb83a2a8ddf766e426a841d8ddd7449d5f00d34ea02b41d2f19eef80"
dependencies = [
"bytes",
"futures-channel",
"futures-core",
"futures-util",
"h2 0.3.26",
"http 0.2.12",
"http-body 0.4.6",
"httparse",
"httpdate",
"itoa",
"pin-project-lite",
"socket2",
"tokio",
"tower-service",
"tracing",
"want",
]
[[package]] [[package]]
name = "hyper" name = "hyper"
version = "1.3.1" version = "1.3.1"
@ -1574,7 +1640,7 @@ dependencies = [
"futures-util", "futures-util",
"h2 0.4.5", "h2 0.4.5",
"http 1.1.0", "http 1.1.0",
"http-body 1.0.0", "http-body",
"httparse", "httparse",
"itoa", "itoa",
"pin-project-lite", "pin-project-lite",
@ -1591,7 +1657,7 @@ checksum = "a0bea761b46ae2b24eb4aef630d8d1c398157b6fc29e6350ecf090a0b70c952c"
dependencies = [ dependencies = [
"futures-util", "futures-util",
"http 1.1.0", "http 1.1.0",
"hyper 1.3.1", "hyper",
"hyper-util", "hyper-util",
"rustls", "rustls",
"rustls-pki-types", "rustls-pki-types",
@ -1608,7 +1674,7 @@ checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0"
dependencies = [ dependencies = [
"bytes", "bytes",
"http-body-util", "http-body-util",
"hyper 1.3.1", "hyper",
"hyper-util", "hyper-util",
"native-tls", "native-tls",
"tokio", "tokio",
@ -1626,8 +1692,8 @@ dependencies = [
"futures-channel", "futures-channel",
"futures-util", "futures-util",
"http 1.1.0", "http 1.1.0",
"http-body 1.0.0", "http-body",
"hyper 1.3.1", "hyper",
"pin-project-lite", "pin-project-lite",
"socket2", "socket2",
"tokio", "tokio",
@ -1673,17 +1739,6 @@ version = "2.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3"
[[package]]
name = "is-terminal"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b"
dependencies = [
"hermit-abi",
"libc",
"windows-sys 0.52.0",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.11" version = "1.0.11"
@ -1718,6 +1773,12 @@ dependencies = [
"static_assertions", "static_assertions",
] ]
[[package]]
name = "language-tags"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4345964bb142484797b161f473a503a434de77149dd8c7427788c6e13379388"
[[package]] [[package]]
name = "lasso" name = "lasso"
version = "0.7.2" version = "0.7.2"
@ -1739,22 +1800,18 @@ version = "0.2.155"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
[[package]]
name = "libredox"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d"
dependencies = [
"bitflags 2.5.0",
"libc",
]
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.4.14" version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89"
[[package]]
name = "local-waker"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d873d7c67ce09b42110d801813efbc9364414e356be9935700d368351657487"
[[package]] [[package]]
name = "lock_api" name = "lock_api"
version = "0.4.12" version = "0.4.12"
@ -1777,6 +1834,15 @@ version = "0.11.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9106e1d747ffd48e6be5bb2d97fa706ed25b144fbee4d5c02eae110cd8d6badd" checksum = "9106e1d747ffd48e6be5bb2d97fa706ed25b144fbee4d5c02eae110cd8d6badd"
[[package]]
name = "matchers"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558"
dependencies = [
"regex-automata 0.1.10",
]
[[package]] [[package]]
name = "maud" name = "maud"
version = "0.26.0" version = "0.26.0"
@ -1847,6 +1913,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c" checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c"
dependencies = [ dependencies = [
"libc", "libc",
"log",
"wasi", "wasi",
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
@ -1869,6 +1936,16 @@ dependencies = [
"tempfile", "tempfile",
] ]
[[package]]
name = "nu-ansi-term"
version = "0.46.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84"
dependencies = [
"overload",
"winapi",
]
[[package]] [[package]]
name = "num-conv" name = "num-conv"
version = "0.1.0" version = "0.1.0"
@ -1953,6 +2030,12 @@ dependencies = [
"vcpkg", "vcpkg",
] ]
[[package]]
name = "overload"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
[[package]] [[package]]
name = "owned_ttf_parser" name = "owned_ttf_parser"
version = "0.15.2" version = "0.15.2"
@ -1985,6 +2068,12 @@ dependencies = [
"windows-targets 0.52.5", "windows-targets 0.52.5",
] ]
[[package]]
name = "paste"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]] [[package]]
name = "percent-encoding" name = "percent-encoding"
version = "2.3.1" version = "2.3.1"
@ -2206,14 +2295,24 @@ dependencies = [
] ]
[[package]] [[package]]
name = "redox_users" name = "regex"
version = "0.4.5" version = "1.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd283d9651eeda4b2a83a43c1c91b266c40fd76ecd39a50a8c630ae69dc72891" checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c"
dependencies = [ dependencies = [
"getrandom", "aho-corasick",
"libredox", "memchr",
"thiserror", "regex-automata 0.4.6",
"regex-syntax 0.8.3",
]
[[package]]
name = "regex-automata"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
dependencies = [
"regex-syntax 0.6.29",
] ]
[[package]] [[package]]
@ -2221,6 +2320,23 @@ name = "regex-automata"
version = "0.4.6" version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax 0.8.3",
]
[[package]]
name = "regex-syntax"
version = "0.6.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
[[package]]
name = "regex-syntax"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56"
[[package]] [[package]]
name = "relative-path" name = "relative-path"
@ -2246,9 +2362,9 @@ dependencies = [
"futures-util", "futures-util",
"h2 0.4.5", "h2 0.4.5",
"http 1.1.0", "http 1.1.0",
"http-body 1.0.0", "http-body",
"http-body-util", "http-body-util",
"hyper 1.3.1", "hyper",
"hyper-rustls", "hyper-rustls",
"hyper-tls", "hyper-tls",
"hyper-util", "hyper-util",
@ -2422,12 +2538,6 @@ dependencies = [
"owned_ttf_parser", "owned_ttf_parser",
] ]
[[package]]
name = "rustversion"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6"
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.18" version = "1.0.18"
@ -2559,6 +2669,15 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae1a47186c03a32177042e55dbc5fd5aee900b8e0069a8d70fba96a9375cd012" checksum = "ae1a47186c03a32177042e55dbc5fd5aee900b8e0069a8d70fba96a9375cd012"
[[package]]
name = "sharded-slab"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
dependencies = [
"lazy_static",
]
[[package]] [[package]]
name = "shell-words" name = "shell-words"
version = "1.1.0" version = "1.1.0"
@ -2569,16 +2688,19 @@ checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
name = "shiny-robots" name = "shiny-robots"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix-http",
"actix-server",
"actix-service",
"anyhow", "anyhow",
"badge", "badge",
"cadence", "cadence",
"crates-index", "crates-index",
"derive_more", "derive_more",
"dotenvy",
"font-awesome-as-a-crate", "font-awesome-as-a-crate",
"futures-util", "futures-util",
"gix", "gix",
"grass", "grass",
"hyper 0.14.28",
"indexmap", "indexmap",
"lru_time_cache", "lru_time_cache",
"maud", "maud",
@ -2592,11 +2714,19 @@ dependencies = [
"serde", "serde",
"serde_urlencoded", "serde_urlencoded",
"sha-1", "sha-1",
"slog",
"slog-async",
"slog-term",
"tokio", "tokio",
"toml 0.8.13", "toml 0.8.13",
"tracing",
"tracing-subscriber",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1"
dependencies = [
"libc",
] ]
[[package]] [[package]]
@ -2614,37 +2744,6 @@ dependencies = [
"autocfg", "autocfg",
] ]
[[package]]
name = "slog"
version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8347046d4ebd943127157b94d63abb990fcf729dc4e9978927fdf4ac3c998d06"
[[package]]
name = "slog-async"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72c8038f898a2c79507940990f05386455b3a317d8f18d4caea7cbc3d5096b84"
dependencies = [
"crossbeam-channel",
"slog",
"take_mut",
"thread_local",
]
[[package]]
name = "slog-term"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6e022d0b998abfe5c3782c1f03551a596269450ccd677ea51c56f8b214610e8"
dependencies = [
"is-terminal",
"slog",
"term",
"thread_local",
"time",
]
[[package]] [[package]]
name = "smallvec" name = "smallvec"
version = "1.13.2" version = "1.13.2"
@ -2737,12 +2836,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "take_mut"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f764005d11ee5f36500a149ace24e00e3da98b0158b3e2d53a7495660d3f4d60"
[[package]] [[package]]
name = "tame-index" name = "tame-index"
version = "0.12.0" version = "0.12.0"
@ -2780,17 +2873,6 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "term"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c59df8ac95d96ff9bede18eb7300b0fda5e5d8d90960e76f8e14ae765eedbf1f"
dependencies = [
"dirs-next",
"rustversion",
"winapi",
]
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.61" version = "1.0.61"
@ -2880,7 +2962,9 @@ dependencies = [
"libc", "libc",
"mio", "mio",
"num_cpus", "num_cpus",
"parking_lot",
"pin-project-lite", "pin-project-lite",
"signal-hook-registry",
"socket2", "socket2",
"tokio-macros", "tokio-macros",
"windows-sys 0.48.0", "windows-sys 0.48.0",
@ -3032,10 +3116,23 @@ version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef"
dependencies = [ dependencies = [
"log",
"pin-project-lite", "pin-project-lite",
"tracing-attributes",
"tracing-core", "tracing-core",
] ]
[[package]]
name = "tracing-attributes"
version = "0.1.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.66",
]
[[package]] [[package]]
name = "tracing-core" name = "tracing-core"
version = "0.1.32" version = "0.1.32"
@ -3043,6 +3140,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
dependencies = [ dependencies = [
"once_cell", "once_cell",
"valuable",
]
[[package]]
name = "tracing-log"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3"
dependencies = [
"log",
"once_cell",
"tracing-core",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b"
dependencies = [
"matchers",
"nu-ansi-term",
"once_cell",
"regex",
"sharded-slab",
"smallvec",
"thread_local",
"tracing",
"tracing-core",
"tracing-log",
] ]
[[package]] [[package]]
@ -3142,6 +3269,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]] [[package]]
name = "vcpkg" name = "vcpkg"
version = "0.2.15" version = "0.2.15"

View file

@ -14,13 +14,16 @@ edition = "2021"
[dependencies] [dependencies]
badge = { path = "./libs/badge" } badge = { path = "./libs/badge" }
actix-http = { version = "3", features = ["http2"] }
actix-server = "2"
actix-service = "2"
anyhow = "1" anyhow = "1"
cadence = "1" cadence = "1"
crates-index = { version = "2", default-features = false, features = ["git"] } crates-index = { version = "2", default-features = false, features = ["git"] }
derive_more = "0.99" derive_more = "0.99"
dotenvy = "0.15"
font-awesome-as-a-crate = "0.3" font-awesome-as-a-crate = "0.3"
futures-util = { version = "0.3", default-features = false, features = ["std"] } futures-util = { version = "0.3", default-features = false, features = ["std"] }
hyper = { version = "0.14.10", features = ["full"] }
indexmap = { version = "2", features = ["serde"] } indexmap = { version = "2", features = ["serde"] }
lru_time_cache = "0.11" lru_time_cache = "0.11"
maud = "0.26" maud = "0.26"
@ -33,11 +36,10 @@ rustsec = "0.29"
semver = { version = "1.0", features = ["serde"] } semver = { version = "1.0", features = ["serde"] }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_urlencoded = "0.7" serde_urlencoded = "0.7"
slog = "2"
slog-async = "2"
slog-term = "2"
tokio = { version = "1.24.2", features = ["rt-multi-thread", "macros", "sync", "time"] } tokio = { version = "1.24.2", features = ["rt-multi-thread", "macros", "sync", "time"] }
toml = "0.8" toml = "0.8"
tracing = "0.1.30"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
[target.'cfg(any())'.dependencies] [target.'cfg(any())'.dependencies]
gix = { version = "0.63", default-features = false, features = ["blocking-http-transport-reqwest-rust-tls"] } gix = { version = "0.63", default-features = false, features = ["blocking-http-transport-reqwest-rust-tls"] }

View file

@ -1,6 +1,4 @@
use std::env; use std::{env, fs, path::Path};
use std::fs;
use std::path::Path;
use sha1::{Digest, Sha1}; use sha1::{Digest, Sha1};

View file

@ -235,8 +235,8 @@ mod tests {
#[test] #[test]
#[ignore] #[ignore]
fn test_to_svg() { fn test_to_svg() {
use std::fs::File; use std::{fs::File, io::Write as _};
use std::io::Write;
let mut file = File::create("test.svg").unwrap(); let mut file = File::create("test.svg").unwrap();
let options = BadgeOptions { let options = BadgeOptions {
subject: "latest".to_owned(), subject: "latest".to_owned(),

View file

@ -2,11 +2,12 @@ use anyhow::Error;
use futures_util::{future::BoxFuture, stream::FuturesOrdered, FutureExt as _, StreamExt as _}; use futures_util::{future::BoxFuture, stream::FuturesOrdered, FutureExt as _, StreamExt as _};
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use crate::models::repo::RepoPath; use crate::{
engine::{
use crate::engine::{
machines::crawler::{ManifestCrawler, ManifestCrawlerOutput}, machines::crawler::{ManifestCrawler, ManifestCrawlerOutput},
Engine, Engine,
},
models::repo::RepoPath,
}; };
pub async fn crawl_manifest( pub async fn crawl_manifest(

View file

@ -1,5 +1,4 @@
mod analyze; mod analyze;
mod crawl; mod crawl;
pub use self::analyze::analyze_dependencies; pub use self::{analyze::analyze_dependencies, crawl::crawl_manifest};
pub use self::crawl::crawl_manifest;

View file

@ -101,9 +101,8 @@ impl DependencyAnalyzer {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::models::crates::{CrateDep, CrateDeps, CrateRelease};
use super::*; use super::*;
use crate::models::crates::{CrateDep, CrateDeps, CrateRelease};
#[test] #[test]
fn tracks_latest_without_matching() { fn tracks_latest_without_matching() {

View file

@ -4,8 +4,10 @@ use anyhow::Error;
use indexmap::IndexMap; use indexmap::IndexMap;
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use crate::models::crates::{CrateDep, CrateDeps, CrateManifest, CrateName}; use crate::{
use crate::parsers::manifest::parse_manifest_toml; models::crates::{CrateDep, CrateDeps, CrateManifest, CrateName},
parsers::manifest::parse_manifest_toml,
};
pub struct ManifestCrawlerOutput { pub struct ManifestCrawlerOutput {
pub crates: IndexMap<CrateName, CrateDeps>, pub crates: IndexMap<CrateName, CrateDeps>,
@ -118,9 +120,8 @@ mod tests {
use relative_path::RelativePath; use relative_path::RelativePath;
use semver::VersionReq; use semver::VersionReq;
use crate::models::crates::CrateDep;
use super::*; use super::*;
use crate::models::crates::CrateDep;
#[test] #[test]
fn simple_package_manifest() { fn simple_package_manifest() {

View file

@ -5,29 +5,33 @@ use std::{
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use actix_service::Service;
use anyhow::{anyhow, Error}; use anyhow::{anyhow, Error};
use cadence::{MetricSink, NopMetricSink, StatsdClient}; use cadence::{MetricSink, NopMetricSink, StatsdClient};
use futures_util::{ use futures_util::{
future::try_join_all, future::try_join_all,
stream::{self, BoxStream}, stream::{self, BoxStream},
StreamExt as _, StreamExt as _,
}; };
use hyper::service::Service;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use relative_path::{RelativePath, RelativePathBuf}; use relative_path::{RelativePath, RelativePathBuf};
use rustsec::database::Database; use rustsec::database::Database;
use semver::VersionReq; use semver::VersionReq;
use slog::Logger;
use crate::interactors::crates::{GetPopularCrates, QueryCrate}; use crate::{
use crate::interactors::github::GetPopularRepos; interactors::{
use crate::interactors::rustsec::FetchAdvisoryDatabase; crates::{GetPopularCrates, QueryCrate},
use crate::interactors::RetrieveFileAtPath; github::GetPopularRepos,
use crate::models::crates::{AnalyzedDependencies, CrateName, CratePath, CrateRelease}; rustsec::FetchAdvisoryDatabase,
use crate::models::repo::{RepoPath, Repository}; RetrieveFileAtPath,
use crate::utils::cache::Cache; },
use crate::ManagedIndex; models::{
crates::{AnalyzedDependencies, CrateName, CratePath, CrateRelease},
repo::{RepoPath, Repository},
},
utils::cache::Cache,
ManagedIndex,
};
mod fut; mod fut;
mod machines; mod machines;
@ -45,33 +49,25 @@ pub struct Engine {
} }
impl Engine { impl Engine {
pub fn new(client: reqwest::Client, index: ManagedIndex, logger: Logger) -> Engine { pub fn new(client: reqwest::Client, index: ManagedIndex) -> Engine {
let metrics = Arc::new(StatsdClient::from_sink("engine", NopMetricSink)); let metrics = Arc::new(StatsdClient::from_sink("engine", NopMetricSink));
let query_crate = Cache::new( let query_crate = Cache::new(QueryCrate::new(index), Duration::from_secs(10), 500);
QueryCrate::new(index),
Duration::from_secs(10),
500,
logger.clone(),
);
let get_popular_crates = Cache::new( let get_popular_crates = Cache::new(
GetPopularCrates::new(client.clone()), GetPopularCrates::new(client.clone()),
Duration::from_secs(15 * 60), Duration::from_secs(15 * 60),
1, 1,
logger.clone(),
); );
let get_popular_repos = Cache::new( let get_popular_repos = Cache::new(
GetPopularRepos::new(client.clone()), GetPopularRepos::new(client.clone()),
Duration::from_secs(5 * 60), Duration::from_secs(5 * 60),
1, 1,
logger.clone(),
); );
let retrieve_file_at_path = RetrieveFileAtPath::new(client.clone()); let retrieve_file_at_path = RetrieveFileAtPath::new(client.clone());
let fetch_advisory_db = Cache::new( let fetch_advisory_db = Cache::new(
FetchAdvisoryDatabase::new(client), FetchAdvisoryDatabase::new(client),
Duration::from_secs(30 * 60), Duration::from_secs(30 * 60),
1, 1,
logger,
); );
Engine { Engine {
@ -281,7 +277,7 @@ impl Engine {
) -> Result<String, Error> { ) -> Result<String, Error> {
let manifest_path = path.join(RelativePath::new("Cargo.toml")); let manifest_path = path.join(RelativePath::new("Cargo.toml"));
let mut service = self.retrieve_file_at_path.clone(); let service = self.retrieve_file_at_path.clone();
service.call((repo_path.clone(), manifest_path)).await service.call((repo_path.clone(), manifest_path)).await
} }

View file

@ -1,12 +1,11 @@
use std::{fmt, str, task::Context, task::Poll}; use std::fmt;
use actix_service::Service;
use anyhow::{anyhow, Error}; use anyhow::{anyhow, Error};
use crates_index::{Crate, DependencyKind}; use crates_index::{Crate, DependencyKind};
use futures_util::FutureExt as _; use futures_util::FutureExt as _;
use hyper::service::Service;
use semver::{Version, VersionReq}; use semver::{Version, VersionReq};
use serde::Deserialize; use serde::Deserialize;
use tokio::task::spawn_blocking; use tokio::task::spawn_blocking;
use crate::{ use crate::{
@ -86,11 +85,9 @@ impl Service<CrateName> for QueryCrate {
type Error = Error; type Error = Error;
type Future = BoxFuture<Result<Self::Response, Self::Error>>; type Future = BoxFuture<Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { actix_service::always_ready!();
Poll::Ready(Ok(()))
}
fn call(&mut self, crate_name: CrateName) -> Self::Future { fn call(&self, crate_name: CrateName) -> Self::Future {
let index = self.index.clone(); let index = self.index.clone();
Self::query(index, crate_name).boxed() Self::query(index, crate_name).boxed()
} }
@ -150,11 +147,9 @@ impl Service<()> for GetPopularCrates {
type Error = Error; type Error = Error;
type Future = BoxFuture<Result<Self::Response, Self::Error>>; type Future = BoxFuture<Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { actix_service::always_ready!();
Poll::Ready(Ok(()))
}
fn call(&mut self, _req: ()) -> Self::Future { fn call(&self, _req: ()) -> Self::Future {
let client = self.client.clone(); let client = self.client.clone();
Self::query(client).boxed() Self::query(client).boxed()
} }

View file

@ -1,12 +1,8 @@
use std::{ use std::fmt;
fmt,
task::{Context, Poll},
};
use actix_service::Service;
use anyhow::Error; use anyhow::Error;
use futures_util::FutureExt as _; use futures_util::FutureExt as _;
use hyper::service::Service;
use serde::Deserialize; use serde::Deserialize;
use crate::{ use crate::{
@ -75,11 +71,9 @@ impl Service<()> for GetPopularRepos {
type Error = Error; type Error = Error;
type Future = BoxFuture<Result<Self::Response, Self::Error>>; type Future = BoxFuture<Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { actix_service::always_ready!();
Poll::Ready(Ok(()))
}
fn call(&mut self, _req: ()) -> Self::Future { fn call(&self, _req: ()) -> Self::Future {
let client = self.client.clone(); let client = self.client.clone();
Self::query(client).boxed() Self::query(client).boxed()
} }

View file

@ -1,11 +1,8 @@
use std::{ use std::fmt;
fmt,
task::{Context, Poll},
};
use actix_service::Service;
use anyhow::{anyhow, Error}; use anyhow::{anyhow, Error};
use futures_util::FutureExt as _; use futures_util::FutureExt as _;
use hyper::service::Service;
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use crate::{models::repo::RepoPath, BoxFuture}; use crate::{models::repo::RepoPath, BoxFuture};
@ -45,11 +42,9 @@ impl Service<(RepoPath, RelativePathBuf)> for RetrieveFileAtPath {
type Error = Error; type Error = Error;
type Future = BoxFuture<Result<Self::Response, Self::Error>>; type Future = BoxFuture<Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { actix_service::always_ready!();
Poll::Ready(Ok(()))
}
fn call(&mut self, (repo_path, path): (RepoPath, RelativePathBuf)) -> Self::Future { fn call(&self, (repo_path, path): (RepoPath, RelativePathBuf)) -> Self::Future {
let client = self.client.clone(); let client = self.client.clone();
Self::query(client, repo_path, path).boxed() Self::query(client, repo_path, path).boxed()
} }
@ -57,6 +52,6 @@ impl Service<(RepoPath, RelativePathBuf)> for RetrieveFileAtPath {
impl fmt::Debug for RetrieveFileAtPath { impl fmt::Debug for RetrieveFileAtPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("RetrieveFileAtPath") f.debug_struct("RetrieveFileAtPath").finish_non_exhaustive()
} }
} }

View file

@ -1,8 +1,8 @@
use std::{fmt, sync::Arc, task::Context, task::Poll}; use std::{fmt, sync::Arc};
use actix_service::Service;
use anyhow::Error; use anyhow::Error;
use futures_util::FutureExt as _; use futures_util::FutureExt as _;
use hyper::service::Service;
use rustsec::database::Database; use rustsec::database::Database;
use crate::BoxFuture; use crate::BoxFuture;
@ -28,11 +28,9 @@ impl Service<()> for FetchAdvisoryDatabase {
type Error = Error; type Error = Error;
type Future = BoxFuture<Result<Self::Response, Self::Error>>; type Future = BoxFuture<Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { actix_service::always_ready!();
Poll::Ready(Ok(()))
}
fn call(&mut self, _req: ()) -> Self::Future { fn call(&self, _req: ()) -> Self::Future {
let client = self.client.clone(); let client = self.client.clone();
Self::fetch(client).boxed() Self::fetch(client).boxed()
} }

View file

@ -4,20 +4,16 @@
use std::{ use std::{
env, env,
future::Future, future::Future,
net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket}, net::{Ipv4Addr, UdpSocket},
pin::Pin, pin::Pin,
time::Duration, time::Duration,
}; };
use actix_http::{HttpService, Request};
use actix_server::Server;
use cadence::{QueuingMetricSink, UdpMetricSink}; use cadence::{QueuingMetricSink, UdpMetricSink};
use hyper::{
server::conn::AddrStream,
service::{make_service_fn, service_fn},
Server,
};
use reqwest::redirect::Policy as RedirectPolicy; use reqwest::redirect::Policy as RedirectPolicy;
use slog::{error, info, o, Drain, Logger}; use tracing::Instrument as _;
mod engine; mod engine;
mod interactors; mod interactors;
@ -26,9 +22,7 @@ mod parsers;
mod server; mod server;
mod utils; mod utils;
use self::engine::Engine; use self::{engine::Engine, server::App, utils::index::ManagedIndex};
use self::server::App;
use self::utils::index::ManagedIndex;
/// Future crate's BoxFuture without the explicit lifetime parameter. /// Future crate's BoxFuture without the explicit lifetime parameter.
pub type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>; pub type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;
@ -43,18 +37,29 @@ fn init_metrics() -> QueuingMetricSink {
QueuingMetricSink::from(sink) QueuingMetricSink::from(sink)
} }
fn init_root_logger() -> Logger { fn init_tracing_subscriber() {
let decorator = slog_term::TermDecorator::new().build(); use tracing::level_filters::LevelFilter;
let drain = slog_term::FullFormat::new(decorator).build().fuse(); use tracing_subscriber::{fmt, prelude::*, EnvFilter};
let drain = slog_async::Async::new(drain).build().fuse();
Logger::root(drain, o!()) let stdout_logger = match env::var("RUST_LOG_TIME").as_deref() {
Ok("false") => fmt::layer().without_time().boxed(),
_ => fmt::layer().boxed(),
};
tracing_subscriber::registry()
.with(
EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy(),
)
.with(stdout_logger)
.init();
} }
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let logger = init_root_logger(); dotenvy::dotenv().ok();
init_tracing_subscriber();
let metrics = init_metrics(); let metrics = init_metrics();
let client = reqwest::Client::builder() let client = reqwest::Client::builder()
@ -69,9 +74,7 @@ async fn main() {
.parse() .parse()
.expect("could not read port"); .expect("could not read port");
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), port); let index = ManagedIndex::new();
let index = ManagedIndex::new(logger.clone());
{ {
let index = index.clone(); let index = index.clone();
@ -81,27 +84,37 @@ async fn main() {
}); });
} }
let mut engine = Engine::new(client.clone(), index, logger.new(o!())); let mut engine = Engine::new(client.clone(), index);
engine.set_metrics(metrics); engine.set_metrics(metrics);
let svc_logger = logger.new(o!()); let server = Server::build()
let make_svc = make_service_fn(move |_socket: &AddrStream| { .bind("deps-rs", (Ipv4Addr::UNSPECIFIED, port), move || {
let engine = engine.clone(); let engine = engine.clone();
let logger = svc_logger.clone();
let app = App::new(engine.clone());
HttpService::build()
.client_disconnect_timeout(Duration::from_secs(5))
.client_request_timeout(Duration::from_secs(5))
.finish(move |req: Request| {
let app = app.clone();
async move { async move {
let server = App::new(logger.clone(), engine.clone()); let path = req.path().to_owned();
Ok::<_, hyper::Error>(service_fn(move |req| {
let server = server.clone(); app.handle(req)
async move { server.handle(req).await } .instrument(tracing::info_span!("@", %path))
})) .await
} }
}); })
let server = Server::bind(&addr).serve(make_svc); .tcp_auto_h2c()
})
.unwrap()
.run();
info!(logger, "Server running on port {}", port); tracing::info!("Server running on port {port}");
if let Err(e) = server.await { if let Err(err) = server.await {
error!(logger, "server error: {}", e); tracing::error!("server error: {err}");
} }
} }

View file

@ -135,9 +135,8 @@ pub fn parse_manifest_toml(input: &str) -> Result<CrateManifest, Error> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::models::crates::CrateManifest;
use super::*; use super::*;
use crate::models::crates::CrateManifest;
#[test] #[test]
fn parse_workspace_without_members_declaration() { fn parse_workspace_without_members_declaration() {

View file

@ -1,16 +1,16 @@
use std::{env, sync::Arc, time::Instant}; use std::{env, sync::Arc, time::Instant};
use actix_http::{
body::MessageBody,
header::{CACHE_CONTROL, CONTENT_TYPE, ETAG, LOCATION},
Method, Request, Response, StatusCode,
};
use badge::BadgeStyle; use badge::BadgeStyle;
use futures_util::future; use futures_util::future;
use hyper::{
header::{CACHE_CONTROL, CONTENT_TYPE, ETAG, LOCATION},
Body, Error as HyperError, Method, Request, Response, StatusCode,
};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use route_recognizer::{Params, Router}; use route_recognizer::{Params, Router};
use semver::VersionReq; use semver::VersionReq;
use serde::Deserialize; use serde::Deserialize;
use slog::{error, info, o, Logger};
mod assets; mod assets;
mod views; mod views;
@ -18,10 +18,14 @@ mod views;
use self::assets::{ use self::assets::{
STATIC_LINKS_JS_ETAG, STATIC_LINKS_JS_PATH, STATIC_STYLE_CSS_ETAG, STATIC_STYLE_CSS_PATH, STATIC_LINKS_JS_ETAG, STATIC_LINKS_JS_PATH, STATIC_STYLE_CSS_ETAG, STATIC_STYLE_CSS_PATH,
}; };
use crate::engine::{AnalyzeDependenciesOutcome, Engine}; use crate::{
use crate::models::crates::{CrateName, CratePath}; engine::{AnalyzeDependenciesOutcome, Engine},
use crate::models::repo::RepoPath; models::{
use crate::models::SubjectPath; crates::{CrateName, CratePath},
repo::RepoPath,
SubjectPath,
},
};
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
enum StatusFormat { enum StatusFormat {
@ -47,13 +51,12 @@ enum Route {
#[derive(Clone)] #[derive(Clone)]
pub struct App { pub struct App {
logger: Logger,
engine: Engine, engine: Engine,
router: Arc<Router<Route>>, router: Arc<Router<Route>>,
} }
impl App { impl App {
pub fn new(logger: Logger, engine: Engine) -> App { pub fn new(engine: Engine) -> App {
let mut router = Router::new(); let mut router = Router::new();
router.add("/", Route::Index); router.add("/", Route::Index);
@ -83,15 +86,15 @@ impl App {
); );
App { App {
logger,
engine, engine,
router: Arc::new(router), router: Arc::new(router),
} }
} }
pub async fn handle(&self, req: Request<Body>) -> Result<Response<Body>, HyperError> { pub async fn handle(
let logger = self.logger.new(o!("path" => req.uri().path().to_owned())); &self,
let logger2 = logger.clone(); req: Request,
) -> Result<Response<impl MessageBody>, actix_http::Error> {
let start = Instant::now(); let start = Instant::now();
// allows `/path/` to also match `/path` // allows `/path/` to also match `/path`
@ -99,48 +102,50 @@ impl App {
let res = if let Ok(route_match) = self.router.recognize(normalized_path) { let res = if let Ok(route_match) = self.router.recognize(normalized_path) {
match (req.method(), route_match.handler()) { match (req.method(), route_match.handler()) {
(&Method::GET, Route::Index) => { (&Method::GET, Route::Index) => self
self.index(req, route_match.params().clone(), logger).await .index(req, route_match.params().clone())
}
(&Method::GET, Route::RepoStatus(format)) => {
self.repo_status(req, route_match.params().clone(), logger, *format)
.await .await
} .map(Response::map_into_boxed_body),
(&Method::GET, Route::CrateStatus(format)) => { (&Method::GET, Route::RepoStatus(format)) => self
self.crate_status(req, route_match.params().clone(), logger, *format) .repo_status(req, route_match.params().clone(), *format)
.await .await
} .map(Response::map_into_boxed_body),
(&Method::GET, Route::LatestCrateBadge) => { (&Method::GET, Route::CrateStatus(format)) => self
self.crate_status(req, route_match.params().clone(), logger, StatusFormat::Svg) .crate_status(req, route_match.params().clone(), *format)
.await .await
} .map(Response::map_into_boxed_body),
(&Method::GET, Route::CrateRedirect) => { (&Method::GET, Route::LatestCrateBadge) => self
self.crate_redirect(req, route_match.params().clone(), logger) .crate_status(req, route_match.params().clone(), StatusFormat::Svg)
.await .await
.map(Response::map_into_boxed_body),
(&Method::GET, Route::CrateRedirect) => self
.crate_redirect(req, route_match.params().clone())
.await
.map(Response::map_into_boxed_body),
(&Method::GET, Route::Static(file)) => {
Ok(App::static_file(*file).map_into_boxed_body())
} }
(&Method::GET, Route::Static(file)) => Ok(App::static_file(*file)), _ => Ok(not_found().map_into_boxed_body()),
_ => Ok(not_found()),
} }
} else { } else {
Ok(not_found()) Ok(not_found().map_into_boxed_body())
}; };
let end = Instant::now(); let end = Instant::now();
let diff = end - start; let diff = end - start;
match &res { match &res {
Ok(res) => info!( Ok(res) => tracing::info!(
logger2, ""; status = %res.status(),
"status" => res.status().to_string(), time = %format_args!("{}ms", diff.as_millis()),
"time" => format!("{}ms", diff.as_millis())
), ),
Err(err) => error!(logger2, ""; "error" => err.to_string()), Err(err) => tracing::error!(%err),
}; };
res res
@ -150,10 +155,9 @@ impl App {
impl App { impl App {
async fn index( async fn index(
&self, &self,
_req: Request<Body>, _req: Request,
_params: Params, _params: Params,
logger: Logger, ) -> Result<Response<impl MessageBody>, actix_http::Error> {
) -> Result<Response<Body>, HyperError> {
let engine = self.engine.clone(); let engine = self.engine.clone();
let popular = let popular =
@ -161,25 +165,25 @@ impl App {
match popular { match popular {
Err(err) => { Err(err) => {
error!(logger, "error: {}", err); tracing::error!(%err);
let mut response = let mut response =
views::html::error::render("Could not retrieve popular items", ""); views::html::error::render("Could not retrieve popular items", "");
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
Ok(response)
Ok(response.map_into_boxed_body())
} }
Ok((popular_repos, popular_crates)) => { Ok((popular_repos, popular_crates)) => {
Ok(views::html::index::render(popular_repos, popular_crates)) Ok(views::html::index::render(popular_repos, popular_crates).map_into_boxed_body())
} }
} }
} }
async fn repo_status( async fn repo_status(
&self, &self,
req: Request<Body>, req: Request,
params: Params, params: Params,
logger: Logger,
format: StatusFormat, format: StatusFormat,
) -> Result<Response<Body>, HyperError> { ) -> Result<Response<impl MessageBody>, actix_http::Error> {
let server = self.clone(); let server = self.clone();
let site = params.find("site").expect("route param 'site' not found"); let site = params.find("site").expect("route param 'site' not found");
@ -192,13 +196,14 @@ impl App {
match repo_path_result { match repo_path_result {
Err(err) => { Err(err) => {
error!(logger, "error: {}", err); tracing::error!(%err);
let mut response = views::html::error::render( let mut response = views::html::error::render(
"Could not parse repository path", "Could not parse repository path",
"Please make sure to provide a valid repository path.", "Please make sure to provide a valid repository path.",
); );
*response.status_mut() = StatusCode::BAD_REQUEST; *response.status_mut() = StatusCode::BAD_REQUEST;
Ok(response)
Ok(response.map_into_boxed_body())
} }
Ok(repo_path) => { Ok(repo_path) => {
@ -209,14 +214,15 @@ impl App {
match analyze_result { match analyze_result {
Err(err) => { Err(err) => {
error!(logger, "error: {}", err); tracing::error!(%err);
let response = App::status_format_analysis( let response = App::status_format_analysis(
None, None,
format, format,
SubjectPath::Repo(repo_path), SubjectPath::Repo(repo_path),
extra_knobs, extra_knobs,
); );
Ok(response)
Ok(response.map_into_boxed_body())
} }
Ok(analysis_outcome) => { Ok(analysis_outcome) => {
let response = App::status_format_analysis( let response = App::status_format_analysis(
@ -225,7 +231,8 @@ impl App {
SubjectPath::Repo(repo_path), SubjectPath::Repo(repo_path),
extra_knobs, extra_knobs,
); );
Ok(response)
Ok(response.map_into_boxed_body())
} }
} }
} }
@ -234,10 +241,9 @@ impl App {
async fn crate_redirect( async fn crate_redirect(
&self, &self,
_req: Request<Body>, _req: Request,
params: Params, params: Params,
logger: Logger, ) -> Result<Response<impl MessageBody>, actix_http::Error> {
) -> Result<Response<Body>, HyperError> {
let engine = self.engine.clone(); let engine = self.engine.clone();
let name = params.find("name").expect("route param 'name' not found"); let name = params.find("name").expect("route param 'name' not found");
@ -245,13 +251,14 @@ impl App {
match crate_name_result { match crate_name_result {
Err(err) => { Err(err) => {
error!(logger, "error: {}", err); tracing::error!(%err);
let mut response = views::html::error::render( let mut response = views::html::error::render(
"Could not parse crate name", "Could not parse crate name",
"Please make sure to provide a valid crate name.", "Please make sure to provide a valid crate name.",
); );
*response.status_mut() = StatusCode::BAD_REQUEST; *response.status_mut() = StatusCode::BAD_REQUEST;
Ok(response)
Ok(response.map_into_boxed_body())
} }
Ok(crate_name) => { Ok(crate_name) => {
@ -261,13 +268,14 @@ impl App {
match release_result { match release_result {
Err(err) => { Err(err) => {
error!(logger, "error: {}", err); tracing::error!(%err);
let mut response = views::html::error::render( let mut response = views::html::error::render(
"Could not fetch crate information", "Could not fetch crate information",
"Please make sure to provide a valid crate name.", "Please make sure to provide a valid crate name.",
); );
*response.status_mut() = StatusCode::NOT_FOUND; *response.status_mut() = StatusCode::NOT_FOUND;
Ok(response)
Ok(response.map_into_boxed_body())
} }
Ok(None) => { Ok(None) => {
let mut response = views::html::error::render( let mut response = views::html::error::render(
@ -275,7 +283,8 @@ impl App {
"Please make sure to provide a valid crate name.", "Please make sure to provide a valid crate name.",
); );
*response.status_mut() = StatusCode::NOT_FOUND; *response.status_mut() = StatusCode::NOT_FOUND;
Ok(response)
Ok(response.map_into_boxed_body())
} }
Ok(Some(release)) => { Ok(Some(release)) => {
let redirect_url = format!( let redirect_url = format!(
@ -285,13 +294,11 @@ impl App {
release.version release.version
); );
let res = Response::builder() let res = Response::build(StatusCode::TEMPORARY_REDIRECT)
.status(StatusCode::TEMPORARY_REDIRECT) .insert_header((LOCATION, redirect_url))
.header(LOCATION, redirect_url) .finish();
.body(Body::empty())
.unwrap();
Ok(res) Ok(res.map_into_boxed_body())
} }
} }
} }
@ -300,11 +307,10 @@ impl App {
async fn crate_status( async fn crate_status(
&self, &self,
req: Request<Body>, req: Request,
params: Params, params: Params,
logger: Logger,
format: StatusFormat, format: StatusFormat,
) -> Result<Response<Body>, HyperError> { ) -> Result<Response<impl MessageBody>, actix_http::Error> {
let server = self.clone(); let server = self.clone();
let name = params.find("name").expect("route param 'name' not found"); let name = params.find("name").expect("route param 'name' not found");
@ -320,7 +326,8 @@ impl App {
"Please make sure to provide a valid crate name and version.", "Please make sure to provide a valid crate name and version.",
); );
*response.status_mut() = StatusCode::BAD_REQUEST; *response.status_mut() = StatusCode::BAD_REQUEST;
return Ok(response);
return Ok(response.map_into_boxed_body());
} }
}; };
@ -330,15 +337,16 @@ impl App {
.await .await
{ {
Ok(Some(latest_rel)) => latest_rel.version.to_string(), Ok(Some(latest_rel)) => latest_rel.version.to_string(),
Ok(None) => return Ok(not_found()), Ok(None) => return Ok(not_found().map_into_boxed_body()),
Err(err) => { Err(err) => {
error!(logger, "error: {}", err); tracing::error!(%err);
let mut response = views::html::error::render( let mut response = views::html::error::render(
"Could not fetch crate information", "Could not fetch crate information",
"Please make sure to provide a valid crate name.", "Please make sure to provide a valid crate name.",
); );
*response.status_mut() = StatusCode::NOT_FOUND; *response.status_mut() = StatusCode::NOT_FOUND;
return Ok(response);
return Ok(response.map_into_boxed_body());
} }
} }
} }
@ -349,14 +357,16 @@ impl App {
match crate_path_result { match crate_path_result {
Err(err) => { Err(err) => {
error!(logger, "error: {}", err); tracing::error!(%err);
let mut response = views::html::error::render( let mut response = views::html::error::render(
"Could not parse crate path", "Could not parse crate path",
"Please make sure to provide a valid crate name and version.", "Please make sure to provide a valid crate name and version.",
); );
*response.status_mut() = StatusCode::BAD_REQUEST; *response.status_mut() = StatusCode::BAD_REQUEST;
Ok(response)
Ok(response.map_into_boxed_body())
} }
Ok(crate_path) => { Ok(crate_path) => {
let analyze_result = server let analyze_result = server
.engine .engine
@ -365,14 +375,15 @@ impl App {
match analyze_result { match analyze_result {
Err(err) => { Err(err) => {
error!(logger, "error: {}", err); tracing::error!(%err);
let response = App::status_format_analysis( let response = App::status_format_analysis(
None, None,
format, format,
SubjectPath::Crate(crate_path), SubjectPath::Crate(crate_path),
badge_knobs, badge_knobs,
); );
Ok(response)
Ok(response.map_into_boxed_body())
} }
Ok(analysis_outcome) => { Ok(analysis_outcome) => {
let response = App::status_format_analysis( let response = App::status_format_analysis(
@ -382,7 +393,7 @@ impl App {
badge_knobs, badge_knobs,
); );
Ok(response) Ok(response.map_into_boxed_body())
} }
} }
} }
@ -394,38 +405,41 @@ impl App {
format: StatusFormat, format: StatusFormat,
subject_path: SubjectPath, subject_path: SubjectPath,
badge_knobs: ExtraConfig, badge_knobs: ExtraConfig,
) -> Response<Body> { ) -> Response<impl MessageBody> {
match format { match format {
StatusFormat::Svg => views::badge::response(analysis_outcome.as_ref(), badge_knobs), StatusFormat::Svg => {
views::badge::response(analysis_outcome.as_ref(), badge_knobs).map_into_boxed_body()
}
StatusFormat::Html => { StatusFormat::Html => {
views::html::status::render(analysis_outcome, subject_path, badge_knobs) views::html::status::render(analysis_outcome, subject_path, badge_knobs)
.map_into_boxed_body()
} }
} }
} }
fn static_file(file: StaticFile) -> Response<Body> { fn static_file(file: StaticFile) -> Response<impl MessageBody> {
match file { match file {
StaticFile::StyleCss => Response::builder() StaticFile::StyleCss => Response::build(StatusCode::OK)
.header(CONTENT_TYPE, "text/css; charset=utf-8") .insert_header((CONTENT_TYPE, "text/css; charset=utf-8"))
.header(ETAG, STATIC_STYLE_CSS_ETAG) .insert_header((ETAG, STATIC_STYLE_CSS_ETAG))
.header(CACHE_CONTROL, "public, max-age=365000000, immutable") .insert_header((CACHE_CONTROL, "public, max-age=365000000, immutable"))
.body(Body::from(assets::STATIC_STYLE_CSS)) .body(assets::STATIC_STYLE_CSS),
.unwrap(),
StaticFile::FaviconPng => Response::builder() StaticFile::FaviconPng => Response::build(StatusCode::OK)
.header(CONTENT_TYPE, "image/svg+xml") .insert_header((CONTENT_TYPE, "image/svg+xml"))
.body(Body::from(assets::STATIC_FAVICON)) .body(assets::STATIC_FAVICON),
.unwrap(),
StaticFile::LinksJs => Response::builder() StaticFile::LinksJs => Response::build(StatusCode::OK)
.header(CONTENT_TYPE, "text/javascript; charset=utf-8") .insert_header((CONTENT_TYPE, "text/javascript; charset=utf-8"))
.header(ETAG, STATIC_LINKS_JS_ETAG) .insert_header((ETAG, STATIC_LINKS_JS_ETAG))
.header(CACHE_CONTROL, "public, max-age=365000000, immutable") .insert_header((CACHE_CONTROL, "public, max-age=365000000, immutable"))
.body(Body::from(assets::STATIC_LINKS_JS)) .body(assets::STATIC_LINKS_JS),
.unwrap(),
} }
} }
} }
fn not_found() -> Response<Body> { fn not_found() -> Response<impl MessageBody> {
views::html::error::render_404() views::html::error::render_404()
} }

View file

@ -1,9 +1,7 @@
use actix_http::{body::MessageBody, header::CONTENT_TYPE, Response, StatusCode};
use badge::{Badge, BadgeOptions}; use badge::{Badge, BadgeOptions};
use hyper::header::CONTENT_TYPE;
use hyper::{Body, Response};
use crate::engine::AnalyzeDependenciesOutcome; use crate::{engine::AnalyzeDependenciesOutcome, server::ExtraConfig};
use crate::server::ExtraConfig;
pub fn badge( pub fn badge(
analysis_outcome: Option<&AnalyzeDependenciesOutcome>, analysis_outcome: Option<&AnalyzeDependenciesOutcome>,
@ -75,11 +73,10 @@ pub fn badge(
pub fn response( pub fn response(
analysis_outcome: Option<&AnalyzeDependenciesOutcome>, analysis_outcome: Option<&AnalyzeDependenciesOutcome>,
badge_knobs: ExtraConfig, badge_knobs: ExtraConfig,
) -> Response<Body> { ) -> Response<impl MessageBody> {
let badge = badge(analysis_outcome, badge_knobs).to_svg(); let badge = badge(analysis_outcome, badge_knobs).to_svg();
Response::builder() Response::build(StatusCode::OK)
.header(CONTENT_TYPE, "image/svg+xml; charset=utf-8") .insert_header((CONTENT_TYPE, "image/svg+xml; charset=utf-8"))
.body(Body::from(badge)) .body(badge)
.unwrap()
} }

View file

@ -1,12 +1,13 @@
use hyper::{ use actix_http::{
body::MessageBody,
header::{CACHE_CONTROL, CONTENT_TYPE}, header::{CACHE_CONTROL, CONTENT_TYPE},
Body, Response, StatusCode, Response, StatusCode,
}; };
use maud::html; use maud::html;
use crate::server::assets::STATIC_STYLE_CSS_PATH; use crate::server::assets::STATIC_STYLE_CSS_PATH;
pub fn render(title: &str, descr: &str) -> Response<Body> { pub fn render(title: &str, descr: &str) -> Response<impl MessageBody> {
super::render_html( super::render_html(
title, title,
html! { html! {
@ -26,7 +27,7 @@ pub fn render(title: &str, descr: &str) -> Response<Body> {
) )
} }
pub fn render_404() -> Response<Body> { pub fn render_404() -> Response<impl MessageBody> {
let rendered = html! { let rendered = html! {
html { html {
head { head {
@ -55,10 +56,8 @@ pub fn render_404() -> Response<Body> {
} }
}; };
Response::builder() Response::build(StatusCode::NOT_FOUND)
.status(StatusCode::NOT_FOUND) .insert_header((CONTENT_TYPE, "text/html; charset=utf-8"))
.header(CONTENT_TYPE, "text/html; charset=utf-8") .insert_header((CACHE_CONTROL, "public, max-age=300, immutable"))
.header(CACHE_CONTROL, "public, max-age=300, immutable") .body(rendered.0)
.body(Body::from(rendered.0))
.unwrap()
} }

View file

@ -1,10 +1,10 @@
use hyper::{Body, Response}; use actix_http::{body::MessageBody, Response};
use maud::{html, Markup}; use maud::{html, Markup};
use crate::models::crates::CratePath; use crate::{
use crate::models::repo::Repository; models::{crates::CratePath, repo::Repository},
server::assets::STATIC_LINKS_JS_PATH,
use crate::server::assets::STATIC_LINKS_JS_PATH; };
fn link_forms() -> Markup { fn link_forms() -> Markup {
html! { html! {
@ -161,7 +161,10 @@ fn popular_table(popular_repos: Vec<Repository>, popular_crates: Vec<CratePath>)
} }
} }
pub fn render(popular_repos: Vec<Repository>, popular_crates: Vec<CratePath>) -> Response<Body> { pub fn render(
popular_repos: Vec<Repository>,
popular_crates: Vec<CratePath>,
) -> Response<impl MessageBody> {
super::render_html( super::render_html(
"Keep your dependencies up-to-date", "Keep your dependencies up-to-date",
html! { html! {

View file

@ -1,17 +1,15 @@
use std::time::Duration; use std::time::Duration;
use hyper::header::CONTENT_TYPE; use actix_http::{body::MessageBody, header::CONTENT_TYPE, Response, StatusCode};
use hyper::{Body, Response};
use maud::{html, Markup, Render, DOCTYPE}; use maud::{html, Markup, Render, DOCTYPE};
pub mod error; pub mod error;
pub mod index; pub mod index;
pub mod status; pub mod status;
use crate::server::assets::STATIC_STYLE_CSS_PATH; use crate::server::{assets::STATIC_STYLE_CSS_PATH, SELF_BASE_URL};
use crate::server::SELF_BASE_URL;
fn render_html<B: Render>(title: &str, body: B) -> Response<Body> { fn render_html<B: Render>(title: &str, body: B) -> Response<impl MessageBody> {
let rendered = html! { let rendered = html! {
(DOCTYPE) (DOCTYPE)
html { html {
@ -28,10 +26,9 @@ fn render_html<B: Render>(title: &str, body: B) -> Response<Body> {
} }
}; };
Response::builder() Response::build(StatusCode::OK)
.header(CONTENT_TYPE, "text/html; charset=utf-8") .insert_header((CONTENT_TYPE, "text/html; charset=utf-8"))
.body(Body::from(rendered.0)) .body(rendered.0)
.unwrap()
} }
fn render_navbar() -> Markup { fn render_navbar() -> Markup {

View file

@ -1,17 +1,20 @@
use actix_http::{body::MessageBody, Response};
use font_awesome_as_a_crate::{svg as fa, Type as FaType}; use font_awesome_as_a_crate::{svg as fa, Type as FaType};
use hyper::{Body, Response};
use indexmap::IndexMap; use indexmap::IndexMap;
use maud::{html, Markup, PreEscaped}; use maud::{html, Markup, PreEscaped};
use pulldown_cmark::{html, Parser}; use pulldown_cmark::{html, Parser};
use rustsec::advisory::Advisory; use rustsec::advisory::Advisory;
use semver::Version; use semver::Version;
use crate::engine::AnalyzeDependenciesOutcome; use crate::{
use crate::models::crates::{AnalyzedDependencies, AnalyzedDependency, CrateName}; engine::AnalyzeDependenciesOutcome,
use crate::models::repo::RepoSite; models::{
use crate::models::SubjectPath; crates::{AnalyzedDependencies, AnalyzedDependency, CrateName},
use crate::server::views::badge; repo::RepoSite,
use crate::server::ExtraConfig; SubjectPath,
},
server::{views::badge, ExtraConfig},
};
fn get_crates_url(name: impl AsRef<str>) -> String { fn get_crates_url(name: impl AsRef<str>) -> String {
format!("https://crates.io/crates/{}", name.as_ref()) format!("https://crates.io/crates/{}", name.as_ref())
@ -454,7 +457,7 @@ pub fn render(
analysis_outcome: Option<AnalyzeDependenciesOutcome>, analysis_outcome: Option<AnalyzeDependenciesOutcome>,
subject_path: SubjectPath, subject_path: SubjectPath,
extra_config: ExtraConfig, extra_config: ExtraConfig,
) -> Response<Body> { ) -> Response<impl MessageBody> {
let title = match subject_path { let title = match subject_path {
SubjectPath::Repo(ref repo_path) => { SubjectPath::Repo(ref repo_path) => {
format!("{} / {}", repo_path.qual.as_ref(), repo_path.name.as_ref()) format!("{} / {}", repo_path.qual.as_ref(), repo_path.name.as_ref())

View file

@ -1,9 +1,8 @@
use std::{fmt, sync::Arc, time::Duration}; use std::{fmt, sync::Arc, time::Duration};
use actix_service::Service;
use derive_more::{Display, Error, From}; use derive_more::{Display, Error, From};
use hyper::service::Service;
use lru_time_cache::LruCache; use lru_time_cache::LruCache;
use slog::{debug, Logger};
use tokio::sync::Mutex; use tokio::sync::Mutex;
#[derive(Debug, Clone, Display, From, Error)] #[derive(Debug, Clone, Display, From, Error)]
@ -18,7 +17,6 @@ where
{ {
inner: S, inner: S,
cache: Arc<Mutex<LruCache<Req, S::Response>>>, cache: Arc<Mutex<LruCache<Req, S::Response>>>,
logger: Logger,
} }
impl<S, Req> fmt::Debug for Cache<S, Req> impl<S, Req> fmt::Debug for Cache<S, Req>
@ -28,7 +26,7 @@ where
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("Cache") fmt.debug_struct("Cache")
.field("inner", &self.inner) .field("inner", &self.inner)
.finish() .finish_non_exhaustive()
} }
} }
@ -38,13 +36,12 @@ where
S::Response: Clone, S::Response: Clone,
Req: Clone + Eq + Ord + fmt::Debug, Req: Clone + Eq + Ord + fmt::Debug,
{ {
pub fn new(service: S, ttl: Duration, capacity: usize, logger: Logger) -> Cache<S, Req> { pub fn new(service: S, ttl: Duration, capacity: usize) -> Cache<S, Req> {
let cache = LruCache::with_expiry_duration_and_capacity(ttl, capacity); let cache = LruCache::with_expiry_duration_and_capacity(ttl, capacity);
Cache { Cache {
inner: service, inner: service,
cache: Arc::new(Mutex::new(cache)), cache: Arc::new(Mutex::new(cache)),
logger,
} }
} }
@ -53,22 +50,22 @@ where
let mut cache = self.cache.lock().await; let mut cache = self.cache.lock().await;
if let Some(cached_response) = cache.get(&req) { if let Some(cached_response) = cache.get(&req) {
debug!( tracing::debug!(
self.logger, "cache hit"; svc = ?self.inner,
"svc" => format!("{:?}", self.inner), req = ?req,
"req" => format!("{:?}", &req) cache = "hit",
); );
return Ok(cached_response.clone()); return Ok(cached_response.clone());
} }
} }
debug!( tracing::debug!(
self.logger, "cache miss"; svc = ?self.inner,
"svc" => format!("{:?}", self.inner), req = ?req,
"req" => format!("{:?}", &req) cache = "miss",
); );
let mut service = self.inner.clone(); let service = self.inner.clone();
let fresh = service.call(req.clone()).await?; let fresh = service.call(req.clone()).await?;
{ {

View file

@ -1,26 +1,28 @@
use std::sync::Arc; use std::{
use std::sync::Mutex; sync::{Arc, Mutex},
use std::time::Duration; time::Duration,
};
use anyhow::Result;
use crates_index::{Crate, GitIndex};
use tokio::{
task::spawn_blocking,
time::{self, MissedTickBehavior},
};
use crate::models::crates::CrateName; use crate::models::crates::CrateName;
use anyhow::Result;
use crates_index::Crate;
use crates_index::GitIndex;
use slog::{error, Logger};
use tokio::task::spawn_blocking;
use tokio::time::{self, MissedTickBehavior};
#[derive(Clone)] #[derive(Clone)]
pub struct ManagedIndex { pub struct ManagedIndex {
index: Arc<Mutex<GitIndex>>, index: Arc<Mutex<GitIndex>>,
logger: Logger,
} }
impl ManagedIndex { impl ManagedIndex {
pub fn new(logger: Logger) -> Self { pub fn new() -> Self {
// the index path is configurable through the `CARGO_HOME` env variable // the index path is configurable through the `CARGO_HOME` env variable
let index = Arc::new(Mutex::new(GitIndex::new_cargo_default().unwrap())); let index = Arc::new(Mutex::new(GitIndex::new_cargo_default().unwrap()));
Self { index, logger }
Self { index }
} }
pub fn crate_(&self, crate_name: CrateName) -> Option<Crate> { pub fn crate_(&self, crate_name: CrateName) -> Option<Crate> {
@ -34,10 +36,9 @@ impl ManagedIndex {
update_interval.set_missed_tick_behavior(MissedTickBehavior::Delay); update_interval.set_missed_tick_behavior(MissedTickBehavior::Delay);
loop { loop {
if let Err(e) = self.refresh().await { if let Err(err) = self.refresh().await {
error!( tracing::error!(
self.logger, "failed refreshing the crates.io-index, the operation will be retried: {err}"
"failed refreshing the crates.io-index, the operation will be retried: {}", e
); );
} }
update_interval.tick().await; update_interval.tick().await;