添加帖子模型、服务和路由,重构数据库连接管理,优化环境变量配置

This commit is contained in:
rsgl 2024-11-28 00:19:40 +08:00
parent e28e759364
commit b60909f240
13 changed files with 650 additions and 79 deletions

6
.env
View File

@ -1,6 +1,2 @@
RUST_LOG=info RUST_LOG=info
DB_MYSQL_USER=rsgl DB_MYSQL_URL=mysql://rsgl:0andrx@122.152.201.90:9912/test_server
DB_MYSQL_PASSWORD=0andrx
DB_MYSQL_URL=122.152.201.90
DB_MYSQL_PORT=9912
DB_MYSQL_NAME=test_server

481
Cargo.lock generated
View File

@ -104,7 +104,7 @@ dependencies = [
"actix-utils", "actix-utils",
"futures-core", "futures-core",
"futures-util", "futures-util",
"mio", "mio 1.0.2",
"socket2", "socket2",
"tokio", "tokio",
"tracing", "tracing",
@ -289,7 +289,7 @@ dependencies = [
"miniz_oxide", "miniz_oxide",
"object", "object",
"rustc-demangle", "rustc-demangle",
"windows-targets", "windows-targets 0.52.6",
] ]
[[package]] [[package]]
@ -317,6 +317,17 @@ dependencies = [
"zeroize", "zeroize",
] ]
[[package]]
name = "bigdecimal"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6773ddc0eafc0e509fb60e48dff7f450f8e674a0686ae8605e8d9901bd5eefa"
dependencies = [
"num-bigint",
"num-integer",
"num-traits",
]
[[package]] [[package]]
name = "bigdecimal" name = "bigdecimal"
version = "0.4.6" version = "0.4.6"
@ -402,7 +413,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2593a3b8b938bd68373196c9832f516be11fa487ef4ae745eb282e6a56a7244" checksum = "c2593a3b8b938bd68373196c9832f516be11fa487ef4ae745eb282e6a56a7244"
dependencies = [ dependencies = [
"once_cell", "once_cell",
"proc-macro-crate", "proc-macro-crate 3.2.0",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.89", "syn 2.0.89",
@ -542,9 +553,9 @@ dependencies = [
[[package]] [[package]]
name = "cmake" name = "cmake"
version = "0.1.51" version = "0.1.52"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb1e43aa7fd152b1f968787f7dbcdeb306d1867ff373c69955211876c053f91a" checksum = "c682c223677e0e5b6b7f63a64b9351844c3f1b1678a68b7ee617e30fb082620e"
dependencies = [ dependencies = [
"cc", "cc",
] ]
@ -711,6 +722,7 @@ dependencies = [
"env_logger", "env_logger",
"log", "log",
"mysql", "mysql",
"mysql_async",
"r2d2", "r2d2",
"r2d2_mysql", "r2d2_mysql",
"serde", "serde",
@ -944,6 +956,17 @@ version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
[[package]]
name = "futures-macro"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.89",
]
[[package]] [[package]]
name = "futures-sink" name = "futures-sink"
version = "0.3.31" version = "0.3.31"
@ -963,9 +986,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"futures-macro",
"futures-task", "futures-task",
"pin-project-lite", "pin-project-lite",
"pin-utils", "pin-utils",
"slab",
] ]
[[package]] [[package]]
@ -1013,7 +1038,7 @@ dependencies = [
"futures-sink", "futures-sink",
"futures-util", "futures-util",
"http", "http",
"indexmap", "indexmap 2.6.0",
"slab", "slab",
"tokio", "tokio",
"tokio-util", "tokio-util",
@ -1031,15 +1056,30 @@ dependencies = [
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.15.1" version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a9bfc1af68b1726ea47d3d5109de126281def866b33970e10fbab11b5dafab3" checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e"
dependencies = [
"ahash 0.8.11",
]
[[package]]
name = "hashbrown"
version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289"
dependencies = [ dependencies = [
"allocator-api2", "allocator-api2",
"equivalent", "equivalent",
"foldhash", "foldhash",
] ]
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]] [[package]]
name = "heck" name = "heck"
version = "0.5.0" version = "0.5.0"
@ -1241,6 +1281,16 @@ version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aae21c3177a27788957044151cc2800043d127acaa460a47ebb9b84dfa2c6aa0" checksum = "aae21c3177a27788957044151cc2800043d127acaa460a47ebb9b84dfa2c6aa0"
[[package]]
name = "indexmap"
version = "1.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99"
dependencies = [
"autocfg",
"hashbrown 0.12.3",
]
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "2.6.0" version = "2.6.0"
@ -1248,7 +1298,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da"
dependencies = [ dependencies = [
"equivalent", "equivalent",
"hashbrown 0.15.1", "hashbrown 0.15.2",
] ]
[[package]] [[package]]
@ -1280,9 +1330,9 @@ dependencies = [
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.13" version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "540654e97a3f4470a492cd30ff187bc95d89557a903a2bbf112e2fae98104ef2" checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
[[package]] [[package]]
name = "jobserver" name = "jobserver"
@ -1306,10 +1356,83 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]] [[package]]
name = "libc" name = "lexical"
version = "0.2.164" version = "6.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f" checksum = "c7aefb36fd43fef7003334742cbf77b243fcd36418a1d1bdd480d613a67968f6"
dependencies = [
"lexical-core",
]
[[package]]
name = "lexical-core"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2cde5de06e8d4c2faabc400238f9ae1c74d5412d03a7bd067645ccbc47070e46"
dependencies = [
"lexical-parse-float",
"lexical-parse-integer",
"lexical-util",
"lexical-write-float",
"lexical-write-integer",
]
[[package]]
name = "lexical-parse-float"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683b3a5ebd0130b8fb52ba0bdc718cc56815b6a097e28ae5a6997d0ad17dc05f"
dependencies = [
"lexical-parse-integer",
"lexical-util",
"static_assertions",
]
[[package]]
name = "lexical-parse-integer"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d0994485ed0c312f6d965766754ea177d07f9c00c9b82a5ee62ed5b47945ee9"
dependencies = [
"lexical-util",
"static_assertions",
]
[[package]]
name = "lexical-util"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5255b9ff16ff898710eb9eb63cb39248ea8a5bb036bea8085b1a767ff6c4e3fc"
dependencies = [
"static_assertions",
]
[[package]]
name = "lexical-write-float"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accabaa1c4581f05a3923d1b4cfd124c329352288b7b9da09e766b0668116862"
dependencies = [
"lexical-util",
"lexical-write-integer",
"static_assertions",
]
[[package]]
name = "lexical-write-integer"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1b6f3d1f4422866b68192d62f77bc5c700bee84f3069f2469d7bc8c77852446"
dependencies = [
"lexical-util",
"static_assertions",
]
[[package]]
name = "libc"
version = "0.2.166"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2ccc108bbc0b1331bd061864e7cd823c0cab660bbe6970e66e2c0614decde36"
[[package]] [[package]]
name = "libloading" name = "libloading"
@ -1318,7 +1441,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4" checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"windows-targets", "windows-targets 0.52.6",
] ]
[[package]] [[package]]
@ -1383,13 +1506,22 @@ version = "0.4.22"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
[[package]]
name = "lru"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "718e8fae447df0c7e1ba7f5189829e63fd536945c8988d61444c19039f16b670"
dependencies = [
"hashbrown 0.13.2",
]
[[package]] [[package]]
name = "lru" name = "lru"
version = "0.12.5" version = "0.12.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "234cf4f4a04dc1f57e24b96cc0cd600cf2af460d4161ac5ecdd0af8e1f3b2a38" checksum = "234cf4f4a04dc1f57e24b96cc0cd600cf2af460d4161ac5ecdd0af8e1f3b2a38"
dependencies = [ dependencies = [
"hashbrown 0.15.1", "hashbrown 0.15.2",
] ]
[[package]] [[package]]
@ -1419,6 +1551,18 @@ dependencies = [
"adler2", "adler2",
] ]
[[package]]
name = "mio"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c"
dependencies = [
"libc",
"log",
"wasi",
"windows-sys 0.48.0",
]
[[package]] [[package]]
name = "mio" name = "mio"
version = "1.0.2" version = "1.0.2"
@ -1444,11 +1588,11 @@ dependencies = [
"flate2", "flate2",
"io-enum", "io-enum",
"libc", "libc",
"lru", "lru 0.12.5",
"mysql_common", "mysql_common 0.32.4",
"named_pipe", "named_pipe",
"native-tls", "native-tls",
"pem", "pem 3.0.4",
"percent-encoding", "percent-encoding",
"serde", "serde",
"serde_json", "serde_json",
@ -1457,6 +1601,24 @@ dependencies = [
"url", "url",
] ]
[[package]]
name = "mysql-common-derive"
version = "0.30.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56b0d8a0db9bf6d2213e11f2c701cb91387b0614361625ab7b9743b41aa4938f"
dependencies = [
"darling",
"heck 0.4.1",
"num-bigint",
"proc-macro-crate 1.3.1",
"proc-macro-error",
"proc-macro2",
"quote",
"syn 2.0.89",
"termcolor",
"thiserror",
]
[[package]] [[package]]
name = "mysql-common-derive" name = "mysql-common-derive"
version = "0.31.2" version = "0.31.2"
@ -1464,9 +1626,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63c3512cf11487168e0e9db7157801bf5273be13055a9cc95356dc9e0035e49c" checksum = "63c3512cf11487168e0e9db7157801bf5273be13055a9cc95356dc9e0035e49c"
dependencies = [ dependencies = [
"darling", "darling",
"heck", "heck 0.5.0",
"num-bigint", "num-bigint",
"proc-macro-crate", "proc-macro-crate 3.2.0",
"proc-macro-error2", "proc-macro-error2",
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1475,6 +1637,77 @@ dependencies = [
"thiserror", "thiserror",
] ]
[[package]]
name = "mysql_async"
version = "0.32.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5272f59b5b1f93d65f7f826c1f025d6e410e89fb50a67e05aa20b35a55a8c0a"
dependencies = [
"bytes",
"crossbeam",
"flate2",
"futures-core",
"futures-sink",
"futures-util",
"lazy_static",
"lru 0.10.1",
"mio 0.8.11",
"mysql_common 0.30.6",
"native-tls",
"once_cell",
"pem 2.0.1",
"percent-encoding",
"pin-project",
"priority-queue",
"serde",
"serde_json",
"socket2",
"thiserror",
"tokio",
"tokio-native-tls",
"tokio-util",
"twox-hash",
"url",
]
[[package]]
name = "mysql_common"
version = "0.30.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57349d5a326b437989b6ee4dc8f2f34b0cc131202748414712a8e7d98952fc8c"
dependencies = [
"base64 0.21.7",
"bigdecimal 0.3.1",
"bindgen",
"bitflags",
"bitvec",
"byteorder",
"bytes",
"cc",
"cmake",
"crc32fast",
"flate2",
"frunk",
"lazy_static",
"lexical",
"mysql-common-derive 0.30.2",
"num-bigint",
"num-traits",
"rand",
"regex",
"rust_decimal",
"saturating",
"serde",
"serde_json",
"sha1",
"sha2",
"smallvec",
"subprocess",
"thiserror",
"time",
"uuid",
]
[[package]] [[package]]
name = "mysql_common" name = "mysql_common"
version = "0.32.4" version = "0.32.4"
@ -1482,7 +1715,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "478b0ff3f7d67b79da2b96f56f334431aef65e15ba4b29dd74a4236e29582bdc" checksum = "478b0ff3f7d67b79da2b96f56f334431aef65e15ba4b29dd74a4236e29582bdc"
dependencies = [ dependencies = [
"base64 0.21.7", "base64 0.21.7",
"bigdecimal", "bigdecimal 0.4.6",
"bindgen", "bindgen",
"bitflags", "bitflags",
"bitvec", "bitvec",
@ -1495,7 +1728,7 @@ dependencies = [
"flate2", "flate2",
"frunk", "frunk",
"lazy_static", "lazy_static",
"mysql-common-derive", "mysql-common-derive 0.31.2",
"num-bigint", "num-bigint",
"num-traits", "num-traits",
"rand", "rand",
@ -1663,7 +1896,7 @@ dependencies = [
"libc", "libc",
"redox_syscall", "redox_syscall",
"smallvec", "smallvec",
"windows-targets", "windows-targets 0.52.6",
] ]
[[package]] [[package]]
@ -1672,6 +1905,16 @@ version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]]
name = "pem"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b13fe415cdf3c8e44518e18a7c95a13431d9bdf6d15367d82b23c377fdd441a"
dependencies = [
"base64 0.21.7",
"serde",
]
[[package]] [[package]]
name = "pem" name = "pem"
version = "3.0.4" version = "3.0.4"
@ -1688,6 +1931,26 @@ version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
[[package]]
name = "pin-project"
version = "1.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be57f64e946e500c8ee36ef6331845d40a93055567ec57e8fae13efd33759b95"
dependencies = [
"pin-project-internal",
]
[[package]]
name = "pin-project-internal"
version = "1.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c0f5fad0874fc7abcd4d750e76917eaebbecaa2c20bde22e1dbeeba8beb758c"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.89",
]
[[package]] [[package]]
name = "pin-project-lite" name = "pin-project-lite"
version = "0.2.15" version = "0.2.15"
@ -1721,13 +1984,57 @@ dependencies = [
"zerocopy", "zerocopy",
] ]
[[package]]
name = "priority-queue"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0bda9164fe05bc9225752d54aae413343c36f684380005398a6a8fde95fe785"
dependencies = [
"autocfg",
"indexmap 1.9.3",
]
[[package]]
name = "proc-macro-crate"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919"
dependencies = [
"once_cell",
"toml_edit 0.19.15",
]
[[package]] [[package]]
name = "proc-macro-crate" name = "proc-macro-crate"
version = "3.2.0" version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ecf48c7ca261d60b74ab1a7b20da18bede46776b2e55535cb958eb595c5fa7b" checksum = "8ecf48c7ca261d60b74ab1a7b20da18bede46776b2e55535cb958eb595c5fa7b"
dependencies = [ dependencies = [
"toml_edit", "toml_edit 0.22.22",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn 1.0.109",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
] ]
[[package]] [[package]]
@ -2342,7 +2649,7 @@ dependencies = [
"backtrace", "backtrace",
"bytes", "bytes",
"libc", "libc",
"mio", "mio 1.0.2",
"parking_lot", "parking_lot",
"pin-project-lite", "pin-project-lite",
"signal-hook-registry", "signal-hook-registry",
@ -2350,6 +2657,16 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "tokio-native-tls"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2"
dependencies = [
"native-tls",
"tokio",
]
[[package]] [[package]]
name = "tokio-util" name = "tokio-util"
version = "0.7.12" version = "0.7.12"
@ -2369,15 +2686,26 @@ version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41"
[[package]]
name = "toml_edit"
version = "0.19.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421"
dependencies = [
"indexmap 2.6.0",
"toml_datetime",
"winnow 0.5.40",
]
[[package]] [[package]]
name = "toml_edit" name = "toml_edit"
version = "0.22.22" version = "0.22.22"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5" checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5"
dependencies = [ dependencies = [
"indexmap", "indexmap 2.6.0",
"toml_datetime", "toml_datetime",
"winnow", "winnow 0.6.20",
] ]
[[package]] [[package]]
@ -2393,9 +2721,9 @@ dependencies = [
[[package]] [[package]]
name = "tracing-core" name = "tracing-core"
version = "0.1.32" version = "0.1.33"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c"
dependencies = [ dependencies = [
"once_cell", "once_cell",
] ]
@ -2504,13 +2832,22 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.5",
]
[[package]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.52.0" version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [ dependencies = [
"windows-targets", "windows-targets 0.52.6",
] ]
[[package]] [[package]]
@ -2519,7 +2856,22 @@ version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [ dependencies = [
"windows-targets", "windows-targets 0.52.6",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc 0.48.5",
"windows_i686_gnu 0.48.5",
"windows_i686_msvc 0.48.5",
"windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc 0.48.5",
] ]
[[package]] [[package]]
@ -2528,28 +2880,46 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [ dependencies = [
"windows_aarch64_gnullvm", "windows_aarch64_gnullvm 0.52.6",
"windows_aarch64_msvc", "windows_aarch64_msvc 0.52.6",
"windows_i686_gnu", "windows_i686_gnu 0.52.6",
"windows_i686_gnullvm", "windows_i686_gnullvm",
"windows_i686_msvc", "windows_i686_msvc 0.52.6",
"windows_x86_64_gnu", "windows_x86_64_gnu 0.52.6",
"windows_x86_64_gnullvm", "windows_x86_64_gnullvm 0.52.6",
"windows_x86_64_msvc", "windows_x86_64_msvc 0.52.6",
] ]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]] [[package]]
name = "windows_aarch64_gnullvm" name = "windows_aarch64_gnullvm"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]] [[package]]
name = "windows_aarch64_msvc" name = "windows_aarch64_msvc"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]] [[package]]
name = "windows_i686_gnu" name = "windows_i686_gnu"
version = "0.52.6" version = "0.52.6"
@ -2562,30 +2932,63 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]] [[package]]
name = "windows_i686_msvc" name = "windows_i686_msvc"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]] [[package]]
name = "windows_x86_64_gnu" name = "windows_x86_64_gnu"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]] [[package]]
name = "windows_x86_64_gnullvm" name = "windows_x86_64_gnullvm"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]] [[package]]
name = "windows_x86_64_msvc" name = "windows_x86_64_msvc"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "winnow"
version = "0.5.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "winnow" name = "winnow"
version = "0.6.20" version = "0.6.20"

