A PLC Mirror written in Rust

db connection

+314 -1
Cargo.lock
··· 132 132 checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf" 133 133 134 134 [[package]] 135 + name = "byteorder" 136 + version = "1.5.0" 137 + source = "registry+https://github.com/rust-lang/crates.io-index" 138 + checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" 139 + 140 + [[package]] 135 141 name = "bytes" 136 142 version = "1.10.1" 137 143 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 273 279 dependencies = [ 274 280 "data-encoding", 275 281 "syn", 282 + ] 283 + 284 + [[package]] 285 + name = "deadpool" 286 + version = "0.12.2" 287 + source = "registry+https://github.com/rust-lang/crates.io-index" 288 + checksum = "5ed5957ff93768adf7a65ab167a17835c3d2c3c50d084fe305174c112f468e2f" 289 + dependencies = [ 290 + "deadpool-runtime", 291 + "num_cpus", 292 + "tokio", 293 + ] 294 + 295 + [[package]] 296 + name = "deadpool-postgres" 297 + version = "0.14.1" 298 + source = "registry+https://github.com/rust-lang/crates.io-index" 299 + checksum = "3d697d376cbfa018c23eb4caab1fd1883dd9c906a8c034e8d9a3cb06a7e0bef9" 300 + dependencies = [ 301 + "async-trait", 302 + "deadpool", 303 + "getrandom 0.2.16", 304 + "tokio", 305 + "tokio-postgres", 306 + "tracing", 307 + ] 308 + 309 + [[package]] 310 + name = "deadpool-runtime" 311 + version = "0.1.4" 312 + source = "registry+https://github.com/rust-lang/crates.io-index" 313 + checksum = "092966b41edc516079bdf31ec78a2e0588d1d0c08f78b91d8307215928642b2b" 314 + dependencies = [ 315 + "tokio", 276 316 ] 277 317 278 318 [[package]] ··· 298 338 dependencies = [ 299 339 "block-buffer", 300 340 "crypto-common", 341 + "subtle", 301 342 ] 302 343 303 344 [[package]] ··· 439 480 ] 440 481 441 482 [[package]] 483 + name = "fallible-iterator" 484 + version = "0.2.0" 485 + source = "registry+https://github.com/rust-lang/crates.io-index" 486 + checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" 487 + 488 + [[package]] 442 489 name = "fastrand" 443 490 version = "2.3.0" 444 491 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 580 627 checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592" 581 628 dependencies = [ 582 629 "cfg-if", 630 + "js-sys", 583 631 "libc", 584 632 "wasi 0.11.0+wasi-snapshot-preview1", 633 + "wasm-bindgen", 585 634 ] 586 635 587 636 [[package]] ··· 635 684 636 685 [[package]] 637 686 name = "hermit-abi" 687 + version = "0.3.9" 688 + source = "registry+https://github.com/rust-lang/crates.io-index" 689 + checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" 690 + 691 + [[package]] 692 + name = "hermit-abi" 638 693 version = "0.5.0" 639 694 source = "registry+https://github.com/rust-lang/crates.io-index" 640 695 checksum = "fbd780fe5cc30f81464441920d82ac8740e2e46b29a6fad543ddd075229ce37e" 641 696 642 697 [[package]] 698 + name = "hmac" 699 + version = "0.12.1" 700 + source = "registry+https://github.com/rust-lang/crates.io-index" 701 + checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" 702 + dependencies = [ 703 + "digest", 704 + ] 705 + 706 + [[package]] 643 707 name = "hostname" 644 708 version = "0.3.1" 645 709 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 984 1048 source = "registry+https://github.com/rust-lang/crates.io-index" 985 1049 checksum = "e04d7f318608d35d4b61ddd75cbdaee86b023ebe2bd5a66ee0915f0bf93095a9" 986 1050 dependencies = [ 987 - "hermit-abi", 1051 + "hermit-abi 0.5.0", 988 1052 "libc", 989 1053 "windows-sys 0.59.0", 990 1054 ] ··· 1060 1124 version = "0.1.0" 1061 1125 source = "registry+https://github.com/rust-lang/crates.io-index" 1062 1126 checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" 1127 + 1128 + [[package]] 1129 + name = "md-5" 1130 + version = "0.10.6" 1131 + source = "registry+https://github.com/rust-lang/crates.io-index" 1132 + checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" 1133 + dependencies = [ 1134 + "cfg-if", 1135 + "digest", 1136 + ] 1063 1137 1064 1138 [[package]] 1065 1139 name = "memchr" ··· 1165 1239 ] 1166 1240 1167 1241 [[package]] 1242 + name = "num_cpus" 1243 + version = "1.16.0" 1244 + source = "registry+https://github.com/rust-lang/crates.io-index" 1245 + checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" 1246 + dependencies = [ 1247 + "hermit-abi 0.3.9", 1248 + "libc", 1249 + ] 1250 + 1251 + [[package]] 1168 1252 name = "num_threads" 1169 1253 version = "0.1.7" 1170 1254 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 1279 1363 checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" 1280 1364 1281 1365 [[package]] 1366 + name = "phf" 1367 + version = "0.11.3" 1368 + source = "registry+https://github.com/rust-lang/crates.io-index" 1369 + checksum = "1fd6780a80ae0c52cc120a26a1a42c1ae51b247a253e4e06113d23d2c2edd078" 1370 + dependencies = [ 1371 + "phf_shared", 1372 + ] 1373 + 1374 + [[package]] 1375 + name = "phf_shared" 1376 + version = "0.11.3" 1377 + source = "registry+https://github.com/rust-lang/crates.io-index" 1378 + checksum = "67eabc2ef2a60eb7faa00097bd1ffdb5bd28e62bf39990626a582201b7a754e5" 1379 + dependencies = [ 1380 + "siphasher", 1381 + ] 1382 + 1383 + [[package]] 1282 1384 name = "pin-project-lite" 1283 1385 version = "0.2.16" 1284 1386 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 1301 1403 version = "0.1.0" 1302 1404 dependencies = [ 1303 1405 "chrono", 1406 + "deadpool-postgres", 1304 1407 "dropshot", 1305 1408 "eyre", 1306 1409 "ipld-core", ··· 1311 1414 "slog-scope", 1312 1415 "slog-stdlog", 1313 1416 "tokio", 1417 + "tokio-postgres", 1418 + ] 1419 + 1420 + [[package]] 1421 + name = "postgres-protocol" 1422 + version = "0.6.8" 1423 + source = "registry+https://github.com/rust-lang/crates.io-index" 1424 + checksum = "76ff0abab4a9b844b93ef7b81f1efc0a366062aaef2cd702c76256b5dc075c54" 1425 + dependencies = [ 1426 + "base64", 1427 + "byteorder", 1428 + "bytes", 1429 + "fallible-iterator", 1430 + "hmac", 1431 + "md-5", 1432 + "memchr", 1433 + "rand", 1434 + "sha2", 1435 + "stringprep", 1436 + ] 1437 + 1438 + [[package]] 1439 + name = "postgres-types" 1440 + version = "0.2.9" 1441 + source = "registry+https://github.com/rust-lang/crates.io-index" 1442 + checksum = "613283563cd90e1dfc3518d548caee47e0e725455ed619881f5cf21f36de4b48" 1443 + dependencies = [ 1444 + "bytes", 1445 + "chrono", 1446 + "fallible-iterator", 1447 + "postgres-protocol", 1448 + "serde", 1449 + "serde_json", 1314 1450 ] 1315 1451 1316 1452 [[package]] ··· 1320 1456 checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" 1321 1457 1322 1458 [[package]] 1459 + name = "ppv-lite86" 1460 + version = "0.2.21" 1461 + source = "registry+https://github.com/rust-lang/crates.io-index" 1462 + checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" 1463 + dependencies = [ 1464 + "zerocopy", 1465 + ] 1466 + 1467 + [[package]] 1323 1468 name = "proc-macro2" 1324 1469 version = "1.0.95" 1325 1470 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 1344 1489 checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5" 1345 1490 1346 1491 [[package]] 1492 + name = "rand" 1493 + version = "0.9.1" 1494 + source = "registry+https://github.com/rust-lang/crates.io-index" 1495 + checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97" 1496 + dependencies = [ 1497 + "rand_chacha", 1498 + "rand_core", 1499 + ] 1500 + 1501 + [[package]] 1502 + name = "rand_chacha" 1503 + version = "0.9.0" 1504 + source = "registry+https://github.com/rust-lang/crates.io-index" 1505 + checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" 1506 + dependencies = [ 1507 + "ppv-lite86", 1508 + "rand_core", 1509 + ] 1510 + 1511 + [[package]] 1512 + name = "rand_core" 1513 + version = "0.9.3" 1514 + source = "registry+https://github.com/rust-lang/crates.io-index" 1515 + checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" 1516 + dependencies = [ 1517 + "getrandom 0.3.2", 1518 + ] 1519 + 1520 + [[package]] 1347 1521 name = "redox_syscall" 1348 1522 version = "0.5.11" 1349 1523 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 1692 1866 ] 1693 1867 1694 1868 [[package]] 1869 + name = "sha2" 1870 + version = "0.10.9" 1871 + source = "registry+https://github.com/rust-lang/crates.io-index" 1872 + checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" 1873 + dependencies = [ 1874 + "cfg-if", 1875 + "cpufeatures", 1876 + "digest", 1877 + ] 1878 + 1879 + [[package]] 1695 1880 name = "shlex" 1696 1881 version = "1.3.0" 1697 1882 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 1705 1890 dependencies = [ 1706 1891 "libc", 1707 1892 ] 1893 + 1894 + [[package]] 1895 + name = "siphasher" 1896 + version = "1.0.1" 1897 + source = "registry+https://github.com/rust-lang/crates.io-index" 1898 + checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d" 1708 1899 1709 1900 [[package]] 1710 1901 name = "slab" ··· 1821 2012 checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" 1822 2013 1823 2014 [[package]] 2015 + name = "stringprep" 2016 + version = "0.1.5" 2017 + source = "registry+https://github.com/rust-lang/crates.io-index" 2018 + checksum = "7b4df3d392d81bd458a8a621b8bffbd2302a12ffe288a9d931670948749463b1" 2019 + dependencies = [ 2020 + "unicode-bidi", 2021 + "unicode-normalization", 2022 + "unicode-properties", 2023 + ] 2024 + 2025 + [[package]] 1824 2026 name = "subtle" 1825 2027 version = "2.6.1" 1826 2028 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 2002 2204 ] 2003 2205 2004 2206 [[package]] 2207 + name = "tinyvec" 2208 + version = "1.9.0" 2209 + source = "registry+https://github.com/rust-lang/crates.io-index" 2210 + checksum = "09b3661f17e86524eccd4371ab0429194e0d7c008abb45f7a7495b1719463c71" 2211 + dependencies = [ 2212 + "tinyvec_macros", 2213 + ] 2214 + 2215 + [[package]] 2216 + name = "tinyvec_macros" 2217 + version = "0.1.1" 2218 + source = "registry+https://github.com/rust-lang/crates.io-index" 2219 + checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" 2220 + 2221 + [[package]] 2005 2222 name = "tokio" 2006 2223 version = "1.44.2" 2007 2224 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 2041 2258 ] 2042 2259 2043 2260 [[package]] 2261 + name = "tokio-postgres" 2262 + version = "0.7.13" 2263 + source = "registry+https://github.com/rust-lang/crates.io-index" 2264 + checksum = "6c95d533c83082bb6490e0189acaa0bbeef9084e60471b696ca6988cd0541fb0" 2265 + dependencies = [ 2266 + "async-trait", 2267 + "byteorder", 2268 + "bytes", 2269 + "fallible-iterator", 2270 + "futures-channel", 2271 + "futures-util", 2272 + "log", 2273 + "parking_lot", 2274 + "percent-encoding", 2275 + "phf", 2276 + "pin-project-lite", 2277 + "postgres-protocol", 2278 + "postgres-types", 2279 + "rand", 2280 + "socket2", 2281 + "tokio", 2282 + "tokio-util", 2283 + "whoami", 2284 + ] 2285 + 2286 + [[package]] 2044 2287 name = "tokio-rustls" 2045 2288 version = "0.25.0" 2046 2289 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 2149 2392 checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" 2150 2393 dependencies = [ 2151 2394 "pin-project-lite", 2395 + "tracing-attributes", 2152 2396 "tracing-core", 2397 + ] 2398 + 2399 + [[package]] 2400 + name = "tracing-attributes" 2401 + version = "0.1.28" 2402 + source = "registry+https://github.com/rust-lang/crates.io-index" 2403 + checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" 2404 + dependencies = [ 2405 + "proc-macro2", 2406 + "quote", 2407 + "syn", 2153 2408 ] 2154 2409 2155 2410 [[package]] ··· 2174 2429 checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f" 2175 2430 2176 2431 [[package]] 2432 + name = "unicode-bidi" 2433 + version = "0.3.18" 2434 + source = "registry+https://github.com/rust-lang/crates.io-index" 2435 + checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5" 2436 + 2437 + [[package]] 2177 2438 name = "unicode-ident" 2178 2439 version = "1.0.18" 2179 2440 source = "registry+https://github.com/rust-lang/crates.io-index" 2180 2441 checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" 2181 2442 2182 2443 [[package]] 2444 + name = "unicode-normalization" 2445 + version = "0.1.24" 2446 + source = "registry+https://github.com/rust-lang/crates.io-index" 2447 + checksum = "5033c97c4262335cded6d6fc3e5c18ab755e1a3dc96376350f3d8e9f009ad956" 2448 + dependencies = [ 2449 + "tinyvec", 2450 + ] 2451 + 2452 + [[package]] 2453 + name = "unicode-properties" 2454 + version = "0.1.3" 2455 + source = "registry+https://github.com/rust-lang/crates.io-index" 2456 + checksum = "e70f2a8b45122e719eb623c01822704c4e0907e7e426a05927e1a1cfff5b75d0" 2457 + 2458 + [[package]] 2183 2459 name = "unsigned-varint" 2184 2460 version = "0.8.0" 2185 2461 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 2270 2546 ] 2271 2547 2272 2548 [[package]] 2549 + name = "wasite" 2550 + version = "0.1.0" 2551 + source = "registry+https://github.com/rust-lang/crates.io-index" 2552 + checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b" 2553 + 2554 + [[package]] 2273 2555 name = "wasm-bindgen" 2274 2556 version = "0.2.100" 2275 2557 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 2348 2630 dependencies = [ 2349 2631 "js-sys", 2350 2632 "wasm-bindgen", 2633 + ] 2634 + 2635 + [[package]] 2636 + name = "whoami" 2637 + version = "1.6.0" 2638 + source = "registry+https://github.com/rust-lang/crates.io-index" 2639 + checksum = "6994d13118ab492c3c80c1f81928718159254c53c472bf9ce36f8dae4add02a7" 2640 + dependencies = [ 2641 + "redox_syscall", 2642 + "wasite", 2643 + "web-sys", 2351 2644 ] 2352 2645 2353 2646 [[package]] ··· 2649 2942 "quote", 2650 2943 "syn", 2651 2944 "synstructure", 2945 + ] 2946 + 2947 + [[package]] 2948 + name = "zerocopy" 2949 + version = "0.8.25" 2950 + source = "registry+https://github.com/rust-lang/crates.io-index" 2951 + checksum = "a1702d9583232ddb9174e01bb7c15a2ab8fb1bc6f227aa1233858c351a3ba0cb" 2952 + dependencies = [ 2953 + "zerocopy-derive", 2954 + ] 2955 + 2956 + [[package]] 2957 + name = "zerocopy-derive" 2958 + version = "0.8.25" 2959 + source = "registry+https://github.com/rust-lang/crates.io-index" 2960 + checksum = "28a6e20d751156648aa063f3800b706ee209a32c0b4d9f24be3d980b01be55ef" 2961 + dependencies = [ 2962 + "proc-macro2", 2963 + "quote", 2964 + "syn", 2652 2965 ] 2653 2966 2654 2967 [[package]]
+2
Cargo.toml
··· 5 5 6 6 [dependencies] 7 7 chrono = { version = "0.4.39", features = ["serde"] } 8 + deadpool-postgres = "0.14.1" 8 9 dropshot = "0.16.0" 9 10 eyre = "0.6.12" 10 11 ipld-core = { version = "0.4.1", features = ["serde"] } ··· 15 16 slog-scope = { version = "4.4.0" } 16 17 slog-stdlog = { version = "4.1.1" } 17 18 tokio = { version = "1.0", features = [ "full" ] } 19 + tokio-postgres = { version = "0.7.13", features = ["with-chrono-0_4", "with-serde_json-1"] }
+3 -1
src/import.rs
··· 1 1 use crate::types::PlcOperationType; 2 2 use crate::utils::*; 3 3 use chrono::{DateTime, Utc}; 4 + use deadpool_postgres::Pool; 4 5 use ipld_core::cid::Cid; 5 6 use reqwest::Client; 6 7 use serde::{Deserialize, Serialize}; ··· 12 13 const SLEEP_TIME_F: f32 = (60. / 90.) * 1000.; 13 14 const SLEEP_TIME: Duration = Duration::from_millis(SLEEP_TIME_F as u64); 14 15 15 - pub async fn importer(log: Logger) { 16 + pub async fn importer(log: Logger, pool: Pool) { 16 17 let plc_main = var("PLC_UPSTREAM").unwrap_or("https://plc.directory".to_string()); 17 18 let user_agent = var("PLC_USER_AGENT").unwrap_or("parakeet-plc".to_string()); 18 19 19 20 let mut after = var("PLC_START_AFTER").ok(); 20 21 21 22 let client = Client::builder().user_agent(user_agent).build().unwrap(); 23 + let conn = pool.get().await.unwrap(); 22 24 23 25 loop { 24 26 let start = Instant::now();
+23 -2
src/lib.rs
··· 1 + use deadpool_postgres::{Manager, Pool}; 1 2 use dropshot::{ApiDescription, ConfigLogging, ConfigLoggingLevel}; 3 + use eyre::Context; 2 4 use slog::Logger; 5 + use std::env::var; 6 + use std::str::FromStr; 7 + use tokio_postgres::{Config, NoTls}; 3 8 4 9 pub mod import; 5 10 mod types; 6 11 mod utils; 7 12 8 13 #[derive(Clone)] 9 - pub struct ApiContext {} 14 + pub struct ApiContext { 15 + pub pool: Pool, 16 + } 10 17 11 18 pub fn create_logger() -> eyre::Result<Logger> { 12 19 let log = ConfigLogging::StderrTerminal { level: ConfigLoggingLevel::Info } ··· 21 28 // TODO: api endpoints 22 29 23 30 Ok(api) 24 - } 31 + } 32 + 33 + pub async fn connect_db() -> eyre::Result<Pool> { 34 + let db_uri = var("PLC_DB_URI").wrap_err("PLC_DB_URI missing")?; 35 + 36 + let cfg = Config::from_str(&db_uri)?; 37 + let mgr = Manager::from_config(cfg, NoTls, Default::default()); 38 + let pool = Pool::builder(mgr).build()?; 39 + 40 + // run the init script 41 + let init_conn = pool.get().await?; 42 + init_conn.simple_query(include_str!("sql/init.sql")).await?; 43 + 44 + Ok(pool) 45 + }
+4 -2
src/main.rs
··· 12 12 let _scope = slog_scope::set_global_logger(log.clone()); 13 13 slog_stdlog::init()?; 14 14 15 - tokio::spawn(plc_mirror::import::importer(log.clone())); 15 + let pool = plc_mirror::connect_db().await?; 16 + 17 + tokio::spawn(plc_mirror::import::importer(log.clone(), pool.clone())); 16 18 17 19 let bind_address = var("PLC_BIND_ADDR") 18 20 .unwrap_or(DEFAULT_BIND.to_string()) 19 21 .parse()?; 20 22 21 - let context = plc_mirror::ApiContext {}; 23 + let context = plc_mirror::ApiContext { pool }; 22 24 23 25 let server = ServerBuilder::new(api, context, log) 24 26 .config(ConfigDropshot {
+19
src/sql/init.sql
··· 1 + create table if not exists dids 2 + ( 3 + did text primary key 4 + ); 5 + 6 + create table if not exists operations 7 + ( 8 + did text not null references dids (did), 9 + hash text not null, 10 + prev text, 11 + sig text not null, 12 + nullified bool not null default false, 13 + 14 + operation jsonb not null, 15 + 16 + created_at timestamptz not null default now(), 17 + 18 + primary key (did, hash) 19 + );