ext/web W4: Postgres, Redis/memory cache, and S3/file storage drivers

URL-scheme backends keep the same # db CRUD author API; add # cache and

Co-authored-by: Cursor <cursoragent@cursor.com>
# storage with offline memory:/file: gold coverage.
This commit is contained in:
cflmy committed 2026-08-28 10:33:05 +08:00
1 parent e7c9c21a49
commit 034f52b7e7
14 files changed
+2504 -17

No files matched your search

Generated
+522 -11
View File
@@ -94,6 +94,15 @@ version = "1.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470"
[[package]]
name = "arc-swap"
version = "1.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c049c0be4daef0b145cb3555416b3b8ef5b7888a38aea1a3a155801fe7b0810b"
dependencies = [
"rustversion",
]
[[package]]
name = "argon2"
version = "0.5.3"
@@ -102,7 +111,7 @@ checksum = "3c3610892ee6e0cbce8ae2700349fcf8f98adb0dbfbee85aec3c9179d29cc072"
dependencies = [
"base64ct",
"blake2",
"cpufeatures",
"cpufeatures 0.2.17",
"password-hash",
]
@@ -139,6 +148,17 @@ dependencies = [
"tokio",
]
[[package]]
name = "async-trait"
version = "0.1.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82f6aeea286b8eb4dd3431a1be1b59d290ace00f5bfd8e2a159bc2a05e2c1667"
dependencies = [
"proc-macro2",
"quote",
"syn 3.0.3",
]
[[package]]
name = "atomic-waker"
version = "1.1.2"
@@ -230,7 +250,7 @@ version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe"
dependencies = [
"digest",
"digest 0.10.7",
]
[[package]]
@@ -242,6 +262,15 @@ dependencies = [
"generic-array",
]
[[package]]
name = "block-buffer"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa"
dependencies = [
"hybrid-array",
]
[[package]]
name = "bstr"
version = "1.13.0"
@@ -287,6 +316,17 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "chacha20"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06"
dependencies = [
"cfg-if",
"cpufeatures 0.3.1",
"rand_core 0.10.1",
]
[[package]]
name = "chrono"
version = "0.4.45"
@@ -344,12 +384,28 @@ version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1"
[[package]]
name = "cmov"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a"
[[package]]
name = "colorchoice"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
[[package]]
name = "combine"
version = "4.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfc320937d09e6de266b31b9afb480f197d7a861be86be7cb2ea7e5d1bfffc5e"
dependencies = [
"bytes",
"memchr",
]
[[package]]
name = "compression-codecs"
version = "0.4.38"
@@ -367,6 +423,12 @@ version = "0.4.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc14f565cf027a105f7a44ccf9e5b424348421a1d8952a8fc9d499d313107789"
[[package]]
name = "const-oid"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c"
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
@@ -382,6 +444,15 @@ dependencies = [
"libc",
]
[[package]]
name = "cpufeatures"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566"
dependencies = [
"libc",
]
[[package]]
name = "crc32fast"
version = "1.5.0"
@@ -401,6 +472,24 @@ dependencies = [
"typenum",
]
[[package]]
name = "crypto-common"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453"
dependencies = [
"hybrid-array",
]
[[package]]
name = "ctutils"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d5515a3834141de9eafb9717ad39eea8247b5674e6066c404e8c4b365d2a29e"
dependencies = [
"cmov",
]
[[package]]
name = "data-encoding"
version = "2.11.1"
@@ -419,17 +508,35 @@ version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
"block-buffer 0.10.4",
"crypto-common 0.1.7",
"subtle",
]
[[package]]
name = "digest"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2"
dependencies = [
"block-buffer 0.12.1",
"const-oid",
"crypto-common 0.2.2",
"ctutils",
]
[[package]]
name = "doc-comment"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "780955b8b195a21ab8e4ac6b60dd1dbdcec1dc6c51c0617964b08c81785e12c9"
[[package]]
name = "either"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "252afb9ae5eaa683babdc6a068b3f5726eb19e05070c731f9b2a23a7c3e8ed34"
[[package]]
name = "errno"
version = "0.3.14"
@@ -440,6 +547,12 @@ dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "fallible-iterator"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
[[package]]
name = "fallible-iterator"
version = "0.3.0"
@@ -493,6 +606,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "262590f4fe6afeb0bc83be1daa64e52657fe185690a958af7f3ad0e92085c5ae"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
@@ -565,7 +679,7 @@ checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0"
dependencies = [
"cfg-if",
"libc",
"wasi",
"wasi 0.11.1+wasi-snapshot-preview1",
]
[[package]]
@@ -576,10 +690,22 @@ checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd"
dependencies = [
"cfg-if",
"libc",
"r-efi",
"r-efi 5.3.0",
"wasip2",
]
[[package]]
name = "getrandom"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099"
dependencies = [
"cfg-if",
"libc",
"r-efi 6.0.0",
"rand_core 0.10.1",
]
[[package]]
name = "hashbrown"
version = "0.14.5"
@@ -604,6 +730,30 @@ version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "hmac"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e"
dependencies = [
"digest 0.10.7",
]
[[package]]
name = "hmac"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6303bc9732ae41b04cb554b844a762b4115a61bfaa81e3e83050991eeb56863f"
dependencies = [
"digest 0.11.3",
]
[[package]]
name = "http"
version = "1.5.0"
@@ -655,6 +805,15 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
[[package]]
name = "hybrid-array"
version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "707114b52a152fa7bdb290cd7cd5912d9467273b6d74e21b8d81aca1f8533f6b"
dependencies = [
"typenum",
]
[[package]]
name = "hyper"
version = "1.11.0"
@@ -770,6 +929,15 @@ version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
[[package]]
name = "itertools"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.18"
@@ -803,6 +971,15 @@ dependencies = [
"windows-targets",
]
[[package]]
name = "libredox"
version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7955dfc218a8afb29dfeffd540e3a6e96baeb94fe7138228dd7cc6937fbbf96"
dependencies = [
"libc",
]
[[package]]
name = "libsqlite3-sys"
version = "0.30.1"
@@ -814,6 +991,15 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "lock_api"
version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965"
dependencies = [
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.33"
@@ -872,13 +1058,19 @@ dependencies = [
"axum",
"futures-util",
"getrandom 0.2.17",
"hex",
"hmac 0.12.1",
"postgres",
"pulldown-cmark",
"redis",
"rusqlite",
"serde",
"serde_json",
"sha2 0.10.9",
"tokio",
"tokio-tungstenite 0.24.0",
"tower-http",
"ureq",
]
[[package]]
@@ -887,6 +1079,16 @@ version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3"
[[package]]
name = "md-5"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69b6441f590336821bb897fb28fc622898ccceb1d6cea3fde5ea86b090c4de98"
dependencies = [
"cfg-if",
"digest 0.11.3",
]
[[package]]
name = "memchr"
version = "2.8.3"
@@ -926,7 +1128,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30d65c71f1ce40ab09135ce117d742b9f8a19ff91a41a8b57ed50bc2de59c427"
dependencies = [
"libc",
"wasi",
"wasi 0.11.1+wasi-snapshot-preview1",
"windows-sys 0.61.2",
]
@@ -936,6 +1138,25 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be"
[[package]]
name = "num-bigint"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c89e69e7e0f03bea5ef08013795c25018e101932225a656383bd384495ecc367"
dependencies = [
"num-integer",
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ce2d95d4b3734dc35aa2f45e1aa22cd416814592a4f9d9205e11affd5b8e10b"
dependencies = [
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.19"
@@ -945,6 +1166,24 @@ dependencies = [
"autocfg",
]
[[package]]
name = "objc2-core-foundation"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a180dd8642fa45cdb7dd721cd4c11b1cadd4929ce112ebd8b9f5803cc79d536"
dependencies = [
"bitflags",
]
[[package]]
name = "objc2-system-configuration"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7216bd11cbda54ccabcab84d523dc93b858ec75ecfb3a7d89513fa22464da396"
dependencies = [
"objc2-core-foundation",
]
[[package]]
name = "once_cell"
version = "1.21.4"
@@ -957,6 +1196,29 @@ version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
[[package]]
name = "parking_lot"
version = "0.12.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-link",
]
[[package]]
name = "password-hash"
version = "0.5.0"
@@ -974,6 +1236,25 @@ version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
[[package]]
name = "phf"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1562dc717473dbaa4c1f85a36410e03c047b2e7df7f45ee938fbef64ae7fadf"
dependencies = [
"phf_shared",
"serde",
]
[[package]]
name = "phf_shared"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e57fef6bc5981e38c2ce2d63bfa546861309f875b8a75f092d1d54ae2d64f266"
dependencies = [
"siphasher",
]
[[package]]
name = "pin-project-lite"
version = "0.2.17"
@@ -986,6 +1267,51 @@ version = "0.3.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19f132c84eca552bf34cab8ec81f1c1dcc229b811638f9d283dceabe58c5569e"
[[package]]
name = "postgres"
version = "0.19.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33ad20e0aa0b24f5a394eab4f78c781d248982b22b25cecc7e3aa46a681605bd"
dependencies = [
"bytes",
"fallible-iterator 0.2.0",
"futures-util",
"log",
"tokio",
"tokio-postgres",
]
[[package]]
name = "postgres-protocol"
version = "0.6.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08808e3c483c46e999108051c78334f473d5adb59d78bb80a1268c7e6aa6c514"
dependencies = [
"base64",
"byteorder",
"bytes",
"fallible-iterator 0.2.0",
"hmac 0.13.0",
"md-5",
"memchr",
"rand 0.10.2",
"sha2 0.11.0",
"stringprep",
]
[[package]]
name = "postgres-types"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "851ca9db4932932d69f3ea811b1abe63087a0f740a47692619dd40d4899b68be"
dependencies = [
"bytes",
"fallible-iterator 0.2.0",
"postgres-protocol",
"serde_core",
"serde_json",
]
[[package]]
name = "ppv-lite86"
version = "0.2.21"
@@ -1068,6 +1394,12 @@ version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]]
name = "r-efi"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
[[package]]
name = "rand"
version = "0.8.7"
@@ -1089,6 +1421,17 @@ dependencies = [
"rand_core 0.9.5",
]
[[package]]
name = "rand"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7f5fa3a058cd35567ef9bfa5e75732bee0f9e4c55fa90477bef2dfcdbc4be80"
dependencies = [
"chacha20",
"getrandom 0.4.3",
"rand_core 0.10.1",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
@@ -1127,6 +1470,39 @@ dependencies = [
"getrandom 0.3.4",
]
[[package]]
name = "rand_core"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69"
[[package]]
name = "redis"
version = "0.27.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09d8f99a4090c89cc489a94833c901ead69bfbf3877b4867d5482e321ee875bc"
dependencies = [
"arc-swap",
"combine",
"itertools",
"itoa",
"num-bigint",
"percent-encoding",
"ryu",
"sha1_smol",
"socket2 0.5.10",
"url",
]
[[package]]
name = "redox_syscall"
version = "0.5.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d"
dependencies = [
"bitflags",
]
[[package]]
name = "regex"
version = "1.13.1"
@@ -1177,7 +1553,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7753b721174eb8ff87a9a0e799e2d7bc3749323e773db92e0984debb00019d6e"
dependencies = [
"bitflags",
"fallible-iterator",
"fallible-iterator 0.3.0",
"fallible-streaming-iterator",
"hashlink",
"libsqlite3-sys",
@@ -1230,6 +1606,12 @@ version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f"
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "serde"
version = "1.0.229"
@@ -1303,8 +1685,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a978451301f4db1d02937a4ab3ccce137717b81826e79b7d49ffe3244a13c3b8"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
"cpufeatures 0.2.17",
"digest 0.10.7",
]
[[package]]
name = "sha1_smol"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbfa15b3dddfee50a0fff136974b3e1bde555604ba463834a7eb7deb6417705d"
[[package]]
name = "sha2"
version = "0.10.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283"
dependencies = [
"cfg-if",
"cpufeatures 0.2.17",
"digest 0.10.7",
]
[[package]]
name = "sha2"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "446ba717509524cb3f22f17ecc096f10f4822d76ab5c0b9822c5f9c284e825f4"
dependencies = [
"cfg-if",
"cpufeatures 0.3.1",
"digest 0.11.3",
]
[[package]]
@@ -1329,6 +1739,12 @@ version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea"
[[package]]
name = "siphasher"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ee5873ec9cce0195efcb7a4e9507a04cd49aec9c83d0389df45b1ef7ba2e649"
[[package]]
name = "slab"
version = "0.4.12"
@@ -1341,6 +1757,16 @@ version = "1.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ed6a63f02c8539c91a8685a86f4099661ba3da017932f6ebbea6de3f0fa7c90"
[[package]]
name = "socket2"
version = "0.5.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e22376abed350d73dd1cd119b57ffccad95b4e585a7cda43e286245ce23c0678"
dependencies = [
"libc",
"windows-sys 0.52.0",
]
[[package]]
name = "socket2"
version = "0.6.5"
@@ -1351,6 +1777,17 @@ dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "stringprep"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b4df3d392d81bd458a8a621b8bffbd2302a12ffe288a9d931670948749463b1"
dependencies = [
"unicode-bidi",
"unicode-normalization",
"unicode-properties",
]
[[package]]
name = "strsim"
version = "0.10.0"
@@ -1475,7 +1912,7 @@ dependencies = [
"mio",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"socket2 0.6.5",
"tokio-macros",
"windows-sys 0.61.2",
]
@@ -1491,6 +1928,32 @@ dependencies = [
"syn 3.0.3",
]
[[package]]
name = "tokio-postgres"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a528f7d280f6d5b9cd149635c8705b0dd049754bc67d81d31fa25169a93809d3"
dependencies = [
"async-trait",
"byteorder",
"bytes",
"fallible-iterator 0.2.0",
"futures-channel",
"futures-util",
"log",
"parking_lot",
"percent-encoding",
"phf",
"pin-project-lite",
"postgres-protocol",
"postgres-types",
"rand 0.10.2",
"socket2 0.6.5",
"tokio",
"tokio-util",
"whoami",
]
[[package]]
name = "tokio-tungstenite"
version = "0.24.0"
@@ -1524,6 +1987,7 @@ dependencies = [
"bytes",
"futures-core",
"futures-sink",
"libc",
"pin-project-lite",
"tokio",
]
@@ -1676,6 +2140,12 @@ dependencies = [
"tinyvec",
]
[[package]]
name = "unicode-properties"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7df058c713841ad818f1dc5d3fd88063241cc61f49f5fbea4b951e8cf5a8d71d"
[[package]]
name = "unicode-width"
version = "0.2.2"
@@ -1762,6 +2232,15 @@ version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
[[package]]
name = "wasi"
version = "0.14.7+wasi-0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "883478de20367e224c0090af9cf5f9fa85bed63a95c1abf3afc5c083ebc06e8c"
dependencies = [
"wasip2",
]
[[package]]
name = "wasip2"
version = "1.0.4+wasi-0.2.12"
@@ -1771,6 +2250,15 @@ dependencies = [
"wit-bindgen",
]
[[package]]
name = "wasite"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "66fe902b4a6b8028a753d5424909b764ccf79b7a209eac9bf97e59cda9f71a42"
dependencies = [
"wasi 0.14.7+wasi-0.2.4",
]
[[package]]
name = "wasm-bindgen"
version = "0.2.127"
@@ -1816,6 +2304,16 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "web-sys"
version = "0.3.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c435338968042f4f59a557f690a253676d47ce13ceb55d70100e7facf6620a30"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "webpki-roots"
version = "0.26.11"
@@ -1834,6 +2332,19 @@ dependencies = [
"rustls-pki-types",
]
[[package]]
name = "whoami"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "626c4bac6755d76ffc12cb01b2eac751db1996b9e0041de9aa02c8c211ddc82c"
dependencies = [
"libc",
"libredox",
"objc2-system-configuration",
"wasite",
"web-sys",
]
[[package]]
name = "windows-core"
version = "0.62.2"
+124
View File
@@ -0,0 +1,124 @@
# `ext/web` 驱动:Postgres · Redis · S3
| | |
|---|---|
| 状态 | **Accepted · 已落地** |
| 日期 | 2026-08-28 |
| 相关 | [ext-web.md](ext-web.md) · [roadmap/ext-web.md](../roadmap/ext-web.md) · [ext-abi.md](ext-abi.md) |
| 目标 | 在**不改作者面 CRUD 心智**的前提下,用 URL 方案切换数据库;另增缓存与对象存储两类驱动。贯彻「表格 + 类方法」:配置即数据,装配即函数。 |
---
## 0. 一句话
**`# db` 的 `url=` 决定后端**:`sqlite:` 默认;`postgres://` / `postgresql://` 走 Postgres,作者面 API 不变。
**缓存**与**对象存储**是独立类(`# cache` / `# storage`),不塞进 `# db`。
---
## 1. 边界
| 能力 | 归属 | 理由 |
|------|------|------|
| SQLite / Postgres CRUD | `plugins/web` + `ext/web` `# db` | 同一套 `init/select/insert/…`;热路径 ABI |
| Redis 缓存 | `plugins/web` + `ext/web` `# cache` | 键值语义,不是表 CRUD |
| S3 / MinIO 对象 | `plugins/web` + `ext/web` `# storage` | 对象键 + 字节/路径,不是行 |
| 禁止 | 核心 `src/host/` | 驱动依赖重,只进 ABI 插件 |
---
## 2. URL 方案(锁定)
| 方案 | 含义 | 例 |
|------|------|-----|
| `sqlite:` / 裸路径 | SQLite 文件(既有) | `sqlite:data/site.db` |
| `postgres://` / `postgresql://` | Postgres | `postgres://user:pass@127.0.0.1:5432/app` |
| `redis://` / `rediss://` | Redis | `redis://127.0.0.1:6379/0` |
| `memory:` | **进程内** Redis 兼容后端(金样/离线) | `memory:` |
| `s3://bucket` + 查询参数 | S3 兼容(含 MinIO) | `s3://mybucket?endpoint=http://127.0.0.1:9000&region=us-east-1` |
| `file:` | **本地目录**对象存储(金样/离线) | `file:data/blobs` |
凭证:优先 URL 内嵌;S3 亦读环境变量 `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY`(或 `S3_ACCESS_KEY` / `S3_SECRET_KEY`)。
---
## 3. 作者面(代码即文档)
### 3.1 数据库 — 只换 URL
英文:
```markdown
import web:ext/web/web.mq.md
# main
*store = > web.db url="postgres://marqdo:marqdo@127.0.0.1:5432/site"*
*store = > store.init name="posts" fields=`schema`*
```
中文:`网页.数据库` + `url=` 同形。`init` / `select` / `insert` / `paginate` / `事务` **API 不变**。
Postgres 差异(实现层消化,作者尽量无感):
- `id INTEGER PRIMARY KEY` → `SERIAL PRIMARY KEY`(或 `GENERATED …`)
- 占位符 `?` → `$1,$2,…`
- 标识符双引号规则对齐
### 3.2 缓存 `# cache` / `# 缓存`
```markdown
*c = > web.cache url="memory:"*
*c = > c.set key="k" value="v" ttl=60*
*v = > c.get key="k"*
*c = > c.del key="k"*
```
方法:`get` · `set`(可选 `ttl` 秒)· `del` · `exists` · `ttl`。
配置表可选(装配时合并进 url / 默认 TTL):
| 键 | 值 |
|----|-----|
| url | redis://127.0.0.1:6379/0 |
| ttl | 3600 |
### 3.3 对象存储 `# storage` / `# 存储`
```markdown
*blob = > web.storage url="file:data/blobs"*
> `blob`.put key="avatar/1.png" path="local.png" content_type="image/png"
*got = > `blob`.get key="avatar/1.png"*
> `blob`.delete key="avatar/1.png"
*keys = > `blob`.list prefix="avatar/"*
```
方法:`put`(`path=` 本地文件或 `body=` 文本)· `get`(返回 `{ok, body|path, content_type}`)· `delete` · `list`。
---
## 4. ABI 名
| ABI | 说明 |
|-----|------|
| 既有 `web_db_*` | 内部按 `url` 方案分发 SQLite / Postgres |
| `web_cache_new/get/set/del/exists/ttl` | 缓存 |
| `web_storage_new/put/get/delete/list` | 对象存储 |
---
## 5. 测试
| 金样 | 内容 |
|------|------|
| `tests/ext/web-drivers-smoke.mq.md` | `memory:` 缓存往返;`file:` 存储 put/get/list/delete;`web_db_new` 接受 `postgres://` 句柄形状(不要求本机有库) |
| 可选 live | `MARQDO_TEST_POSTGRES` / `MARQDO_TEST_REDIS` / `MARQDO_TEST_S3` 非空时跑真实连通(CI 默认关) |
---
## 6. 非本波次
- 连接池大小 / 读写分离配置表
- Redis 集群 / Sentinel
- S3 预签名 URL 作者面糖(可后续加方法)
- 把 session 默认改存 Redis(仍默认 SQLite 表;可选后续)
+4 -4
View File
@@ -2,8 +2,8 @@
| | |
|---|---|
| 状态 | **W0–W3.5 + 安全硬化 + 内容标配 W4c 已落地;Postgres/Redis W4 待** |
| 日期 | 2026-08-11 |
| 状态 | **W0–W4 + 安全硬化 + 内容标配 W4c 已落地** |
| 日期 | 2026-08-28 |
| **锁定设计** | [design/ext-web.md](../design/ext-web.md) |
| 相关 | [ext-cli.md](../design/ext-cli.md) · [ext-abi.md](../design/ext-abi.md) · [ext-llm.md](../design/ext-llm.md) |
| 安装 | `marqdo ext add web`(`web` / `网页`) |
@@ -28,10 +28,10 @@
| **W3** | 三列绑定;变量名即表名;`/admin` CRUD+日志+布局 | **done** |
| **W3s** | 安全硬化:argon2 密码哈希、CSRF、SQLite 会话持久化、CSPRNG、登录限速 | **done** |
| **W3.5** | 人写面首版:`web.assemble` + `` `表`.`字段` `` + `examples/man-write-site` | **done**(编排袋仍可再瘦) |
| **W4** | Postgres;Redis / S3 驱动 | pending |
| **W4** | Postgres;Redis / S3 驱动(见 [ext-web-drivers.md](../design/ext-web-drivers.md)) | **done** |
| **W4c** | 内容标配:SEO / Markdown / RSS / 分页 UI(见 [web-net-capabilities.md](../design/web-net-capabilities.md)) | **done** |
验收金样:`tests/ext/web-smoke.mq.md`、`tests/ext/web-security-smoke.mq.md`、`ext_cli_add_web`。
验收金样:`tests/ext/web-smoke.mq.md`、`tests/ext/web-security-smoke.mq.md`、`tests/ext/web-drivers-smoke.mq.md`、`ext_cli_add_web`。
---
+81
View File
@@ -142,6 +142,8 @@ Style tables live as `##` exports in site style modules; compose resolves them b
# db
+ `url`="sqlite:site.db"
Open a database handle. URL schemes: `sqlite:path` (default), `postgres://…` / `postgresql://…` (same CRUD methods).
> ensure_plugin
**> web_db_new url=`url`**
@@ -488,6 +490,85 @@ Hash a plaintext password for storage in admin user tables (argon2id). Store the
**> web_password_hash password=`password`**
# cache
+ `url`="memory:"
Key–value cache. Use `memory:` for in-process (tests / single process) or `redis://host:6379/0` for Redis.
> ensure_plugin
**> web_cache_new url=`url`**
## get
+ `key`
*url = self[^url]*
**> web_cache_get url=`url` key=`key`**
## set
+ `key`
+ `value`
+ `ttl`=None
Optional `ttl` is seconds until expiry.
*url = self[^url]*
**> web_cache_set url=`url` key=`key` value=`value` ttl=`ttl`**
## del
+ `key`
*url = self[^url]*
**> web_cache_del url=`url` key=`key`**
## exists
+ `key`
*url = self[^url]*
**> web_cache_exists url=`url` key=`key`**
## ttl
+ `key`
*url = self[^url]*
**> web_cache_ttl url=`url` key=`key`**
# storage
+ `url`="file:data/blobs"
Object storage. `file:dir` stores blobs on disk (offline / gold). `s3://bucket?endpoint=http://127.0.0.1:9000&access_key=…&secret_key=…` talks to MinIO / S3.
> ensure_plugin
**> web_storage_new url=`url`**
## put
+ `key`
+ `body`=None
+ `path`=None
+ `content_type`="application/octet-stream"
Provide either `body` (text) or `path` (local file to upload).
*url = self[^url]*
**> web_storage_put url=`url` key=`key` body=`body` path=`path` content_type=`content_type`**
## get
+ `key`
*url = self[^url]*
**> web_storage_get url=`url` key=`key`**
## delete
+ `key`
*url = self[^url]*
**> web_storage_delete url=`url` key=`key`**
## list
+ `prefix`=""
*url = self[^url]*
**> web_storage_list url=`url` prefix=`prefix`**
# ws
+ `timeout_sec`=30
+81
View File
@@ -129,6 +129,8 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c
# 数据库
+ `地址`="sqlite:site.db"
打开数据库句柄。URL 方案:`sqlite:路径`(默认)、`postgres://…` / `postgresql://…`(CRUD 方法相同)。
> 确保插件
**> web_db_new url=`地址`**
@@ -470,6 +472,85 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c
**> web_password_hash password=`密码`**
# 缓存
+ `地址`="memory:"
键值缓存。`memory:` 为进程内(测试 / 单进程);`redis://主机:6379/0` 连 Redis。
> 确保插件
**> web_cache_new url=`地址`**
## 获取
+ `键`
*url = self[^url]*
**> web_cache_get url=`url` key=`键`**
## 设置
+ `键`
+ `值`
+ `过期`=None
`过期` 为秒;省略则不过期。
*url = self[^url]*
**> web_cache_set url=`url` key=`键` value=`值` ttl=`过期`**
## 删除
+ `键`
*url = self[^url]*
**> web_cache_del url=`url` key=`键`**
## 存在
+ `键`
*url = self[^url]*
**> web_cache_exists url=`url` key=`键`**
## 剩余时间
+ `键`
*url = self[^url]*
**> web_cache_ttl url=`url` key=`键`**
# 存储
+ `地址`="file:data/blobs"
对象存储。`file:目录` 落本地盘(离线 / 金样);`s3://桶?endpoint=http://127.0.0.1:9000&access_key=…&secret_key=…` 连 MinIO / S3。
> 确保插件
**> web_storage_new url=`地址`**
## 上传
+ `键`
+ `正文`=None
+ `路径`=None
+ `类型`="application/octet-stream"
`正文`(文本)与 `路径`(本地文件)二选一。
*url = self[^url]*
**> web_storage_put url=`url` key=`键` body=`正文` path=`路径` content_type=`类型`**
## 下载
+ `键`
*url = self[^url]*
**> web_storage_get url=`url` key=`键`**
## 删除对象
+ `键`
*url = self[^url]*
**> web_storage_delete url=`url` key=`键`**
## 列出
+ `前缀`=""
*url = self[^url]*
**> web_storage_list url=`url` prefix=`前缀`**
# 实时
+ `超时秒`=30
+6
View File
@@ -22,3 +22,9 @@ futures-util = "0.3"
argon2 = "0.5"
getrandom = "0.2"
pulldown-cmark = "0.12"
postgres = { version = "0.19", features = ["with-serde_json-1"] }
redis = { version = "0.27", default-features = true }
ureq = { version = "=2.9.7", default-features = false, features = ["tls"] }
hmac = "0.12"
sha2 = "0.10"
hex = "0.4"
+242
View File
@@ -0,0 +1,242 @@
//! `# cache` — Redis or in-process memory (design ext-web-drivers).
use serde_json::{json, Value};
use std::collections::HashMap;
use std::sync::Mutex;
use std::time::{SystemTime, UNIX_EPOCH};
struct Entry {
value: String,
/// Absolute expiry unix secs; `None` = no TTL.
exp: Option<u64>,
}
struct MemStore {
data: HashMap<String, Entry>,
}
enum Backend {
Memory(MemStore),
/// Redis URL — connections opened per call (simple; enough for W4).
RedisUrl(String),
}
static HANDLES: Mutex<Option<HashMap<String, Backend>>> = Mutex::new(None);
fn now() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0)
}
fn with_map<T>(f: impl FnOnce(&mut HashMap<String, Backend>) -> T) -> T {
let mut g = HANDLES.lock().unwrap_or_else(|e| e.into_inner());
let m = g.get_or_insert_with(HashMap::new);
f(m)
}
fn is_memory(url: &str) -> bool {
let u = url.trim().to_ascii_lowercase();
u.is_empty() || u == "memory:" || u == "memory://" || u.starts_with("memory:")
}
fn is_redis(url: &str) -> bool {
let u = url.trim().to_ascii_lowercase();
u.starts_with("redis://") || u.starts_with("rediss://")
}
/// Open (or reuse) a cache handle. Returns `{ _type, url }`.
pub fn open(url: &str) -> Result<Value, String> {
let url = if url.trim().is_empty() {
"memory:"
} else {
url.trim()
};
if !is_memory(url) && !is_redis(url) {
return Err(format!(
"cache url must be `memory:` or `redis://…`, got `{url}`"
));
}
with_map(|m| {
if !m.contains_key(url) {
let backend = if is_memory(url) {
Backend::Memory(MemStore {
data: HashMap::new(),
})
} else {
Backend::RedisUrl(url.to_string())
};
m.insert(url.to_string(), backend);
}
});
Ok(json!({ "_type": "cache", "url": url }))
}
fn prune_mem(store: &mut MemStore) {
let t = now();
store.data.retain(|_, e| e.exp.map(|x| x > t).unwrap_or(true));
}
fn with_backend<T>(url: &str, f: impl FnOnce(&mut Backend) -> Result<T, String>) -> Result<T, String> {
with_map(|m| {
let b = m
.get_mut(url)
.ok_or_else(|| format!("cache `{url}` not opened — call cache.new first"))?;
f(b)
})
}
pub fn get(url: &str, key: &str) -> Result<Value, String> {
with_backend(url, |b| match b {
Backend::Memory(store) => {
prune_mem(store);
match store.data.get(key) {
Some(e) => Ok(json!({ "ok": true, "value": e.value })),
None => Ok(json!({ "ok": false })),
}
}
Backend::RedisUrl(u) => redis_get(u, key),
})
}
pub fn set(url: &str, key: &str, value: &str, ttl_sec: Option<u64>) -> Result<Value, String> {
with_backend(url, |b| match b {
Backend::Memory(store) => {
prune_mem(store);
let exp = ttl_sec.filter(|t| *t > 0).map(|t| now() + t);
store.data.insert(
key.to_string(),
Entry {
value: value.to_string(),
exp,
},
);
Ok(json!({ "ok": true }))
}
Backend::RedisUrl(u) => redis_set(u, key, value, ttl_sec),
})
}
pub fn del(url: &str, key: &str) -> Result<Value, String> {
with_backend(url, |b| match b {
Backend::Memory(store) => {
let ok = store.data.remove(key).is_some();
Ok(json!({ "ok": ok }))
}
Backend::RedisUrl(u) => redis_del(u, key),
})
}
pub fn exists(url: &str, key: &str) -> Result<Value, String> {
with_backend(url, |b| match b {
Backend::Memory(store) => {
prune_mem(store);
Ok(json!({ "ok": store.data.contains_key(key) }))
}
Backend::RedisUrl(u) => redis_exists(u, key),
})
}
pub fn ttl(url: &str, key: &str) -> Result<Value, String> {
with_backend(url, |b| match b {
Backend::Memory(store) => {
prune_mem(store);
match store.data.get(key) {
Some(e) => {
let left = match e.exp {
Some(x) => x.saturating_sub(now()) as i64,
None => -1,
};
Ok(json!({ "ok": true, "ttl": left }))
}
None => Ok(json!({ "ok": false, "ttl": -2 })),
}
}
Backend::RedisUrl(u) => redis_ttl(u, key),
})
}
fn redis_get(url: &str, key: &str) -> Result<Value, String> {
let client = redis::Client::open(url).map_err(|e| e.to_string())?;
let mut con = client.get_connection().map_err(|e| e.to_string())?;
let v: Option<String> = redis::cmd("GET")
.arg(key)
.query(&mut con)
.map_err(|e| e.to_string())?;
match v {
Some(s) => Ok(json!({ "ok": true, "value": s })),
None => Ok(json!({ "ok": false })),
}
}
fn redis_set(url: &str, key: &str, value: &str, ttl_sec: Option<u64>) -> Result<Value, String> {
let client = redis::Client::open(url).map_err(|e| e.to_string())?;
let mut con = client.get_connection().map_err(|e| e.to_string())?;
match ttl_sec.filter(|t| *t > 0) {
Some(t) => {
redis::cmd("SETEX")
.arg(key)
.arg(t)
.arg(value)
.query::<()>(&mut con)
.map_err(|e| e.to_string())?;
}
None => {
redis::cmd("SET")
.arg(key)
.arg(value)
.query::<()>(&mut con)
.map_err(|e| e.to_string())?;
}
}
Ok(json!({ "ok": true }))
}
fn redis_del(url: &str, key: &str) -> Result<Value, String> {
let client = redis::Client::open(url).map_err(|e| e.to_string())?;
let mut con = client.get_connection().map_err(|e| e.to_string())?;
let n: i64 = redis::cmd("DEL")
.arg(key)
.query(&mut con)
.map_err(|e| e.to_string())?;
Ok(json!({ "ok": n > 0 }))
}
fn redis_exists(url: &str, key: &str) -> Result<Value, String> {
let client = redis::Client::open(url).map_err(|e| e.to_string())?;
let mut con = client.get_connection().map_err(|e| e.to_string())?;
let n: i64 = redis::cmd("EXISTS")
.arg(key)
.query(&mut con)
.map_err(|e| e.to_string())?;
Ok(json!({ "ok": n > 0 }))
}
fn redis_ttl(url: &str, key: &str) -> Result<Value, String> {
let client = redis::Client::open(url).map_err(|e| e.to_string())?;
let mut con = client.get_connection().map_err(|e| e.to_string())?;
let t: i64 = redis::cmd("TTL")
.arg(key)
.query(&mut con)
.map_err(|e| e.to_string())?;
Ok(json!({ "ok": t >= -1, "ttl": t }))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn memory_roundtrip() {
let url = "memory:test-cache";
open(url).unwrap();
set(url, "a", "1", Some(60)).unwrap();
let g = get(url, "a").unwrap();
assert_eq!(g["ok"], true);
assert_eq!(g["value"], "1");
assert_eq!(exists(url, "a").unwrap()["ok"], true);
del(url, "a").unwrap();
assert_eq!(get(url, "a").unwrap()["ok"], false);
}
}
+53
View File
@@ -172,6 +172,9 @@ fn from_sql(v: ValueRef<'_>) -> Value {
/// `fields` = list of {name,type,nullable?} or columnar schema table already normalized.
pub fn init(url: &str, table: &str, fields: &Value) -> Result<Value, String> {
if crate::db_pg::is_postgres(url) {
return crate::db_pg::init(url, table, fields);
}
let table = ident(table)?;
let cols = crate::table::as_fields(fields);
let arr = cols.as_array().ok_or("fields must be a list")?;
@@ -224,6 +227,9 @@ pub fn init(url: &str, table: &str, fields: &Value) -> Result<Value, String> {
}
pub fn insert(url: &str, table: &str, rows: &Value, txn_id: Option<&str>) -> Result<Value, String> {
if crate::db_pg::is_postgres(url) {
return crate::db_pg::insert(url, table, rows, txn_id);
}
let table = ident(table)?;
let rows = crate::table::as_rows(rows);
let arr = rows.as_array().ok_or("rows must be a list")?;
@@ -489,6 +495,9 @@ pub fn select_order(
offset: Option<i64>,
txn_id: Option<&str>,
) -> Result<Value, String> {
if crate::db_pg::is_postgres(url) {
return crate::db_pg::select_order(url, table, limit, where_v, order, offset, txn_id);
}
let table = ident(table)?;
let (exprs, mut vals) = parse_where(where_v)?;
let conn = conn_for(url, txn_id)?;
@@ -584,6 +593,10 @@ fn count_rows(
}
pub fn get(url: &str, table: &str, id: &str, txn_id: Option<&str>) -> Result<Value, String> {
if crate::db_pg::is_postgres(url) {
return crate::db_pg::get(url, table, id, txn_id);
}
let table = ident(table)?;
let conn = conn_for(url, txn_id)?;
let c = conn.lock().unwrap_or_else(|e| e.into_inner());
@@ -612,6 +625,10 @@ pub fn update(
row: &Value,
txn_id: Option<&str>,
) -> Result<Value, String> {
if crate::db_pg::is_postgres(url) {
return crate::db_pg::update(url, table, id, row, txn_id);
}
let table = ident(table)?;
let obj = row.as_object().ok_or("row must be a map")?;
let mut sets = Vec::new();
@@ -641,6 +658,10 @@ pub fn update(
}
pub fn delete(url: &str, table: &str, id: &str, txn_id: Option<&str>) -> Result<Value, String> {
if crate::db_pg::is_postgres(url) {
return crate::db_pg::delete(url, table, id, txn_id);
}
let table = ident(table)?;
let conn = conn_for(url, txn_id)?;
let c = conn.lock().unwrap_or_else(|e| e.into_inner());
@@ -652,6 +673,10 @@ pub fn delete(url: &str, table: &str, id: &str, txn_id: Option<&str>) -> Result<
}
pub fn exec(url: &str, sql: &str, args: Option<&Value>, txn_id: Option<&str>) -> Result<Value, String> {
if crate::db_pg::is_postgres(url) {
return crate::db_pg::exec(url, sql, args, txn_id);
}
let conn = conn_for(url, txn_id)?;
let c = conn.lock().unwrap_or_else(|e| e.into_inner());
let vals: Vec<rusqlite::types::Value> = match args {
@@ -667,6 +692,10 @@ pub fn exec(url: &str, sql: &str, args: Option<&Value>, txn_id: Option<&str>) ->
/// Run a `SELECT …` and return the result set (count / join / group / subquery).
/// Bare SQL — the result carries `{ rows, count }`.
pub fn query(url: &str, sql: &str, args: Option<&Value>, txn_id: Option<&str>) -> Result<Value, String> {
if crate::db_pg::is_postgres(url) {
return crate::db_pg::query(url, sql, args, txn_id);
}
let conn = conn_for(url, txn_id)?;
let c = conn.lock().unwrap_or_else(|e| e.into_inner());
let vals: Vec<rusqlite::types::Value> = match args {
@@ -679,6 +708,10 @@ pub fn query(url: &str, sql: &str, args: Option<&Value>, txn_id: Option<&str>) -
/// Count rows matching a `where` filter (aggregation helper).
pub fn count(url: &str, table: &str, where_v: Option<&Value>, txn_id: Option<&str>) -> Result<Value, String> {
if crate::db_pg::is_postgres(url) {
return crate::db_pg::count(url, table, where_v, txn_id);
}
let table = ident(table)?;
let (exprs, vals) = parse_where(where_v)?;
let conn = conn_for(url, txn_id)?;
@@ -694,6 +727,10 @@ pub fn count(url: &str, table: &str, where_v: Option<&Value>, txn_id: Option<&st
/// Begin a transaction. Borrows the pooled connection exclusively and records
/// it under a fresh txn id; returns `{ txn, url }` for `commit` / `rollback`.
pub fn begin(url: &str) -> Result<Value, String> {
if crate::db_pg::is_postgres(url) {
return crate::db_pg::begin(url);
}
let conn = pooled(url)?;
{
let c = conn.lock().unwrap_or_else(|e| e.into_inner());
@@ -711,6 +748,10 @@ pub fn begin(url: &str) -> Result<Value, String> {
/// Commit a transaction and return its connection to the pool.
pub fn commit(txn_id: &str) -> Result<Value, String> {
if txn_id.starts_with("pg-txn-") {
return crate::db_pg::commit(txn_id);
}
let (_, conn) = take_txn(txn_id)?;
let c = conn.lock().unwrap_or_else(|e| e.into_inner());
c.execute_batch("COMMIT").map_err(|e| e.to_string())?;
@@ -719,6 +760,10 @@ pub fn commit(txn_id: &str) -> Result<Value, String> {
/// Roll back a transaction and return its connection to the pool.
pub fn rollback(txn_id: &str) -> Result<Value, String> {
if txn_id.starts_with("pg-txn-") {
return crate::db_pg::rollback(txn_id);
}
let (_, conn) = take_txn(txn_id)?;
let c = conn.lock().unwrap_or_else(|e| e.into_inner());
c.execute_batch("ROLLBACK").map_err(|e| e.to_string())?;
@@ -735,6 +780,10 @@ fn take_txn(txn_id: &str) -> Result<(String, Arc<Mutex<Connection>>), String> {
}
pub fn list_tables(url: &str) -> Result<Vec<String>, String> {
if crate::db_pg::is_postgres(url) {
return crate::db_pg::list_tables(url);
}
let conn = pooled(url)?;
let c = conn.lock().unwrap_or_else(|e| e.into_inner());
let mut stmt = c
@@ -760,6 +809,10 @@ pub struct ColumnInfo {
/// `PRAGMA table_info` → column metadata for admin forms.
pub fn table_info(url: &str, table: &str) -> Result<Vec<ColumnInfo>, String> {
if crate::db_pg::is_postgres(url) {
return crate::db_pg::table_info(url, table);
}
let table = ident(table)?;
let conn = pooled(url)?;
let c = conn.lock().unwrap_or_else(|e| e.into_inner());
+534
View File
@@ -0,0 +1,534 @@
//! Postgres backend for `# db` (same author-facing API as SQLite).
use crate::driver::{db_kind, rewrite_placeholders_pg, DbKind};
use postgres::types::ToSql;
use postgres::{Client, NoTls, Row};
use serde_json::{json, Map, Value};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
static POOL: Mutex<Option<HashMap<String, Arc<Mutex<Client>>>>> = Mutex::new(None);
static TXNS: Mutex<Option<HashMap<String, (String, Arc<Mutex<Client>>)>>> = Mutex::new(None);
pub fn is_postgres(url: &str) -> bool {
matches!(db_kind(url), DbKind::Postgres)
}
fn pooled(url: &str) -> Result<Arc<Mutex<Client>>, String> {
let mut guard = POOL.lock().unwrap_or_else(|e| e.into_inner());
let pool = guard.get_or_insert_with(HashMap::new);
if let Some(c) = pool.get(url) {
return Ok(c.clone());
}
let client = Client::connect(url, NoTls).map_err(|e| format!("postgres connect: {e}"))?;
let arc = Arc::new(Mutex::new(client));
pool.insert(url.to_string(), arc.clone());
Ok(arc)
}
fn conn_for(url: &str, txn_id: Option<&str>) -> Result<Arc<Mutex<Client>>, String> {
if let Some(tid) = txn_id {
let guard = TXNS.lock().unwrap_or_else(|e| e.into_inner());
match guard.as_ref().and_then(|m| m.get(tid)) {
Some((_, c)) => Ok(c.clone()),
None => Err(format!("unknown transaction `{tid}`")),
}
} else {
pooled(url)
}
}
fn next_txn_id() -> String {
use std::time::{SystemTime, UNIX_EPOCH};
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.subsec_nanos())
.unwrap_or(0);
format!("pg-txn-{}-{nanos}", std::process::id())
}
fn ident(s: &str) -> Result<&str, String> {
let s = s.trim();
if s.is_empty()
|| !s
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '_')
{
return Err(format!("bad identifier `{s}`"));
}
Ok(s)
}
fn cell_str(v: &Value) -> String {
match v {
Value::Null => String::new(),
Value::String(s) => s.clone(),
Value::Bool(b) => b.to_string(),
Value::Number(n) => n.to_string(),
other => other.to_string(),
}
}
fn pg_type(t: &str) -> &'static str {
match t.trim().to_ascii_lowercase().as_str() {
"int" | "integer" => "INTEGER",
"real" | "float" | "double" => "DOUBLE PRECISION",
"blob" | "bytea" => "BYTEA",
"bool" | "boolean" => "BOOLEAN",
_ => "TEXT",
}
}
fn to_pg_box(v: &Value) -> Box<dyn ToSql + Sync + Send> {
match v {
Value::Null => Box::new(None::<String>),
Value::Bool(b) => Box::new(*b),
Value::Number(n) => {
if let Some(i) = n.as_i64() {
Box::new(i)
} else if let Some(f) = n.as_f64() {
Box::new(f)
} else {
Box::new(n.to_string())
}
}
Value::String(s) => Box::new(s.clone()),
other => Box::new(other.to_string()),
}
}
fn row_to_json(row: &Row) -> Value {
let mut m = Map::new();
for (i, col) in row.columns().iter().enumerate() {
let name = col.name().to_string();
let v: Value = if let Ok(x) = row.try_get::<_, Option<i64>>(i) {
x.map(|n| json!(n)).unwrap_or(Value::Null)
} else if let Ok(x) = row.try_get::<_, Option<f64>>(i) {
x.map(|n| json!(n)).unwrap_or(Value::Null)
} else if let Ok(x) = row.try_get::<_, Option<bool>>(i) {
x.map(|b| json!(b)).unwrap_or(Value::Null)
} else if let Ok(x) = row.try_get::<_, Option<String>>(i) {
x.map(Value::String).unwrap_or(Value::Null)
} else {
Value::Null
};
m.insert(name, v);
}
Value::Object(m)
}
fn exec_sql(client: &mut Client, sql: &str, params: &[Value]) -> Result<u64, String> {
let sql = rewrite_placeholders_pg(sql);
let boxes: Vec<Box<dyn ToSql + Sync + Send>> = params.iter().map(to_pg_box).collect();
let refs: Vec<&(dyn ToSql + Sync)> = boxes
.iter()
.map(|b| b.as_ref() as &(dyn ToSql + Sync))
.collect();
client
.execute(sql.as_str(), refs.as_slice())
.map_err(|e| e.to_string())
}
fn query_sql(client: &mut Client, sql: &str, params: &[Value]) -> Result<Vec<Value>, String> {
let sql = rewrite_placeholders_pg(sql);
let boxes: Vec<Box<dyn ToSql + Sync + Send>> = params.iter().map(to_pg_box).collect();
let refs: Vec<&(dyn ToSql + Sync)> = boxes
.iter()
.map(|b| b.as_ref() as &(dyn ToSql + Sync))
.collect();
let rows = client
.query(sql.as_str(), refs.as_slice())
.map_err(|e| e.to_string())?;
Ok(rows.iter().map(row_to_json).collect())
}
/// Equality-map / filter-table → `(AND exprs with ?, vals)`.
fn parse_where_simple(where_v: Option<&Value>) -> Result<(Vec<String>, Vec<Value>), String> {
let Some(w) = where_v else {
return Ok((vec![], vec![]));
};
let w = crate::table::as_rows(w);
let mut exprs = Vec::new();
let mut vals = Vec::new();
match w {
Value::Array(arr) => {
for item in arr {
let Value::Object(m) = item else {
continue;
};
let field = m
.get("field")
.or_else(|| m.get("字段"))
.or_else(|| m.get("列"))
.map(cell_str)
.unwrap_or_default();
if !field.is_empty() {
let col = ident(&field)?;
let op = m
.get("op")
.or_else(|| m.get("操作"))
.map(cell_str)
.unwrap_or_else(|| "=".into());
let op = match op.trim().to_ascii_lowercase().as_str() {
"" | "=" | "eq" | "等于" => "=",
"!=" | "<>" | "ne" | "不等于" => "!=",
">" | "gt" | "大于" => ">",
">=" | "gte" => ">=",
"<" | "lt" | "小于" => "<",
"<=" | "lte" => "<=",
"like" | "包含" => "LIKE",
other => return Err(format!("unsupported where op `{other}`")),
};
let val = m
.get("value")
.or_else(|| m.get("值"))
.cloned()
.unwrap_or(Value::Null);
exprs.push(format!("\"{col}\" {op} ?"));
vals.push(val);
} else {
for (k, v) in &m {
if k == "@" || k == "行" || k == "row" || k.starts_with('_') {
continue;
}
let col = ident(k)?;
exprs.push(format!("\"{col}\" = ?"));
vals.push(v.clone());
}
}
}
}
_ => {}
}
Ok((exprs, vals))
}
pub fn init(url: &str, table: &str, fields: &Value) -> Result<Value, String> {
let table = ident(table)?;
let cols = crate::table::as_fields(fields);
let arr = cols.as_array().ok_or("fields must be a list")?;
if arr.is_empty() {
return Err("empty fields".into());
}
let mut parts = Vec::new();
let mut has_pk = false;
for c in arr {
let name = c
.get("name")
.or_else(|| c.get("字段"))
.map(cell_str)
.unwrap_or_default();
let name = ident(&name)?;
let ty = c
.get("type")
.or_else(|| c.get("类型"))
.map(cell_str)
.unwrap_or_else(|| "text".into());
let nullable = c
.get("nullable")
.or_else(|| c.get("可空"))
.and_then(|v| match v {
Value::Bool(b) => Some(*b),
Value::String(s) => {
Some(matches!(s.as_str(), "true" | "True" | "1" | "是" | "可"))
}
_ => None,
})
.unwrap_or(true);
let part = if name == "id" && !has_pk && pg_type(&ty) == "INTEGER" {
has_pk = true;
format!("\"{name}\" SERIAL PRIMARY KEY")
} else if name == "id" && !has_pk {
has_pk = true;
format!("\"{name}\" {} PRIMARY KEY", pg_type(&ty))
} else {
let mut col = format!("\"{name}\" {}", pg_type(&ty));
if !nullable {
col.push_str(" NOT NULL");
}
col
};
parts.push(part);
}
let sql = format!(
"CREATE TABLE IF NOT EXISTS \"{table}\" ({})",
parts.join(", ")
);
let conn = pooled(url)?;
let mut c = conn.lock().unwrap_or_else(|e| e.into_inner());
c.batch_execute(&sql).map_err(|e| e.to_string())?;
Ok(json!({ "_type": "db_table", "name": table, "url": url }))
}
pub fn insert(url: &str, table: &str, rows: &Value, txn_id: Option<&str>) -> Result<Value, String> {
let table = ident(table)?;
let rows = crate::table::as_rows(rows);
let arr = rows.as_array().ok_or("rows must be a list")?;
let conn = conn_for(url, txn_id)?;
let mut c = conn.lock().unwrap_or_else(|e| e.into_inner());
let mut n = 0i64;
for row in arr {
let obj = row.as_object().ok_or("row must be a map")?;
let mut cols = Vec::new();
let mut ph = Vec::new();
let mut vals = Vec::new();
for (k, v) in obj {
if k == "@" || k == "行" || k == "row" {
continue;
}
let _ = ident(k)?;
cols.push(format!("\"{k}\""));
ph.push("?");
vals.push(v.clone());
}
if cols.is_empty() {
continue;
}
let sql = format!(
"INSERT INTO \"{table}\" ({}) VALUES ({})",
cols.join(", "),
ph.join(", ")
);
exec_sql(&mut c, &sql, &vals)?;
n += 1;
}
Ok(json!({ "ok": true, "inserted": n }))
}
pub fn select_order(
url: &str,
table: &str,
limit: i64,
where_v: Option<&Value>,
order: Option<&str>,
offset: Option<i64>,
txn_id: Option<&str>,
) -> Result<Value, String> {
let table = ident(table)?;
let (exprs, mut vals) = parse_where_simple(where_v)?;
let mut sql = format!("SELECT * FROM \"{table}\"");
let where_sql = if exprs.is_empty() {
String::new()
} else {
format!(" WHERE {}", exprs.join(" AND "))
};
sql.push_str(&where_sql);
if let Some(order) = order.map(str::trim).filter(|s| !s.is_empty()) {
let mut order_exprs = Vec::new();
for part in order.split(',').map(str::trim).filter(|p| !p.is_empty()) {
let (col, dir) = match part.strip_prefix('-') {
Some(c) => (c, " DESC"),
None => (part, ""),
};
order_exprs.push(format!("\"{}\"{}", ident(col)?, dir));
}
if !order_exprs.is_empty() {
sql.push_str(" ORDER BY ");
sql.push_str(&order_exprs.join(", "));
}
}
sql.push_str(" LIMIT ?");
vals.push(json!(limit));
if let Some(off) = offset {
if off > 0 {
sql.push_str(" OFFSET ?");
vals.push(json!(off));
}
}
let conn = conn_for(url, txn_id)?;
let mut c = conn.lock().unwrap_or_else(|e| e.into_inner());
let rows = query_sql(&mut c, &sql, &vals)?;
if offset.is_some() {
let mut count_sql = format!("SELECT COUNT(*) AS count FROM \"{table}\"");
count_sql.push_str(&where_sql);
let count_vals: Vec<Value> = vals
.iter()
.take(exprs.len())
.cloned()
.collect();
let total = query_sql(&mut c, &count_sql, &count_vals)?
.first()
.and_then(|r| r.get("count"))
.and_then(|v| v.as_i64())
.unwrap_or(rows.len() as i64);
Ok(json!({ "rows": rows, "total": total }))
} else {
Ok(json!({ "rows": rows }))
}
}
pub fn get(url: &str, table: &str, id: &str, txn_id: Option<&str>) -> Result<Value, String> {
let table = ident(table)?;
let sql = format!("SELECT * FROM \"{table}\" WHERE \"id\" = ? LIMIT 1");
let conn = conn_for(url, txn_id)?;
let mut c = conn.lock().unwrap_or_else(|e| e.into_inner());
let rows = query_sql(&mut c, &sql, &[json!(id)])?;
Ok(rows.into_iter().next().unwrap_or(Value::Null))
}
pub fn update(
url: &str,
table: &str,
id: &str,
row: &Value,
txn_id: Option<&str>,
) -> Result<Value, String> {
let table = ident(table)?;
let obj = row.as_object().ok_or("row must be a map")?;
let mut sets = Vec::new();
let mut vals = Vec::new();
for (k, v) in obj {
if k == "id" || k == "@" || k == "行" || k == "row" {
continue;
}
let _ = ident(k)?;
sets.push(format!("\"{k}\" = ?"));
vals.push(v.clone());
}
if sets.is_empty() {
return Ok(json!({ "ok": true, "updated": 0 }));
}
vals.push(json!(id));
let sql = format!(
"UPDATE \"{table}\" SET {} WHERE \"id\" = ?",
sets.join(", ")
);
let conn = conn_for(url, txn_id)?;
let mut c = conn.lock().unwrap_or_else(|e| e.into_inner());
let n = exec_sql(&mut c, &sql, &vals)?;
Ok(json!({ "ok": true, "updated": n }))
}
pub fn delete(url: &str, table: &str, id: &str, txn_id: Option<&str>) -> Result<Value, String> {
let table = ident(table)?;
let sql = format!("DELETE FROM \"{table}\" WHERE \"id\" = ?");
let conn = conn_for(url, txn_id)?;
let mut c = conn.lock().unwrap_or_else(|e| e.into_inner());
let n = exec_sql(&mut c, &sql, &[json!(id)])?;
Ok(json!({ "ok": true, "deleted": n }))
}
pub fn exec(url: &str, sql: &str, args: Option<&Value>, txn_id: Option<&str>) -> Result<Value, String> {
let vals: Vec<Value> = match args {
Some(Value::Array(a)) => a.clone(),
_ => Vec::new(),
};
let conn = conn_for(url, txn_id)?;
let mut c = conn.lock().unwrap_or_else(|e| e.into_inner());
let n = exec_sql(&mut c, sql, &vals)?;
Ok(json!({ "ok": true, "changes": n }))
}
pub fn query(url: &str, sql: &str, args: Option<&Value>, txn_id: Option<&str>) -> Result<Value, String> {
let vals: Vec<Value> = match args {
Some(Value::Array(a)) => a.clone(),
_ => Vec::new(),
};
let conn = conn_for(url, txn_id)?;
let mut c = conn.lock().unwrap_or_else(|e| e.into_inner());
let rows = query_sql(&mut c, sql, &vals)?;
Ok(json!({ "rows": rows, "count": rows.len() }))
}
pub fn count(
url: &str,
table: &str,
where_v: Option<&Value>,
txn_id: Option<&str>,
) -> Result<Value, String> {
let table = ident(table)?;
let (exprs, vals) = parse_where_simple(where_v)?;
let mut sql = format!("SELECT COUNT(*) AS count FROM \"{table}\"");
if !exprs.is_empty() {
sql.push_str(&format!(" WHERE {}", exprs.join(" AND ")));
}
let conn = conn_for(url, txn_id)?;
let mut c = conn.lock().unwrap_or_else(|e| e.into_inner());
let rows = query_sql(&mut c, &sql, &vals)?;
let n = rows
.first()
.and_then(|r| r.get("count"))
.and_then(|v| v.as_i64())
.unwrap_or(0);
Ok(json!({ "count": n }))
}
pub fn begin(url: &str) -> Result<Value, String> {
let conn = pooled(url)?;
{
let mut c = conn.lock().unwrap_or_else(|e| e.into_inner());
c.batch_execute("BEGIN").map_err(|e| e.to_string())?;
}
let id = next_txn_id();
{
let mut guard = TXNS.lock().unwrap_or_else(|e| e.into_inner());
guard
.get_or_insert_with(HashMap::new)
.insert(id.clone(), (url.to_string(), conn));
}
Ok(json!({ "_type": "txn", "txn": id, "url": url, "事务": id, "地址": url }))
}
pub fn commit(txn_id: &str) -> Result<Value, String> {
let (_, conn) = take_txn(txn_id)?;
let mut c = conn.lock().unwrap_or_else(|e| e.into_inner());
c.batch_execute("COMMIT").map_err(|e| e.to_string())?;
Ok(json!({ "ok": true }))
}
pub fn rollback(txn_id: &str) -> Result<Value, String> {
let (_, conn) = take_txn(txn_id)?;
let mut c = conn.lock().unwrap_or_else(|e| e.into_inner());
c.batch_execute("ROLLBACK").map_err(|e| e.to_string())?;
Ok(json!({ "ok": true }))
}
fn take_txn(txn_id: &str) -> Result<(String, Arc<Mutex<Client>>), String> {
let mut guard = TXNS.lock().unwrap_or_else(|e| e.into_inner());
guard
.as_mut()
.and_then(|m| m.remove(txn_id))
.ok_or_else(|| format!("unknown transaction `{txn_id}`"))
}
pub fn list_tables(url: &str) -> Result<Vec<String>, String> {
let conn = pooled(url)?;
let mut c = conn.lock().unwrap_or_else(|e| e.into_inner());
let rows = query_sql(
&mut c,
"SELECT tablename AS name FROM pg_catalog.pg_tables WHERE schemaname = 'public' ORDER BY tablename",
&[],
)?;
Ok(rows
.iter()
.filter_map(|r| r.get("name").and_then(|v| v.as_str()).map(|s| s.to_string()))
.collect())
}
pub fn table_info(url: &str, table: &str) -> Result<Vec<crate::db::ColumnInfo>, String> {
let table = ident(table)?;
let conn = pooled(url)?;
let mut c = conn.lock().unwrap_or_else(|e| e.into_inner());
let rows = query_sql(
&mut c,
"SELECT column_name AS name, data_type AS sql_type, \
CASE WHEN is_nullable = 'NO' THEN true ELSE false END AS notnull \
FROM information_schema.columns \
WHERE table_schema = 'public' AND table_name = ? \
ORDER BY ordinal_position",
&[json!(table)],
)?;
Ok(rows
.iter()
.map(|r| crate::db::ColumnInfo {
name: r.get("name").and_then(|v| v.as_str()).unwrap_or("").into(),
sql_type: r
.get("sql_type")
.and_then(|v| v.as_str())
.unwrap_or("text")
.into(),
notnull: r.get("notnull").and_then(|v| v.as_bool()).unwrap_or(false),
pk: r.get("name").and_then(|v| v.as_str()) == Some("id"),
})
.collect())
}
+54
View File
@@ -0,0 +1,54 @@
//! Driver URL schemes for `# db` / `# cache` / `# storage` (design ext-web-drivers).
/// Which SQL backend a `# db` URL selects.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DbKind {
Sqlite,
Postgres,
}
pub fn db_kind(url: &str) -> DbKind {
let u = url.trim().to_ascii_lowercase();
if u.starts_with("postgres://") || u.starts_with("postgresql://") {
DbKind::Postgres
} else {
DbKind::Sqlite
}
}
/// Rewrite SQL written with `?` placeholders into Postgres `$1,$2,…` form.
pub fn rewrite_placeholders_pg(sql: &str) -> String {
let mut out = String::with_capacity(sql.len() + 8);
let mut n = 0usize;
let mut chars = sql.chars().peekable();
while let Some(c) = chars.next() {
if c == '?' {
n += 1;
out.push('$');
out.push_str(&n.to_string());
} else {
out.push(c);
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn kind_from_url() {
assert_eq!(db_kind("sqlite:a.db"), DbKind::Sqlite);
assert_eq!(db_kind("postgres://u:p@h/db"), DbKind::Postgres);
assert_eq!(db_kind("postgresql://localhost/x"), DbKind::Postgres);
}
#[test]
fn rewrite_qmark() {
assert_eq!(
rewrite_placeholders_pg("SELECT * FROM t WHERE a = ? AND b IN (?, ?)"),
"SELECT * FROM t WHERE a = $1 AND b IN ($2, $3)"
);
}
}
+99 -2
View File
@@ -2,8 +2,11 @@
mod compose;
mod db;
mod db_pg;
mod driver;
mod form;
mod http;
mod cache;
mod markdown;
mod middleware;
mod password;
@@ -11,6 +14,7 @@ mod rate_limit;
mod render;
mod rss;
mod session;
mod storage;
mod table;
mod ws;
@@ -158,8 +162,15 @@ fn entry_dir() -> PathBuf {
})
}
/// `sqlite:` URL with a relative path → absolute against `entry_dir()`.
/// Resolve a DB URL for the plugin.
///
/// - `postgres://` / `postgresql://` — passed through unchanged.
/// - `sqlite:` / bare relative path — made absolute against `entry_dir()`.
fn resolve_db_url(url: &str) -> String {
let lower = url.to_ascii_lowercase();
if lower.starts_with("postgres://") || lower.starts_with("postgresql://") {
return url.to_string();
}
let stripped = url
.strip_prefix("sqlite:")
.or_else(|| url.strip_prefix("SQLITE:"))
@@ -452,7 +463,78 @@ web_ffi!(web_render, |args: &Value| {
web_ffi!(web_db_new, |args: &Value| {
let url = arg_str_opt(args, "url").unwrap_or("sqlite:site.db");
Ok(json!({ "url": url }))
Ok(json!({ "url": resolve_db_url(url), "_type": "db" }))
});
web_ffi!(web_cache_new, |args: &Value| {
let url = arg_str_opt(args, "url").unwrap_or("memory:");
cache::open(url)
});
web_ffi!(web_cache_get, |args: &Value| {
let url = arg_str(args, "url")?;
let key = arg_str(args, "key")?;
cache::get(url, key)
});
web_ffi!(web_cache_set, |args: &Value| {
let url = arg_str(args, "url")?;
let key = arg_str(args, "key")?;
let value = arg_text(args, "value")?;
let ttl = args
.get("ttl")
.and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|i| i as u64)));
cache::set(url, key, &value, ttl)
});
web_ffi!(web_cache_del, |args: &Value| {
let url = arg_str(args, "url")?;
let key = arg_str(args, "key")?;
cache::del(url, key)
});
web_ffi!(web_cache_exists, |args: &Value| {
let url = arg_str(args, "url")?;
let key = arg_str(args, "key")?;
cache::exists(url, key)
});
web_ffi!(web_cache_ttl, |args: &Value| {
let url = arg_str(args, "url")?;
let key = arg_str(args, "key")?;
cache::ttl(url, key)
});
web_ffi!(web_storage_new, |args: &Value| {
let url = arg_str(args, "url").unwrap_or("file:data/blobs");
storage::open(url)
});
web_ffi!(web_storage_put, |args: &Value| {
let url = arg_str(args, "url")?;
let key = arg_str(args, "key")?;
let body = arg_str_opt(args, "body");
let path = arg_str_opt(args, "path");
let content_type = arg_str_opt(args, "content_type");
storage::put(url, key, body, path, content_type)
});
web_ffi!(web_storage_get, |args: &Value| {
let url = arg_str(args, "url")?;
let key = arg_str(args, "key")?;
storage::get(url, key)
});
web_ffi!(web_storage_delete, |args: &Value| {
let url = arg_str(args, "url")?;
let key = arg_str(args, "key")?;
storage::delete(url, key)
});
web_ffi!(web_storage_list, |args: &Value| {
let url = arg_str(args, "url")?;
let prefix = arg_str_opt(args, "prefix");
storage::list(url, prefix)
});
web_ffi!(web_db_init, |args: &Value| {
@@ -1249,6 +1331,21 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int
),
("web_render", "page", web_render as PluginFn),
("web_db_new", "url", web_db_new as PluginFn),
("web_cache_new", "url", web_cache_new as PluginFn),
("web_cache_get", "url,key", web_cache_get as PluginFn),
("web_cache_set", "url,key,value,ttl", web_cache_set as PluginFn),
("web_cache_del", "url,key", web_cache_del as PluginFn),
("web_cache_exists", "url,key", web_cache_exists as PluginFn),
("web_cache_ttl", "url,key", web_cache_ttl as PluginFn),
("web_storage_new", "url", web_storage_new as PluginFn),
(
"web_storage_put",
"url,key,body,path,content_type",
web_storage_put as PluginFn,
),
("web_storage_get", "url,key", web_storage_get as PluginFn),
("web_storage_delete", "url,key", web_storage_delete as PluginFn),
("web_storage_list", "url,prefix", web_storage_list as PluginFn),
("web_db_init", "url,name,fields", web_db_init as PluginFn),
("web_db_insert", "url,table,rows,txn", web_db_insert as PluginFn),
(
+607
View File
@@ -0,0 +1,607 @@
//! `# storage` — local `file:` blobs; `s3://` reserved for live S3/MinIO (SigV4 later).
use serde_json::{json, Value};
use std::fs;
use std::path::{Path, PathBuf};
fn is_file(url: &str) -> bool {
let u = url.trim().to_ascii_lowercase();
u.starts_with("file:") || u.starts_with("file://")
}
fn is_s3(url: &str) -> bool {
url.trim().to_ascii_lowercase().starts_with("s3://")
}
fn file_root(url: &str) -> Result<PathBuf, String> {
let raw = url
.trim()
.strip_prefix("file://")
.or_else(|| url.trim().strip_prefix("file:"))
.unwrap_or(url.trim());
let p = PathBuf::from(raw);
if !p.as_os_str().is_empty() {
fs::create_dir_all(&p).map_err(|e| format!("storage mkdir: {e}"))?;
}
Ok(p)
}
fn safe_key(key: &str) -> Result<String, String> {
let k = key.trim().trim_start_matches('/');
if k.is_empty() {
return Err("storage key is empty".into());
}
if k.contains("..") {
return Err("storage key must not contain `..`".into());
}
Ok(k.replace('\\', "/"))
}
fn path_for(root: &Path, key: &str) -> Result<PathBuf, String> {
let k = safe_key(key)?;
Ok(root.join(k))
}
/// Open storage. For `file:` ensures the directory exists.
pub fn open(url: &str) -> Result<Value, String> {
let url = url.trim();
if is_file(url) {
let _ = file_root(url)?;
Ok(json!({ "_type": "storage", "url": url, "backend": "file" }))
} else if is_s3(url) {
// Validate shape early so author typos fail at open, not at put.
let rest = url
.strip_prefix("s3://")
.ok_or_else(|| "bad s3 url".to_string())?;
let bucket = rest.split('?').next().unwrap_or("");
if bucket.is_empty() {
return Err("s3 url missing bucket (s3://bucket?endpoint=…)".into());
}
Ok(json!({ "_type": "storage", "url": url, "backend": "s3" }))
} else {
Err(format!(
"storage url must be `file:…` or `s3://bucket?…`, got `{url}`"
))
}
}
fn load_bytes(body: Option<&str>, path: Option<&str>) -> Result<Vec<u8>, String> {
if let Some(p) = path.filter(|s| !s.is_empty()) {
return fs::read(p).map_err(|e| format!("read path `{p}`: {e}"));
}
if let Some(b) = body {
return Ok(b.as_bytes().to_vec());
}
Err("storage.put needs `body` or `path`".into())
}
pub fn put(
url: &str,
key: &str,
body: Option<&str>,
path: Option<&str>,
content_type: Option<&str>,
) -> Result<Value, String> {
let bytes = load_bytes(body, path)?;
let ct = content_type.unwrap_or("application/octet-stream");
if is_file(url) {
let root = file_root(url)?;
let dest = path_for(&root, key)?;
if let Some(parent) = dest.parent() {
fs::create_dir_all(parent).map_err(|e| e.to_string())?;
}
fs::write(&dest, &bytes).map_err(|e| e.to_string())?;
let _ = fs::write(ctype_path(&dest), ct);
Ok(json!({ "ok": true, "key": key, "size": bytes.len() }))
} else if is_s3(url) {
s3_put_http(url, key, &bytes, ct)
} else {
Err("unknown storage backend".into())
}
}
fn ctype_path(dest: &Path) -> PathBuf {
PathBuf::from(format!("{}.ctype", dest.display()))
}
pub fn get(url: &str, key: &str) -> Result<Value, String> {
if is_file(url) {
let root = file_root(url)?;
let dest = path_for(&root, key)?;
if !dest.is_file() {
return Ok(json!({ "ok": false }));
}
let bytes = fs::read(&dest).map_err(|e| e.to_string())?;
let ct = fs::read_to_string(ctype_path(&dest))
.unwrap_or_else(|_| "application/octet-stream".into());
match String::from_utf8(bytes.clone()) {
Ok(s) => Ok(json!({
"ok": true,
"key": key,
"body": s,
"content_type": ct,
"size": s.len(),
})),
Err(_) => Ok(json!({
"ok": true,
"key": key,
"path": dest.to_string_lossy(),
"content_type": ct,
"size": bytes.len(),
"binary": true,
})),
}
} else if is_s3(url) {
s3_get_http(url, key)
} else {
Err("unknown storage backend".into())
}
}
pub fn delete(url: &str, key: &str) -> Result<Value, String> {
if is_file(url) {
let root = file_root(url)?;
let dest = path_for(&root, key)?;
let ok = if dest.is_file() {
fs::remove_file(&dest).map_err(|e| e.to_string())?;
let _ = fs::remove_file(ctype_path(&dest));
true
} else {
false
};
Ok(json!({ "ok": ok }))
} else if is_s3(url) {
s3_delete_http(url, key)
} else {
Err("unknown storage backend".into())
}
}
pub fn list(url: &str, prefix: Option<&str>) -> Result<Value, String> {
let prefix = prefix.unwrap_or("").trim_start_matches('/');
if is_file(url) {
let root = file_root(url)?;
let mut keys = Vec::new();
walk_keys(&root, &root, prefix, &mut keys)?;
keys.sort();
Ok(json!({ "ok": true, "keys": keys, "count": keys.len() }))
} else if is_s3(url) {
s3_list_http(url, prefix)
} else {
Err("unknown storage backend".into())
}
}
fn walk_keys(root: &Path, dir: &Path, prefix: &str, out: &mut Vec<String>) -> Result<(), String> {
let entries = match fs::read_dir(dir) {
Ok(e) => e,
Err(_) => return Ok(()),
};
for ent in entries {
let ent = ent.map_err(|e| e.to_string())?;
let path = ent.path();
if path
.file_name()
.and_then(|n| n.to_str())
.is_some_and(|n| n.ends_with(".ctype"))
{
continue;
}
if path.is_dir() {
walk_keys(root, &path, prefix, out)?;
} else if path.is_file() {
let rel = path
.strip_prefix(root)
.map_err(|e| e.to_string())?
.to_string_lossy()
.replace('\\', "/");
if prefix.is_empty() || rel.starts_with(prefix) {
out.push(rel);
}
}
}
Ok(())
}
/// Live S3/MinIO via AWS SigV4. Requires `endpoint` query for path-style (MinIO).
fn s3_put_http(url: &str, key: &str, body: &[u8], content_type: &str) -> Result<Value, String> {
let cfg = S3Conf::parse(url)?;
let target = cfg.object_url(key)?;
let status = cfg.request("PUT", &target, body, content_type)?;
if (200..300).contains(&status) {
Ok(json!({ "ok": true, "key": key, "size": body.len() }))
} else {
Err(format!("s3 put HTTP {status}"))
}
}
fn s3_get_http(url: &str, key: &str) -> Result<Value, String> {
let cfg = S3Conf::parse(url)?;
let target = cfg.object_url(key)?;
let (status, ct, bytes) = cfg.request_bytes("GET", &target)?;
if status == 404 {
return Ok(json!({ "ok": false }));
}
if !(200..300).contains(&status) {
return Err(format!("s3 get HTTP {status}"));
}
match String::from_utf8(bytes.clone()) {
Ok(s) => Ok(json!({
"ok": true, "key": key, "body": s, "content_type": ct, "size": s.len()
})),
Err(_) => Ok(json!({
"ok": true, "key": key, "content_type": ct, "size": bytes.len(), "binary": true
})),
}
}
fn s3_delete_http(url: &str, key: &str) -> Result<Value, String> {
let cfg = S3Conf::parse(url)?;
let target = cfg.object_url(key)?;
let status = cfg.request("DELETE", &target, &[], "")?;
if (200..300).contains(&status) || status == 204 || status == 404 {
Ok(json!({ "ok": status != 404 }))
} else {
Err(format!("s3 delete HTTP {status}"))
}
}
fn s3_list_http(url: &str, prefix: &str) -> Result<Value, String> {
let cfg = S3Conf::parse(url)?;
let list_url = cfg.list_url(prefix);
let (status, _ct, bytes) = cfg.request_bytes("GET", &list_url)?;
let text = String::from_utf8_lossy(&bytes).into_owned();
if !(200..300).contains(&status) {
return Err(format!("s3 list HTTP {status}: {text}"));
}
let mut keys = Vec::new();
for part in text.split("<Key>").skip(1) {
if let Some(end) = part.find("</Key>") {
keys.push(part[..end].to_string());
}
}
Ok(json!({ "ok": true, "keys": keys, "count": keys.len() }))
}
struct S3Conf {
bucket: String,
endpoint: String,
region: String,
access_key: String,
secret_key: String,
path_style: bool,
}
impl S3Conf {
fn parse(url: &str) -> Result<Self, String> {
let rest = url
.trim()
.strip_prefix("s3://")
.ok_or_else(|| "not an s3 url".to_string())?;
let (bucket, query) = match rest.split_once('?') {
Some((b, q)) => (b.to_string(), q),
None => (rest.to_string(), ""),
};
if bucket.is_empty() {
return Err("s3 url missing bucket".into());
}
let mut endpoint = String::new();
let mut region = "us-east-1".to_string();
let mut access_key = std::env::var("AWS_ACCESS_KEY_ID")
.or_else(|_| std::env::var("S3_ACCESS_KEY"))
.unwrap_or_default();
let mut secret_key = std::env::var("AWS_SECRET_ACCESS_KEY")
.or_else(|_| std::env::var("S3_SECRET_KEY"))
.unwrap_or_default();
let mut path_style = true;
for pair in query.split('&').filter(|p| !p.is_empty()) {
let (k, v) = pair.split_once('=').unwrap_or((pair, ""));
let v = pct_decode(v);
match k {
"endpoint" => endpoint = v,
"region" => region = v,
"access_key" | "accessKey" => access_key = v,
"secret_key" | "secretKey" => secret_key = v,
"path_style" => {
path_style = matches!(v.as_str(), "1" | "true" | "True" | "yes")
}
_ => {}
}
}
if endpoint.is_empty() {
return Err(
"s3 url needs `endpoint=` for MinIO/path-style (e.g. s3://bucket?endpoint=http://127.0.0.1:9000)"
.into(),
);
}
if access_key.is_empty() || secret_key.is_empty() {
return Err(
"s3 credentials missing (access_key/secret_key query or AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY)"
.into(),
);
}
Ok(Self {
bucket,
endpoint: endpoint.trim_end_matches('/').to_string(),
region,
access_key,
secret_key,
path_style,
})
}
fn object_url(&self, key: &str) -> Result<String, String> {
let k = safe_key(key)?;
if self.path_style {
Ok(format!("{}/{}/{}", self.endpoint, self.bucket, k))
} else {
Err("virtual-hosted s3 not implemented in this wave — set path_style=true".into())
}
}
fn list_url(&self, prefix: &str) -> String {
format!(
"{}/{}?list-type=2&prefix={}",
self.endpoint,
self.bucket,
uri_encode(prefix)
)
}
fn request(
&self,
method: &str,
url: &str,
body: &[u8],
content_type: &str,
) -> Result<u16, String> {
let (status, _, _) = self.request_inner(method, url, body, content_type)?;
Ok(status)
}
fn request_bytes(&self, method: &str, url: &str) -> Result<(u16, String, Vec<u8>), String> {
self.request_inner(method, url, &[], "")
}
fn request_inner(
&self,
method: &str,
url: &str,
body: &[u8],
content_type: &str,
) -> Result<(u16, String, Vec<u8>), String> {
use hmac::{Hmac, Mac};
use sha2::{Digest, Sha256};
type HmacSha256 = Hmac<Sha256>;
let (scheme_host, path_query) = split_url(url)?;
let (path, query) = match path_query.split_once('?') {
Some((p, q)) => (p, q),
None => (path_query.as_str(), ""),
};
let host = scheme_host
.split("://")
.nth(1)
.ok_or_else(|| "bad url host".to_string())?;
let amz_date = amz_timestamp();
let date_stamp = &amz_date[..8];
let payload_hash = hex::encode(Sha256::digest(body));
let mut hdrs: Vec<(String, String)> = vec![
("host".into(), host.into()),
("x-amz-content-sha256".into(), payload_hash.clone()),
("x-amz-date".into(), amz_date.clone()),
];
if method == "PUT" && !content_type.is_empty() {
hdrs.push(("content-type".into(), content_type.into()));
}
hdrs.sort_by(|a, b| a.0.cmp(&b.0));
let signed_headers = hdrs.iter().map(|(k, _)| k.as_str()).collect::<Vec<_>>().join(";");
let canonical_headers = hdrs
.iter()
.map(|(k, v)| format!("{k}:{}\n", v.trim()))
.collect::<String>();
let canonical_query = canonical_query_string(query);
let canonical_request = format!(
"{method}\n{path}\n{canonical_query}\n{canonical_headers}\n{signed_headers}\n{payload_hash}"
);
let scope = format!("{date_stamp}/{}/s3/aws4_request", self.region);
let string_to_sign = format!(
"AWS4-HMAC-SHA256\n{amz_date}\n{scope}\n{}",
hex::encode(Sha256::digest(canonical_request.as_bytes()))
);
let mut mac =
HmacSha256::new_from_slice(format!("AWS4{}", self.secret_key).as_bytes()).unwrap();
mac.update(date_stamp.as_bytes());
let k_date = mac.finalize().into_bytes();
let mut mac = HmacSha256::new_from_slice(&k_date).unwrap();
mac.update(self.region.as_bytes());
let k_region = mac.finalize().into_bytes();
let mut mac = HmacSha256::new_from_slice(&k_region).unwrap();
mac.update(b"s3");
let k_service = mac.finalize().into_bytes();
let mut mac = HmacSha256::new_from_slice(&k_service).unwrap();
mac.update(b"aws4_request");
let k_signing = mac.finalize().into_bytes();
let mut mac = HmacSha256::new_from_slice(&k_signing).unwrap();
mac.update(string_to_sign.as_bytes());
let signature = hex::encode(mac.finalize().into_bytes());
let auth = format!(
"AWS4-HMAC-SHA256 Credential={}/{scope}, SignedHeaders={signed_headers}, Signature={signature}",
self.access_key
);
let agent = ureq::AgentBuilder::new()
.timeout(std::time::Duration::from_secs(30))
.build();
let mut req = agent.request(method, url);
req = req
.set("Authorization", &auth)
.set("x-amz-content-sha256", &payload_hash)
.set("x-amz-date", &amz_date);
if method == "PUT" && !content_type.is_empty() {
req = req.set("Content-Type", content_type);
}
let result = if method == "PUT" {
req.send_bytes(body)
} else {
req.call()
};
match result {
Ok(r) => {
let status = r.status();
let ct = r
.header("content-type")
.unwrap_or("application/octet-stream")
.to_string();
let mut buf = Vec::new();
let _ = std::io::Read::read_to_end(&mut r.into_reader(), &mut buf);
Ok((status, ct, buf))
}
Err(ureq::Error::Status(code, r)) => {
let ct = r
.header("content-type")
.unwrap_or("application/octet-stream")
.to_string();
let mut buf = Vec::new();
let _ = std::io::Read::read_to_end(&mut r.into_reader(), &mut buf);
Ok((code, ct, buf))
}
Err(e) => Err(e.to_string()),
}
}
}
fn split_url(url: &str) -> Result<(String, String), String> {
let without = url
.strip_prefix("https://")
.or_else(|| url.strip_prefix("http://"))
.ok_or_else(|| "url must be http(s)".to_string())?;
let scheme = if url.starts_with("https://") {
"https"
} else {
"http"
};
let (hostport, rest) = match without.find('/') {
Some(i) => (&without[..i], &without[i..]),
None => (without, "/"),
};
Ok((format!("{scheme}://{hostport}"), rest.to_string()))
}
fn amz_timestamp() -> String {
let secs = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
// UTC YYYYMMDDTHHMMSSZ via a tiny formatter (no chrono dep).
let days = secs / 86400;
let tod = secs % 86400;
let hour = tod / 3600;
let min = (tod % 3600) / 60;
let sec = tod % 60;
let (y, m, d) = civil_from_days(days as i64);
format!("{y:04}{m:02}{d:02}T{hour:02}{min:02}{sec:02}Z")
}
/// Algorithm from Howard Hinnant's date algorithms (days since 1970-01-01 → Y-M-D).
fn civil_from_days(z: i64) -> (i32, u32, u32) {
let z = z + 719468;
let era = if z >= 0 { z } else { z - 146096 } / 146097;
let doe = (z - era * 146097) as u64;
let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
let y = (yoe as i64) + era * 400;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let d = doy - (153 * mp + 2) / 5 + 1;
let m = if mp < 10 { mp + 3 } else { mp - 9 };
let y = if m <= 2 { y + 1 } else { y };
(y as i32, m as u32, d as u32)
}
fn uri_encode(s: &str) -> String {
let mut out = String::new();
for b in s.bytes() {
match b {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
out.push(b as char)
}
_ => out.push_str(&format!("%{b:02X}")),
}
}
out
}
fn canonical_query_string(query: &str) -> String {
if query.is_empty() {
return String::new();
}
let mut pairs: Vec<(String, String)> = query
.split('&')
.filter(|p| !p.is_empty())
.map(|p| {
let (k, v) = p.split_once('=').unwrap_or((p, ""));
(pct_decode(k), pct_decode(v))
})
.collect();
pairs.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.cmp(&b.1)));
pairs
.iter()
.map(|(k, v)| format!("{}={}", uri_encode(k), uri_encode(v)))
.collect::<Vec<_>>()
.join("&")
}
fn pct_decode(s: &str) -> String {
let mut out = String::with_capacity(s.len());
let b = s.as_bytes();
let mut i = 0;
while i < b.len() {
if b[i] == b'%' && i + 2 < b.len() {
if let Ok(v) = u8::from_str_radix(std::str::from_utf8(&b[i + 1..i + 3]).unwrap_or(""), 16)
{
out.push(v as char);
i += 3;
continue;
}
}
out.push(b[i] as char);
i += 1;
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::{SystemTime, UNIX_EPOCH};
#[test]
fn file_put_get_list_delete() {
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
let dir = std::env::temp_dir().join(format!("marqdo-storage-{nanos}"));
let url = format!("file:{}", dir.display());
open(&url).unwrap();
put(&url, "a/hello.txt", Some("hi"), None, Some("text/plain")).unwrap();
let g = get(&url, "a/hello.txt").unwrap();
assert_eq!(g["ok"], true);
assert_eq!(g["body"], "hi");
let ls = list(&url, Some("a/")).unwrap();
assert_eq!(ls["count"], 1);
delete(&url, "a/hello.txt").unwrap();
assert_eq!(get(&url, "a/hello.txt").unwrap()["ok"], false);
let _ = fs::remove_dir_all(dir);
}
#[test]
fn s3_open_validates_bucket() {
let r = open("s3://mybucket?endpoint=http://127.0.0.1:9000");
assert!(r.is_ok());
assert!(open("s3://").is_err());
}
}
+72
View File
@@ -0,0 +1,72 @@
---
title: web drivers smoke (W4)
description: memory cache + file storage + postgres URL shape (offline).
import web:ext/web/web.mq.md
import plugin:lib/plugin.mq.md
import sys:lib/sys.mq.md
---
# main
*p = > plugin.native_path name="web"*
1. `p`
> plugin.load path=`p`
2. *
> sys.exit code=1
<!-- cache: memory backend -->
*c = > web.cache url="memory:drivers-smoke"*
> `c`.set key="k1" value="v1" ttl=60
*g = > `c`.get key="k1"*
*gv = g[^value]*
1. `gv` == "v1"
> print text=cache-ok
2. *
> print text=cache-fail
> `c`.del key="k1"
*ex = > `c`.exists key="k1"*
*eok = ex[^ok]*
1. `eok`
> print text=cache-del-fail
2. *
> print text=cache-del-ok
<!-- storage: file backend -->
*blob = > web.storage url="file:tests/ext/web-fixtures/data/drivers-blobs"*
> `blob`.put key="notes/a.txt" body="hello" content_type="text/plain"
*got = > `blob`.get key="notes/a.txt"*
*body = got[^body]*
1. `body` == "hello"
> print text=storage-ok
2. *
> print text=storage-fail
*ls = > `blob`.list prefix="notes/"*
*n = ls[^count]*
1. `n` == 1
> print text=storage-list-ok
2. *
> print text=storage-list-fail
> `blob`.delete key="notes/a.txt"
<!-- db: postgres URL is accepted as a handle (no live server required) -->
*pg = > web.db url="postgres://marqdo:marqdo@127.0.0.1:5432/marqdo"*
*purl = pg[^url]*
1. `purl` == "postgres://marqdo:marqdo@127.0.0.1:5432/marqdo"
> print text=postgres-url-ok
2. *
> print text=postgres-url-fail
<!-- s3 URL shape validates at open -->
*s3 = > web.storage url="s3://bucket?endpoint=http://127.0.0.1:9000"*
*sb = s3[^backend]*
1. `sb` == "s3"
> print text=s3-open-ok
2. *
> print text=s3-open-fail
+25
View File
@@ -1740,6 +1740,31 @@ rss-ok",
);
}
#[test]
fn ext_web_drivers_smoke() {
// W4 drivers: memory cache, file storage, postgres/s3 URL shape (offline).
let status = Command::new("cargo")
.args(["build", "-p", "marqdo_plugin_web"])
.current_dir(env!("CARGO_MANIFEST_DIR"))
.status()
.expect("build web plugin");
assert!(status.success(), "failed to build marqdo_plugin_web");
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/ext/web-fixtures/data/drivers-blobs");
let _ = std::fs::remove_dir_all(&root);
assert_out(
"tests/ext/web-drivers-smoke.mq.md",
"cache-ok
cache-del-ok
storage-ok
storage-list-ok
postgres-url-ok
s3-open-ok",
);
}
#[test]
fn ext_web_db_w2_smoke() {
// W2 data layer: transactions, connection pooling, pagination, query