View File

@ -7,6 +7,7 @@ edition = "2021"
mysql = "25.0.1" mysql = "25.0.1"
r2d2 = "0.8.10" r2d2 = "0.8.10"
r2d2_mysql = "25.0.0" r2d2_mysql = "25.0.0"
mysql_async = "0.32.0"
actix-web = "4" actix-web = "4"
serde = "1.0" serde = "1.0"
serde_derive = "1.0" serde_derive = "1.0"

View File

@ -1,7 +1,10 @@
use actix_web::{web, App, HttpServer}; use actix_web::{web, App, HttpServer};
use dotenv::dotenv; use dotenv::dotenv;
use log::{error, info}; use log::info;
use r2d2_mysql::MySqlConnectionManager; use r2d2_mysql::{
mysql::{Opts, OptsBuilder},
r2d2, MySqlConnectionManager,
};
use std::env; use std::env;
use std::io; use std::io;
@ -10,6 +13,7 @@ mod repositories;
mod routes; mod routes;
mod services; mod services;
use crate::routes::post_routes;
use crate::routes::user_routes; use crate::routes::user_routes;
#[actix_web::main] #[actix_web::main]
@ -18,45 +22,18 @@ async fn main() -> io::Result<()> {
dotenv().ok(); dotenv().ok();
info!("rust demo -_- 正在启动中..."); info!("rust demo -_- 正在启动中...");
let db_url = env::var("DB_MYSQL_URL").unwrap(); let url = env::var("DB_MYSQL_URL").expect("DB_MYSQL_URL must be set");
let db_port = env::var("DB_MYSQL_PORT").unwrap(); let opts = Opts::from_url(url.as_str()).expect("DB_MYSQL_URL is invalid");
let db_user = env::var("DB_MYSQL_USER").unwrap(); let builder = OptsBuilder::from_opts(opts);
let db_password = env::var("DB_MYSQL_PASSWORD").unwrap(); let manager = MySqlConnectionManager::new(builder);
let db_name = env::var("DB_MYSQL_NAME").unwrap(); let pool = r2d2::Pool::new(manager).expect("Failed to create pool");
let url = format!(
"mysql://{}:{}@{}:{}/{}",
db_user, db_password, db_url, db_port, db_name
);
let opts = match r2d2_mysql::mysql::Opts::from_url(&url) {
Ok(opts) => opts,
Err(e) => {
eprintln!("Failed to parse MySQL connection URL: {}", e);
std::process::exit(1);
}
};
let opts_builder = r2d2_mysql::mysql::OptsBuilder::from_opts(opts);
let manager = MySqlConnectionManager::new(opts_builder);
let pool = match r2d2::Pool::builder().max_size(15).build(manager) {
Ok(pool) => {
info!("Successfully created connection pool");
pool
}
Err(e) => {
error!("Failed to create connection pool: {}", e);
return Err(io::Error::new(
io::ErrorKind::Other,
"Failed to create connection pool",
));
}
};
let pool_data = web::Data::new(pool.clone()); let pool_data = web::Data::new(pool.clone());
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.app_data(pool_data.clone()) .app_data(pool_data.clone())
.configure(user_routes::register) .configure(user_routes::register)
.configure(post_routes::register)
}) })
.bind("127.0.0.1:8080")? .bind("127.0.0.1:8080")?
.run() .run()

