From 691d9ba2694fe6bca33d17f83f54c675f87a8fed Mon Sep 17 00:00:00 2001 From: tm26a21p Date: Fri, 14 Jun 2024 17:58:05 -0700 Subject: [PATCH] feat: some proper data about user ip and location --- Cargo.lock | 505 ++++++++++++++++++++++++++++++-- Cargo.toml | 3 +- src/htmx_routes.rs | 60 ++++ src/htmx_templates.rs | 23 +- src/main.rs | 2 + src/project.rs | 6 +- src/routes.rs | 13 +- src/state.rs | 22 ++ src/templates.rs | 9 +- src/utils.rs | 1 + templates/components/stats.html | 8 +- templates/htmx/location.html | 12 + templates/pages/metrics.html | 9 +- 13 files changed, 625 insertions(+), 48 deletions(-) create mode 100644 templates/htmx/location.html diff --git a/Cargo.lock b/Cargo.lock index c5e0cfd..7eec234 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -74,7 +74,7 @@ checksum = "a41603f7cdbf5ac4af60760f17253eb6adf6ec5b6f14a7ed830cf687d375f163" dependencies = [ "askama", "axum-core", - "http", + "http 1.1.0", ] [[package]] @@ -135,6 +135,12 @@ dependencies = [ "syn 2.0.64", ] +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + [[package]] name = "autocfg" version = "1.3.0" @@ -151,10 +157,10 @@ dependencies = [ "axum-core", "bytes", "futures-util", - "http", - "http-body", + "http 1.1.0", + "http-body 1.0.0", "http-body-util", - "hyper", + "hyper 1.3.1", "hyper-util", "itoa 1.0.11", "matchit", @@ -184,8 +190,8 @@ dependencies = [ "async-trait", "bytes", "futures-util", - "http", - "http-body", + "http 1.1.0", + "http-body 1.0.0", "http-body-util", "mime", "pin-project-lite", @@ -428,6 +434,37 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" +[[package]] +name = "encoding_rs" +version = "0.8.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "errno" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "fastrand" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" + [[package]] name = "flate2" version = "1.0.30" @@ -444,6 +481,21 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + [[package]] name = "form_urlencoded" version = "1.2.1" @@ -600,6 +652,50 @@ version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +[[package]] +name = "h2" +version = "0.3.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http 0.2.12", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "h2" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa82e28a107a8cc405f0839610bdc9b15f1e25ec7d696aa5cf173edbcb1486ab" +dependencies = [ + "atomic-waker", + "bytes", + "fnv", + "futures-core", + "futures-sink", + "http 1.1.0", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + [[package]] name = "heck" version = "0.5.0" @@ -626,6 +722,17 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "http" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" +dependencies = [ + "bytes", + "fnv", + "itoa 1.0.11", +] + [[package]] name = "http" version = "1.1.0" @@ -637,6 +744,17 @@ dependencies = [ "itoa 1.0.11", ] +[[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]] name = "http-body" version = "1.0.0" @@ -644,7 +762,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" dependencies = [ "bytes", - "http", + "http 1.1.0", ] [[package]] @@ -655,8 +773,8 @@ checksum = "0475f8b2ac86659c21b64320d5d653f9efe42acd2a4e560073ec61a155a34f1d" dependencies = [ "bytes", "futures-core", - "http", - "http-body", + "http 1.1.0", + "http-body 1.0.0", "pin-project-lite", ] @@ -687,6 +805,30 @@ dependencies = [ "libm", ] +[[package]] +name = "hyper" +version = "0.14.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f361cde2f109281a220d4307746cdfd5ee3f410da58a70377762396775634b33" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2 0.3.26", + "http 0.2.12", + "http-body 0.4.6", + "httparse", + "httpdate", + "itoa 1.0.11", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", +] + [[package]] name = "hyper" version = "1.3.1" @@ -696,8 +838,9 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "http", - "http-body", + "h2 0.4.5", + "http 1.1.0", + "http-body 1.0.0", "httparse", "httpdate", "itoa 1.0.11", @@ -714,8 +857,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0bea761b46ae2b24eb4aef630d8d1c398157b6fc29e6350ecf090a0b70c952c" dependencies = [ "futures-util", - "http", - "hyper", + "http 1.1.0", + "hyper 1.3.1", "hyper-util", "log", "rustls", @@ -732,13 +875,42 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3203a961e5c83b6f5498933e78b6b263e208c197b63e9c6c53cc82ffd3f63793" dependencies = [ - "hyper", + "hyper 1.3.1", "hyper-util", "pin-project-lite", "tokio", "tower-service", ] +[[package]] +name = "hyper-tls" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +dependencies = [ + "bytes", + "hyper 0.14.29", + "native-tls", + "tokio", + "tokio-native-tls", +] + +[[package]] +name = "hyper-tls" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" +dependencies = [ + "bytes", + "http-body-util", + "hyper 1.3.1", + "hyper-util", + "native-tls", + "tokio", + "tokio-native-tls", + "tower-service", +] + [[package]] name = "hyper-util" version = "0.1.3" @@ -748,9 +920,9 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "http", - "http-body", - "hyper", + "http 1.1.0", + "http-body 1.0.0", + "hyper 1.3.1", "pin-project-lite", "socket2", "tokio", @@ -792,6 +964,35 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "indexmap" +version = "2.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "ipgeolocate" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03ef02a710095765c2af4fe51101c5177c17f910bb1d0d51aae1589e6da6d6cc" +dependencies = [ + "futures", + "reqwest 0.11.27", + "serde_json", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "ipnet" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" + [[package]] name = "iri-string" version = "0.7.2" @@ -865,6 +1066,12 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" +[[package]] +name = "linux-raw-sys" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" + [[package]] name = "lock_api" version = "0.4.12" @@ -970,6 +1177,23 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "native-tls" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8614eb2c83d59d1c8cc974dd3f920198647674a0a035e1af1fa58707e317466" +dependencies = [ + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + [[package]] name = "new_debug_unreachable" version = "1.0.6" @@ -1070,10 +1294,10 @@ dependencies = [ "either", "futures", "futures-util", - "http", - "http-body", + "http 1.1.0", + "http-body 1.0.0", "http-body-util", - "hyper", + "hyper 1.3.1", "hyper-rustls", "hyper-timeout", "hyper-util", @@ -1100,12 +1324,50 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "openssl" +version = "0.10.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95a0481286a310808298130d22dd1fef0fa571e05a8f44ec801801e84b216b1f" +dependencies = [ + "bitflags 2.5.0", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.64", +] + [[package]] name = "openssl-probe" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" +[[package]] +name = "openssl-sys" +version = "0.9.102" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c597637d56fbc83893a35eb0dd04b2b8e7a50c91e64e9493e398b5df4fb45fa2" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "overload" version = "0.1.1" @@ -1272,10 +1534,12 @@ dependencies = [ "axum", "chrono", "dotenv", + "ipgeolocate", "lazy_static", "markdown", "octocrab", "rand 0.8.5", + "reqwest 0.12.4", "scraper", "serde", "tokio", @@ -1417,6 +1681,88 @@ dependencies = [ "bitflags 2.5.0", ] +[[package]] +name = "reqwest" +version = "0.11.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd67538700a17451e7cba03ac727fb961abb7607553461627b97de0b89cf4a62" +dependencies = [ + "base64 0.21.7", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "h2 0.3.26", + "http 0.2.12", + "http-body 0.4.6", + "hyper 0.14.29", + "hyper-tls 0.5.0", + "ipnet", + "js-sys", + "log", + "mime", + "native-tls", + "once_cell", + "percent-encoding", + "pin-project-lite", + "rustls-pemfile 1.0.4", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper 0.1.2", + "system-configuration", + "tokio", + "tokio-native-tls", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "winreg 0.50.0", +] + +[[package]] +name = "reqwest" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "566cafdd92868e0939d3fb961bd0dc25fcfaaed179291093b3d43e6b3150ea10" +dependencies = [ + "base64 0.22.1", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "h2 0.4.5", + "http 1.1.0", + "http-body 1.0.0", + "http-body-util", + "hyper 1.3.1", + "hyper-tls 0.6.0", + "hyper-util", + "ipnet", + "js-sys", + "log", + "mime", + "native-tls", + "once_cell", + "percent-encoding", + "pin-project-lite", + "rustls-pemfile 2.1.2", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper 0.1.2", + "system-configuration", + "tokio", + "tokio-native-tls", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "winreg 0.52.0", +] + [[package]] name = "ring" version = "0.17.8" @@ -1447,6 +1793,19 @@ dependencies = [ "semver", ] +[[package]] +name = "rustix" +version = "0.38.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" +dependencies = [ + "bitflags 2.5.0", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", +] + [[package]] name = "rustls" version = "0.22.4" @@ -1468,12 +1827,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f1fb85efa936c42c6d5fc28d2629bb51e4b2f4b8a5211e297d599cc5a093792" dependencies = [ "openssl-probe", - "rustls-pemfile", + "rustls-pemfile 2.1.2", "rustls-pki-types", "schannel", "security-framework", ] +[[package]] +name = "rustls-pemfile" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c" +dependencies = [ + "base64 0.21.7", +] + [[package]] name = "rustls-pemfile" version = "2.1.2" @@ -1825,6 +2193,39 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394" +[[package]] +name = "system-configuration" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "system-configuration-sys", +] + +[[package]] +name = "system-configuration-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75fb188eb626b924683e3b95e3a48e63551fcfb51949de2f06a9d91dbee93c9" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "tempfile" +version = "3.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" +dependencies = [ + "cfg-if", + "fastrand", + "rustix", + "windows-sys 0.52.0", +] + [[package]] name = "tendril" version = "0.4.3" @@ -1948,6 +2349,16 @@ dependencies = [ "syn 2.0.64", ] +[[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + [[package]] name = "tokio-rustls" version = "0.25.0" @@ -2001,8 +2412,8 @@ dependencies = [ "bytes", "futures-core", "futures-util", - "http", - "http-body", + "http 1.1.0", + "http-body 1.0.0", "http-body-util", "http-range-header", "httpdate", @@ -2177,6 +2588,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version_check" version = "0.9.4" @@ -2229,6 +2646,18 @@ dependencies = [ "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "wasm-bindgen-macro" version = "0.2.92" @@ -2258,6 +2687,16 @@ version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" +[[package]] +name = "web-sys" +version = "0.3.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "winapi" version = "0.3.9" @@ -2428,6 +2867,26 @@ version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" +[[package]] +name = "winreg" +version = "0.50.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" +dependencies = [ + "cfg-if", + "windows-sys 0.48.0", +] + +[[package]] +name = "winreg" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a277a57398d4bfa075df44f501a17cfdf8542d224f0d36095a2adc7aee4ef0a5" +dependencies = [ + "cfg-if", + "windows-sys 0.48.0", +] + [[package]] name = "zeroize" version = "1.8.1" diff --git a/Cargo.toml b/Cargo.toml index f904964..76e2dd3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,8 @@ tower = "0.4.13" octocrab = "0.38.0" dotenv = "0.15.0" chrono = "0.4.38" +reqwest = { version = "0.12.4", features = ["json"] } +ipgeolocate = "0.3.5" [profile.dev.package.askama_derive] opt-level = 3 - diff --git a/src/htmx_routes.rs b/src/htmx_routes.rs index 5ec418c..6ff7203 100644 --- a/src/htmx_routes.rs +++ b/src/htmx_routes.rs @@ -56,3 +56,63 @@ pub async fn set_theme( state.set_theme(payload.theme); (StatusCode::OK, "Theme set".into_response()) } + +async fn get_ip() -> String +{ + reqwest::get("https://api.ipify.org") + .await + .expect("Failed to get ip") + .text() + .await + .expect("Failed to get ip") +} + +pub async fn ip_adress( + Extension(state): Extension +) -> impl IntoResponse +{ + let ip = get_ip().await; + + let mut metrics = state.metrics.write().unwrap(); + metrics.ip = ip.clone(); + drop(metrics); + (StatusCode::OK, ip.into_response()) +} + +use ipgeolocate::{Locator, Service}; + +async fn get_location(ip: &str) -> Option +{ + let service = Service::IpApi; + match Locator::get(ip, service).await { + Ok(response) => { + Some(GeoLocation { + latitude: response.latitude, + longitude: response.longitude, + city: response.city, + region: response.region, + country: response.country, + timezone: response.timezone, + }) + } + Err(error) => { + eprintln!("Failed to get location: {:?}", error); + None + } + } +} + +pub async fn location( + Extension(_state): Extension +) -> impl IntoResponse +{ + let ip = get_ip().await; + + let location = get_location(&ip).await.unwrap(); + // let's acually return a jinja template with the location + + let reply_html = LocationT { location } + .render() + .expect("Failed to render template"); + (StatusCode::OK, Html(reply_html).into_response()) +} diff --git a/src/htmx_templates.rs b/src/htmx_templates.rs index 9bb45b7..ebb149d 100644 --- a/src/htmx_templates.rs +++ b/src/htmx_templates.rs @@ -1,9 +1,28 @@ use askama_axum::Template; + use crate::project::Project; #[derive(Template)] #[template(path = "htmx/tabs.html")] -pub struct TabsT { +pub struct TabsT +{ pub liked: bool, pub projects: Vec, -} \ No newline at end of file +} + +#[derive(Template)] +#[template(path = "htmx/location.html")] +pub struct LocationT +{ + pub location: GeoLocation, +} + +pub struct GeoLocation +{ + pub latitude: String, + pub longitude: String, + pub city: String, + pub region: String, + pub country: String, + pub timezone: String, +} diff --git a/src/main.rs b/src/main.rs index c2eb0fc..7b25ba0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,6 +37,8 @@ async fn main() .route("/api/projects", get(projects)) .route("/api/projects-liked", get(projects_liked)) .route("/api/set-theme", post(set_theme)) + .route("/api/ip", get(ip_adress)) + .route("/api/location", get(location)) .nest("/public", using_serve_dir()) .layer(Extension(state.clone())); diff --git a/src/project.rs b/src/project.rs index 6fe3ead..ba05457 100644 --- a/src/project.rs +++ b/src/project.rs @@ -17,7 +17,7 @@ pub struct Project impl Project { - pub fn new(url: &str) -> Self + pub fn _new(url: &str) -> Self { Self { url: url.to_string(), @@ -104,7 +104,9 @@ impl Project .collect() } - pub async fn get_repositories_liked(octo: Octocrab) -> octocrab::Result> + pub async fn get_repositories_liked( + octo: Octocrab + ) -> octocrab::Result> { let repos = Project::fetch_user_repositories(octo).await?; let projects = Project::process_repositories(repos); diff --git a/src/routes.rs b/src/routes.rs index 7e2a831..64c88ce 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -32,17 +32,20 @@ pub async fn metrics_page( title: state.name.clone() + " - Metrics", daisy_theme: state.get_theme(), }; + + let mut metrics = state.metrics.write().unwrap(); + metrics.visited += 1; + let metrics_clone = metrics.clone(); + drop(metrics); + let template = MetricsT { base, - likes: 0, - likes_ratio_over_last_month: 0.0, - views: 0, - view_ratio_over_last_month: 0.0, + metrics: metrics_clone, }; + let reply_html = template.render().expect("Failed to render template"); (StatusCode::OK, Html(reply_html).into_response()) } - pub async fn projects_page( Extension(state): Extension ) -> impl IntoResponse diff --git a/src/state.rs b/src/state.rs index febff6c..41f58f0 100644 --- a/src/state.rs +++ b/src/state.rs @@ -10,6 +10,7 @@ pub struct Common pub daisy_theme: Arc>, pub _github_token: String, pub octocrab: octocrab::Octocrab, + pub metrics: Arc>, } impl Common @@ -27,6 +28,7 @@ impl Common .personal_token(github_token) .build() .expect("Failed to create Octocrab instance."), + metrics: Arc::new(RwLock::new(Metrics::new())), } } @@ -44,3 +46,23 @@ impl Common *theme = new_theme; } } + +#[derive(Debug, Clone)] +pub struct Metrics +{ + pub visited: usize, + pub ip: String, + pub location: String, +} + +impl Metrics +{ + pub fn new() -> Self + { + Self { + visited: 1, + ip: "Unknown".to_string(), + location: "Unknown".to_string(), + } + } +} diff --git a/src/templates.rs b/src/templates.rs index 5a1435e..68b1d39 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -1,6 +1,6 @@ use askama_axum::Template; -use crate::project::Project; +use crate::{htmx_templates::GeoLocation, project::Project, state::Metrics}; #[derive(Template)] #[template(path = "base.html")] @@ -23,10 +23,7 @@ pub struct IndexT pub struct MetricsT { pub base: BaseT, - pub likes: usize, - pub likes_ratio_over_last_month: f64, - pub views: usize, - pub view_ratio_over_last_month: f64, + pub metrics: Metrics, } #[derive(Template)] @@ -42,4 +39,4 @@ pub struct ProjectsT pub struct PlaygroundT { pub base: BaseT, -} \ No newline at end of file +} diff --git a/src/utils.rs b/src/utils.rs index d0c1aa9..ae1c099 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -44,6 +44,7 @@ pub fn read_daisy_theme_config(file_path: &str) -> Vec } use rand::Rng; + // Function to return a random number between 0 and `range` pub fn random_number(range: u32) -> u32 { diff --git a/templates/components/stats.html b/templates/components/stats.html index 17d169e..661cd8f 100644 --- a/templates/components/stats.html +++ b/templates/components/stats.html @@ -10,8 +10,8 @@
Total Likes
-
{{ likes }}
-
{{ likes_ratio_over_last_month }}% more than last month
+
data
+
data more than last month
@@ -23,8 +23,8 @@
Website Views
-
{{ views }}
-
{{ view_ratio_over_last_month }}% more than last month
+
data
+
data more than last month
diff --git a/templates/htmx/location.html b/templates/htmx/location.html new file mode 100644 index 0000000..1296dd2 --- /dev/null +++ b/templates/htmx/location.html @@ -0,0 +1,12 @@ + + Location + {{ location.country }}, {{ location.city }}, {{ location.region }} + + + Latitude & Longitude + {{location.latitude}} | {{location.longitude}} + + + Timezone + {{location.timezone}} + \ No newline at end of file diff --git a/templates/pages/metrics.html b/templates/pages/metrics.html index 535777e..d0638dd 100644 --- a/templates/pages/metrics.html +++ b/templates/pages/metrics.html @@ -23,12 +23,11 @@

What I Know About Y IP Address - 192.168.1.1 - - - Location - San Francisco, CA, USA + Unknown + address + + Device Type Desktop