View File

@ -1 +1,2 @@
pub mod user; pub mod user;
pub mod post;

10
src/models/post.rs Normal file
View File

@ -0,0 +1,10 @@
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Post {
pub id: Option<i32>,
pub title: Option<String>,
pub content: Option<String>,
pub link: Option<String>,
pub popularity: Option<String>,
pub created_time: Option<String>,
pub updated_time: Option<String>,
}

View File

@ -1 +1,2 @@
pub mod post_repository;
pub mod user_repository; pub mod user_repository;

View File

@ -0,0 +1,44 @@
use mysql::prelude::Queryable;
use mysql::{params, Error as MysqlError};
use r2d2::{Error as R2D2Error, Pool};
use r2d2_mysql::MySqlConnectionManager;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum PostRepositoryError {
#[error("mysql error: {0}")]
MySqlError(#[from] MysqlError),
#[error("Connection pool error: {0}")]
PoolError(#[from] R2D2Error),
}
pub struct PostRepository {
pool: Pool<MySqlConnectionManager>,
}
impl PostRepository {
pub fn new(pool: Pool<MySqlConnectionManager>) -> Self {
PostRepository { pool }
}
/**
*
*/
pub fn insert_post(
&self,
title: &str,
content: &str,
link: &str,
popularity: &str,
) -> Result<(), PostRepositoryError> {
let mut conn = self.pool.get().map_err(PostRepositoryError::PoolError)?;
let sql = "INSERT INTO posts (title, content, link, popularity) VALUES (:title, :content, :link, :popularity)";
conn.exec_drop(
sql,
params! {"title" => title, "content" => content, "link" => link, "popularity" => popularity},
)
.map_err(PostRepositoryError::MySqlError)?;
Ok(())
}
}

View File

@ -1 +1,2 @@
pub mod post_routes;
pub mod user_routes; pub mod user_routes;

67
src/routes/post_routes.rs Normal file
View File

@ -0,0 +1,67 @@
use crate::repositories::post_repository::PostRepository;
use crate::services::post_service;
use crate::{models::post::Post, services::post_service::PostServiceError};
use actix_web::{post, web, HttpResponse, Responder};
use r2d2_mysql::MySqlConnectionManager;
use serde::Serialize;
#[derive(Serialize)]
struct ApiResponse<T> {
status: String,
message: Option<T>,
error: Option<String>,
}
pub(crate) fn register(config: &mut web::ServiceConfig) {
config.service(insert_post);
}
#[derive(serde::Deserialize)]
struct PostFrom {
title: String,
content: String,
link: String,
popularity: i32,
}
#[post("/post/insert")]
async fn insert_post(
post_from: web::Form<PostFrom>,
pool: web::Data<r2d2::Pool<MySqlConnectionManager>>,
) -> impl Responder {
let post_repository = PostRepository::new(pool.get_ref().clone());
let post_service = post_service::PostService::new(post_repository);
let post = Post {
id: None,
title: Some(post_from.title.clone()),
content: Some(post_from.content.clone()),
link: Some(post_from.link.clone()),
popularity: Some(post_from.popularity.to_string()),
created_time: None,
updated_time: None,
};
match post_service.insert_post(&post) {
Ok(_) => HttpResponse::Ok().json(ApiResponse {
status: "success".to_string(),
message: Some("Post created successfully".to_string()),
error: None,
}),
Err(e) => {
// 根据错误类型返回不同的 HTTP 状态码
let response = if matches!(e, PostServiceError::MissingField(_)) {
HttpResponse::BadRequest().json(ApiResponse::<()> {
status: "error".to_string(),
message: None,
error: Some(e.to_string()),
})
} else {
HttpResponse::InternalServerError().json(ApiResponse::<()> {
status: "error".to_string(),
message: None,
error: Some(e.to_string()),
})
};
response
}
}
}

View File

@ -1 +1,2 @@
pub mod post_service;
pub mod user_service; pub mod user_service;

View File

@ -0,0 +1,64 @@
use std::time::Instant;
use crate::{
models::post::Post,
repositories::post_repository::{PostRepository, PostRepositoryError},
};
use log::info;
use mysql::*;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum PostServiceError {
#[error("数据库异常: {0}")]
MySqlError(#[from] PostRepositoryError),
#[error("缺少字段: {0}")]
MissingField(&'static str),
}
pub struct PostService {
repository: PostRepository,
}
impl PostService {
pub fn new(repository: PostRepository) -> Self {
PostService { repository }
}
/**
*
*/
pub fn insert_post(&self, post: &Post) -> Result<(), PostServiceError> {
let title = post
.title
.as_ref()
.ok_or(PostServiceError::MissingField("title"))?;
let content = post
.content
.as_ref()
.ok_or(PostServiceError::MissingField("content"))?;
let link = post
.link
.as_ref()
.ok_or(PostServiceError::MissingField("link"))?;
let popularity = post
.popularity
.as_ref()
.ok_or(PostServiceError::MissingField("popularity"))?;
let start = Instant::now();
self.repository.insert_post(
title,
content,
link,
popularity,
)?;
info!("insert_post elapsed: {:?}", start.elapsed());
Ok(())
}
}

View File

@ -1,8 +1,11 @@
use std::time::Instant;
use crate::{ use crate::{
models::user::User, models::user::User,
repositories::user_repository::{UserRepository, UserRepositoryError}, repositories::user_repository::{UserRepository, UserRepositoryError},
}; };
use bcrypt::{hash, DEFAULT_COST}; use bcrypt::{hash, DEFAULT_COST};
use log::info;
use mysql::*; use mysql::*;
use thiserror::Error; use thiserror::Error;
@ -51,8 +54,10 @@ impl UserService {
return Err(UserServiceError::UserAlreadyExists); return Err(UserServiceError::UserAlreadyExists);
} }
let start = Instant::now();
self.repository self.repository
.insert_user(&account, &name, &hash(password, DEFAULT_COST).unwrap())?; .insert_user(&account, &name, &hash(password, DEFAULT_COST).unwrap())?;
info!("insert_user elapsed: {:?}", start.elapsed());
Ok(()) Ok(())
} }
} }