diff --git a/Cargo.lock b/Cargo.lock index 29cfc78..9677956 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -194,6 +194,154 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" +[[package]] +name = "async-attributes" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3203e79f4dd9bdda415ed03cf14dae5a2bf775c683a00f94e9cd1faf0f596e5" +dependencies = [ + "quote", + "syn 1.0.109", +] + +[[package]] +name = "async-channel" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81953c529336010edd6d8e358f886d9581267795c61b19475b71314bffa46d35" +dependencies = [ + "concurrent-queue", + "event-listener 2.5.3", + "futures-core", +] + +[[package]] +name = "async-channel" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89b47800b0be77592da0afd425cc03468052844aff33b84e33cc696f64e77b6a" +dependencies = [ + "concurrent-queue", + "event-listener-strategy", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "async-executor" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7ebdfa2ebdab6b1760375fa7d6f382b9f486eac35fc994625a00e89280bdbb7" +dependencies = [ + "async-task", + "concurrent-queue", + "fastrand 2.1.0", + "futures-lite 2.3.0", + "slab", +] + +[[package]] +name = "async-global-executor" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05b1b633a2115cd122d73b955eadd9916c18c8f510ec9cd1686404c60ad1c29c" +dependencies = [ + "async-channel 2.3.1", + "async-executor", + "async-io 2.3.3", + "async-lock 3.4.0", + "blocking", + "futures-lite 2.3.0", + "once_cell", + "tokio", +] + +[[package]] +name = "async-io" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fc5b45d93ef0529756f812ca52e44c221b35341892d3dcc34132ac02f3dd2af" +dependencies = [ + "async-lock 2.8.0", + "autocfg", + "cfg-if", + "concurrent-queue", + "futures-lite 1.13.0", + "log", + "parking", + "polling 2.8.0", + "rustix 0.37.27", + "slab", + "socket2 0.4.10", + "waker-fn", +] + +[[package]] +name = "async-io" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d6baa8f0178795da0e71bc42c9e5d13261aac7ee549853162e66a241ba17964" +dependencies = [ + "async-lock 3.4.0", + "cfg-if", + "concurrent-queue", + "futures-io", + "futures-lite 2.3.0", + "parking", + "polling 3.7.2", + "rustix 0.38.34", + "slab", + "tracing", + "windows-sys 0.52.0", +] + +[[package]] +name = "async-lock" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "287272293e9d8c41773cec55e365490fe034813a2f172f502d6ddcf75b2f582b" +dependencies = [ + "event-listener 2.5.3", +] + +[[package]] +name = "async-lock" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff6e472cdea888a4bd64f342f09b3f50e1886d32afe8df3d663c01140b811b18" +dependencies = [ + "event-listener 5.3.1", + "event-listener-strategy", + "pin-project-lite", +] + +[[package]] +name = "async-std" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62565bb4402e926b29953c785397c6dc0391b7b446e45008b0049eb43cec6f5d" +dependencies = [ + "async-attributes", + "async-channel 1.9.0", + "async-global-executor", + "async-io 1.13.0", + "async-lock 2.8.0", + "crossbeam-utils", + "futures-channel", + "futures-core", + "futures-io", + "futures-lite 1.13.0", + "gloo-timers", + "kv-log-macro", + "log", + "memchr", + "once_cell", + "pin-project-lite", + "pin-utils", + "slab", + "wasm-bindgen-futures", +] + [[package]] name = "async-stream" version = "0.3.5" @@ -216,6 +364,12 @@ dependencies = [ "syn 2.0.72", ] +[[package]] +name = "async-task" +version = "4.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de" + [[package]] name = "async-trait" version = "0.1.81" @@ -337,6 +491,19 @@ dependencies = [ "generic-array", ] +[[package]] +name = "blocking" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "703f41c54fc768e63e091340b424302bb1c29ef4aa0c7f10fe849dfb114d29ea" +dependencies = [ + "async-channel 2.3.1", + "async-task", + "futures-io", + "futures-lite 2.3.0", + "piper", +] + [[package]] name = "borsh" version = "1.5.1" @@ -552,6 +719,15 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" +[[package]] +name = "concurrent-queue" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "const-oid" version = "0.9.6" @@ -748,6 +924,13 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "entity" +version = "0.1.0" +dependencies = [ + "sea-orm", +] + [[package]] name = "enum-ordinalize" version = "4.3.0" @@ -841,6 +1024,27 @@ version = "2.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" +[[package]] +name = "event-listener" +version = "5.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6032be9bd27023a771701cc49f9f053c751055f71efb2e0ae5c15809093675ba" +dependencies = [ + "concurrent-queue", + "parking", + "pin-project-lite", +] + +[[package]] +name = "event-listener-strategy" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f214dc438f977e6d4e3500aaa277f5ad94ca83fbbd9b1a15713ce2344ccc5a1" +dependencies = [ + "event-listener 5.3.1", + "pin-project-lite", +] + [[package]] name = "eyre" version = "0.6.12" @@ -851,6 +1055,15 @@ dependencies = [ "once_cell", ] +[[package]] +name = "fastrand" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be" +dependencies = [ + "instant", +] + [[package]] name = "fastrand" version = "2.1.0" @@ -972,6 +1185,34 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" +[[package]] +name = "futures-lite" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49a9d51ce47660b1e808d3c990b4709f2f415d928835a17dfd16991515c46bce" +dependencies = [ + "fastrand 1.9.0", + "futures-core", + "futures-io", + "memchr", + "parking", + "pin-project-lite", + "waker-fn", +] + +[[package]] +name = "futures-lite" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52527eb5074e35e9339c6b4e8d12600c7128b68fb25dcb9fa9dec18f7c25f3a5" +dependencies = [ + "fastrand 2.1.0", + "futures-core", + "futures-io", + "parking", + "pin-project-lite", +] + [[package]] name = "futures-macro" version = "0.3.30" @@ -1058,6 +1299,18 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" +[[package]] +name = "gloo-timers" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b995a66bb87bebce9a0f4a95aed01daca4872c050bfcb21653361c03bc35e5c" +dependencies = [ + "futures-channel", + "futures-core", + "js-sys", + "wasm-bindgen", +] + [[package]] name = "h2" version = "0.4.5" @@ -1150,6 +1403,12 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" +[[package]] +name = "hermit-abi" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" + [[package]] name = "hex" version = "0.4.3" @@ -1298,7 +1557,7 @@ dependencies = [ "http-body", "hyper", "pin-project-lite", - "socket2", + "socket2 0.5.7", "tokio", "tower", "tower-service", @@ -1375,12 +1634,32 @@ dependencies = [ "generic-array", ] +[[package]] +name = "instant" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222" +dependencies = [ + "cfg-if", +] + [[package]] name = "inventory" version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f958d3d68f4167080a18141e10381e7634563984a537f2a49a30fd8e53ac5767" +[[package]] +name = "io-lifetimes" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" +dependencies = [ + "hermit-abi 0.3.9", + "libc", + "windows-sys 0.48.0", +] + [[package]] name = "ipnet" version = "2.9.0" @@ -1441,6 +1720,15 @@ dependencies = [ "simple_asn1", ] +[[package]] +name = "kv-log-macro" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f" +dependencies = [ + "log", +] + [[package]] name = "lazy_static" version = "1.5.0" @@ -1473,6 +1761,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "linux-raw-sys" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" + [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -1494,6 +1788,9 @@ name = "log" version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" +dependencies = [ + "value-bag", +] [[package]] name = "lua-src" @@ -1539,6 +1836,14 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "migration" +version = "0.1.0" +dependencies = [ + "async-std", + "sea-orm-migration", +] + [[package]] name = "mime" version = "0.3.17" @@ -1576,7 +1881,7 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4569e456d394deccd22ce1c1913e6ea0e54519f577285001215d33557431afe4" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.9", "libc", "wasi", "windows-sys 0.52.0", @@ -1874,6 +2179,12 @@ version = "3.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" +[[package]] +name = "parking" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae" + [[package]] name = "parking_lot" version = "0.12.3" @@ -1971,6 +2282,17 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "piper" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae1d5c74c9876f070d3e8fd503d748c7d974c3e48da8f41350fa5222ef9b4391" +dependencies = [ + "atomic-waker", + "fastrand 2.1.0", + "futures-io", +] + [[package]] name = "pkcs1" version = "0.7.5" @@ -2089,6 +2411,37 @@ dependencies = [ "pnet_sys", ] +[[package]] +name = "polling" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b2d323e8ca7996b3e23126511a523f7e62924d93ecd5ae73b333815b0eb3dce" +dependencies = [ + "autocfg", + "bitflags 1.3.2", + "cfg-if", + "concurrent-queue", + "libc", + "log", + "pin-project-lite", + "windows-sys 0.48.0", +] + +[[package]] +name = "polling" +version = "3.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3ed00ed3fbf728b5816498ecd316d1716eecaced9c0c8d2c5a6740ca214985b" +dependencies = [ + "cfg-if", + "concurrent-queue", + "hermit-abi 0.4.0", + "pin-project-lite", + "rustix 0.38.34", + "tracing", + "windows-sys 0.52.0", +] + [[package]] name = "polyval" version = "0.6.2" @@ -2109,9 +2462,13 @@ checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" [[package]] name = "ppv-lite86" -version = "0.2.17" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +checksum = "2288c0e17cc8d342c712bb43a257a80ebffce59cdb33d5000d8348f3ec02528b" +dependencies = [ + "zerocopy", + "zerocopy-derive", +] [[package]] name = "proc-macro-crate" @@ -2236,12 +2593,21 @@ dependencies = [ [[package]] name = "realm" version = "0.1.0" +dependencies = [ + "realm-api", +] + +[[package]] +name = "realm-api" +version = "0.1.0" dependencies = [ "argon2", "chrono", "clap", "color-eyre", "const_format", + "entity", + "migration", "mlua", "salvo", "sea-orm", @@ -2496,6 +2862,20 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152" +[[package]] +name = "rustix" +version = "0.37.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea8ca367a3a01fe35e6943c400addf443c0f57670e6ec51196f71a4b8762dd2" +dependencies = [ + "bitflags 1.3.2", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys 0.3.8", + "windows-sys 0.48.0", +] + [[package]] name = "rustix" version = "0.38.34" @@ -2505,7 +2885,7 @@ dependencies = [ "bitflags 2.6.0", "errno", "libc", - "linux-raw-sys", + "linux-raw-sys 0.4.14", "windows-sys 0.52.0", ] @@ -2677,7 +3057,7 @@ version = "0.68.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ebbf0e49bb64696194b00ab6c368770b8c02d32ca65bd56ea661892098a2550" dependencies = [ - "fastrand", + "fastrand 2.1.0", "futures-util", "hyper", "hyper-rustls", @@ -3160,6 +3540,16 @@ version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" +[[package]] +name = "socket2" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7916fc008ca5542385b89a3d3ce689953c143e9304a9bf8beec1de48994c0d" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "socket2" version = "0.5.7" @@ -3238,7 +3628,7 @@ dependencies = [ "crc", "crossbeam-queue", "either", - "event-listener", + "event-listener 2.5.3", "futures-channel", "futures-core", "futures-intrusive", @@ -3538,8 +3928,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" dependencies = [ "cfg-if", - "fastrand", - "rustix", + "fastrand 2.1.0", + "rustix 0.38.34", "windows-sys 0.52.0", ] @@ -3632,7 +4022,7 @@ dependencies = [ "parking_lot", "pin-project-lite", "signal-hook-registry", - "socket2", + "socket2 0.5.7", "tokio-macros", "windows-sys 0.52.0", ] @@ -3707,9 +4097,9 @@ dependencies = [ [[package]] name = "toml_datetime" -version = "0.6.7" +version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8fb9f64314842840f1d940ac544da178732128f1c78c21772e876579e0da1db" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" [[package]] name = "toml_edit" @@ -3984,6 +4374,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "value-bag" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a84c137d37ab0142f0f2ddfe332651fdbf252e7b7dbb4e67b6c1f1b2e925101" + [[package]] name = "vcpkg" version = "0.2.15" @@ -3996,6 +4392,12 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "waker-fn" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "317211a0dc0ceedd78fb2ca9a44aed3d7b9b26f81870d485c07122b4350673b7" + [[package]] name = "walkdir" version = "2.5.0" @@ -4128,13 +4530,13 @@ dependencies = [ [[package]] name = "which" -version = "6.0.1" +version = "6.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8211e4f58a2b2805adfbefbc07bab82958fc91e3836339b1ab7ae32465dce0d7" +checksum = "3d9c5ed668ee1f17edb3b627225343d210006a90bb1e3745ce1f30b1fb115075" dependencies = [ "either", "home", - "rustix", + "rustix 0.38.34", "winsafe", ] @@ -4385,6 +4787,7 @@ version = "0.7.35" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" dependencies = [ + "byteorder", "zerocopy-derive", ] diff --git a/Cargo.toml b/Cargo.toml index a7e21f6..4fae58f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,50 +3,15 @@ name = "realm" version = "0.1.0" edition = "2021" -[dependencies] -tokio = { version = "1", features = ["full"] } -tracing = "0.1" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } -mlua = { version = "0.9", features = ["lua54", "vendored", "serialize"] } -serde = { version = "1.0", features = ["derive"] } -serde_json = "1" -const_format = "0.2" -clap = { version = "4", features = ["derive"] } -chrono = { version = "0.4", features = ["serde"] } -sonyflake = { version = "0.2.0" } -argon2 = "0.5" -color-eyre = "0.6" - -[dependencies.salvo] -version = "0.68" -features = [ - "oapi", - "eyre", - "websocket", - "logging", - "basic-auth", - "compression", - "affix", -] - -[dependencies.sea-orm] -version = "1.0.0-rc.7" -features = [ - "runtime-tokio", - "sqlx-postgres", - "debug-print", - "macros", - "with-uuid", - "with-json", - "with-chrono", +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[workspace] +members = [ + ".", + "api", + # "service", + "entity", + "migration" ] -[dependencies.sea-orm-migration] -version = "1.0.0-rc.7" -features = [ - # Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI. - # View the list of supported features at https://www.sea-ql.org/SeaORM/docs/install-and-config/database-and-async-runtime. - # e.g. - "runtime-tokio", # `ASYNC_RUNTIME` feature - "sqlx-postgres", # `DATABASE_DRIVER` feature -] +[dependencies] +realm-api = { path = "api" } diff --git a/api/Cargo.toml b/api/Cargo.toml new file mode 100644 index 0000000..477ec81 --- /dev/null +++ b/api/Cargo.toml @@ -0,0 +1,55 @@ +[package] +name = "realm-api" +version = "0.1.0" +edition = "2021" + +[dependencies] +tokio = { version = "1", features = ["full"] } +tracing = "0.1" +tracing-subscriber = { version = "0.3", features = ["env-filter"] } +mlua = { version = "0.9", features = ["lua54", "vendored", "serialize"] } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1" +const_format = "0.2" +clap = { version = "4", features = ["derive"] } +chrono = { version = "0.4", features = ["serde"] } +sonyflake = { version = "0.2.0" } +argon2 = "0.5" +color-eyre = "0.6" + +entity = { path = "../entity" } +migration = { path = "../migration" } + +[dependencies.salvo] +version = "0.68" +features = [ + "oapi", + "eyre", + "websocket", + "logging", + "basic-auth", + "compression", + "affix", +] + +[dependencies.sea-orm] +version = "1.0.0-rc.7" +features = [ + "runtime-tokio", + "sqlx-postgres", + "debug-print", + "macros", + "with-uuid", + "with-json", + "with-chrono", +] + +[dependencies.sea-orm-migration] +version = "1.0.0-rc.7" +features = [ + # Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI. + # View the list of supported features at https://www.sea-ql.org/SeaORM/docs/install-and-config/database-and-async-runtime. + # e.g. + "runtime-tokio", # `ASYNC_RUNTIME` feature + "sqlx-postgres", # `DATABASE_DRIVER` feature +] diff --git a/src/config.rs b/api/src/config.rs similarity index 100% rename from src/config.rs rename to api/src/config.rs diff --git a/src/config/config.lua b/api/src/config/config.lua similarity index 100% rename from src/config/config.lua rename to api/src/config/config.lua diff --git a/src/db.rs b/api/src/db.rs similarity index 82% rename from src/db.rs rename to api/src/db.rs index 9105bf7..883def9 100644 --- a/src/db.rs +++ b/api/src/db.rs @@ -1,6 +1,7 @@ use sea_orm::{ConnectOptions, Database, DatabaseConnection, DbErr}; use color_eyre::Result; use tracing::info; +use migration::{Migrator, MigratorTrait}; use crate::config::Config; @@ -24,5 +25,8 @@ pub async fn init(config: &Config) -> Result { opt.sqlx_logging(true); info!("Connecting to database at {}", db_url); - Database::connect(opt).await + let connection = Database::connect(opt).await?; + + Migrator::up(&connection, None).await?; + Ok(connection) } diff --git a/src/error.rs b/api/src/error.rs similarity index 100% rename from src/error.rs rename to api/src/error.rs diff --git a/src/handler/mod.rs b/api/src/handler/mod.rs similarity index 100% rename from src/handler/mod.rs rename to api/src/handler/mod.rs diff --git a/src/handler/server_handler.rs b/api/src/handler/server_handler.rs similarity index 100% rename from src/handler/server_handler.rs rename to api/src/handler/server_handler.rs diff --git a/src/handler/user_handler.rs b/api/src/handler/user_handler.rs similarity index 100% rename from src/handler/user_handler.rs rename to api/src/handler/user_handler.rs diff --git a/api/src/lib.rs b/api/src/lib.rs new file mode 100644 index 0000000..86c1472 --- /dev/null +++ b/api/src/lib.rs @@ -0,0 +1,84 @@ +pub mod config; +pub mod db; +pub mod error; +pub mod handler; +pub mod middleware; +pub mod models; +pub mod router; +pub mod state; +pub mod utils; + +use crate::router::make_route; +use color_eyre::Result; +use salvo::prelude::*; +use tokio::signal; +use tracing::info; +use tracing_subscriber::EnvFilter; + +#[tokio::main] +pub async fn main() -> Result<()> { + let filter = EnvFilter::from_default_env(); + tracing_subscriber::fmt().with_env_filter(filter).with_test_writer().init(); + color_eyre::install()?; + + + + let config = config::get_config().await.unwrap_or_else(|_| { + info!("failed to read config file, using default instead"); + config::Config::default() + }); + + let acceptor = TcpListener::new(config.host.clone() + ":" + &config.port.to_string()) + .bind() + .await; + + let router = Router::with_path("api").push(make_route(&config).await); + + // TODO: http3 + let server = Server::new(acceptor); + let handle = server.handle(); + + // graceful shutdown + tokio::spawn(async move { + signal::ctrl_c().await.expect("failed to listen for event"); + handle.stop_graceful(None); + }); + + server.serve(router).await; + Ok(()) +} + +// Break this test into smaller routes. +#[cfg(test)] +mod tests { + use salvo::prelude::*; + use salvo::test::{ResponseExt, TestClient}; + + use crate::config::Config; + + #[tokio::test] + async fn test_basic_auth() { + let test_config = Config::default(); + let service = Service::new(super::make_route(&test_config).await); + + let url = format!("http://{}:{}/", test_config.host, test_config.port); + + let content = TestClient::get(url.clone() + "hello_admin") + .basic_auth("admin", Some("admin")) + .send(&service) + .await + .take_string() + .await + .unwrap(); + assert!(content.contains("Admin")); + + let content = TestClient::get(url.clone() + "hello_admin") + .basic_auth("admin", Some("admin2")) + .send(&service) + .await + .take_string() + .await + .unwrap(); + assert!(content.contains("Unauthorized")); + } +} diff --git a/src/middleware.rs b/api/src/middleware.rs similarity index 100% rename from src/middleware.rs rename to api/src/middleware.rs diff --git a/src/middleware/basic_auth.rs b/api/src/middleware/basic_auth.rs similarity index 100% rename from src/middleware/basic_auth.rs rename to api/src/middleware/basic_auth.rs diff --git a/src/models/message.rs b/api/src/models/message.rs similarity index 100% rename from src/models/message.rs rename to api/src/models/message.rs diff --git a/src/models/mod.rs b/api/src/models/mod.rs similarity index 100% rename from src/models/mod.rs rename to api/src/models/mod.rs diff --git a/src/models/server.rs b/api/src/models/server.rs similarity index 100% rename from src/models/server.rs rename to api/src/models/server.rs diff --git a/src/models/user.rs b/api/src/models/user.rs similarity index 100% rename from src/models/user.rs rename to api/src/models/user.rs diff --git a/src/router.rs b/api/src/router.rs similarity index 100% rename from src/router.rs rename to api/src/router.rs diff --git a/src/state.rs b/api/src/state.rs similarity index 100% rename from src/state.rs rename to api/src/state.rs diff --git a/src/utils.rs b/api/src/utils.rs similarity index 100% rename from src/utils.rs rename to api/src/utils.rs diff --git a/entity/Cargo.toml b/entity/Cargo.toml new file mode 100644 index 0000000..13c93d7 --- /dev/null +++ b/entity/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "entity" +version = "0.1.0" +edition = "2021" + +[dependencies] +sea-orm = { version = "1.0.0-rc.7" } diff --git a/entity/src/lib.rs b/entity/src/lib.rs new file mode 100644 index 0000000..b93cf3f --- /dev/null +++ b/entity/src/lib.rs @@ -0,0 +1,14 @@ +pub fn add(left: u64, right: u64) -> u64 { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} diff --git a/migration/Cargo.toml b/migration/Cargo.toml new file mode 100644 index 0000000..fe207db --- /dev/null +++ b/migration/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "migration" +version = "0.1.0" +edition = "2021" +publish = false + +[lib] +name = "migration" +path = "src/lib.rs" + +[dependencies] +async-std = { version = "1", features = ["attributes", "tokio1"] } + +[dependencies.sea-orm-migration] +version = "1.0.0-rc.7" +features = [ + # Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI. + # View the list of supported features at https://www.sea-ql.org/SeaORM/docs/install-and-config/database-and-async-runtime. + # e.g. + "runtime-tokio", # `ASYNC_RUNTIME` feature + "sqlx-postgres", # `DATABASE_DRIVER` feature +] diff --git a/migration/README.md b/migration/README.md new file mode 100644 index 0000000..3b438d8 --- /dev/null +++ b/migration/README.md @@ -0,0 +1,41 @@ +# Running Migrator CLI + +- Generate a new migration file + ```sh + cargo run -- generate MIGRATION_NAME + ``` +- Apply all pending migrations + ```sh + cargo run + ``` + ```sh + cargo run -- up + ``` +- Apply first 10 pending migrations + ```sh + cargo run -- up -n 10 + ``` +- Rollback last applied migrations + ```sh + cargo run -- down + ``` +- Rollback last 10 applied migrations + ```sh + cargo run -- down -n 10 + ``` +- Drop all tables from the database, then reapply all migrations + ```sh + cargo run -- fresh + ``` +- Rollback all applied migrations, then reapply all migrations + ```sh + cargo run -- refresh + ``` +- Rollback all applied migrations + ```sh + cargo run -- reset + ``` +- Check the status of all migrations + ```sh + cargo run -- status + ``` diff --git a/migration/src/lib.rs b/migration/src/lib.rs new file mode 100644 index 0000000..2c605af --- /dev/null +++ b/migration/src/lib.rs @@ -0,0 +1,12 @@ +pub use sea_orm_migration::prelude::*; + +mod m20220101_000001_create_table; + +pub struct Migrator; + +#[async_trait::async_trait] +impl MigratorTrait for Migrator { + fn migrations() -> Vec> { + vec![Box::new(m20220101_000001_create_table::Migration)] + } +} diff --git a/migration/src/m20220101_000001_create_table.rs b/migration/src/m20220101_000001_create_table.rs new file mode 100644 index 0000000..c8f2a5d --- /dev/null +++ b/migration/src/m20220101_000001_create_table.rs @@ -0,0 +1,47 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + // Replace the sample below with your own migration scripts + todo!(); + + manager + .create_table( + Table::create() + .table(Post::Table) + .if_not_exists() + .col( + ColumnDef::new(Post::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(Post::Title).string().not_null()) + .col(ColumnDef::new(Post::Text).string().not_null()) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + // Replace the sample below with your own migration scripts + todo!(); + + manager + .drop_table(Table::drop().table(Post::Table).to_owned()) + .await + } +} + +#[derive(DeriveIden)] +enum Post { + Table, + Id, + Title, + Text, +} diff --git a/migration/src/main.rs b/migration/src/main.rs new file mode 100644 index 0000000..c6b6e48 --- /dev/null +++ b/migration/src/main.rs @@ -0,0 +1,6 @@ +use sea_orm_migration::prelude::*; + +#[async_std::main] +async fn main() { + cli::run_cli(migration::Migrator).await; +} diff --git a/src/main.rs b/src/main.rs index a52d6bf..0770582 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,84 +1,3 @@ -pub mod config; -pub mod db; -pub mod error; -pub mod handler; -pub mod middleware; -pub mod models; -pub mod router; -pub mod state; -pub mod utils; - -use crate::router::make_route; -use color_eyre::Result; -use salvo::prelude::*; -use tokio::signal; -use tracing::info; -use tracing_subscriber::EnvFilter; - -#[tokio::main] -async fn main() -> Result<()> { - let filter = EnvFilter::from_default_env(); - tracing_subscriber::fmt().with_env_filter(filter).with_test_writer().init(); - color_eyre::install()?; - // std::env::set_var("RUST_LOG", "trace"); - // tracing_subscriber::fmt::init(); - - let config = config::get_config().await.unwrap_or_else(|_| { - info!("failed to read config file, using default instead"); - config::Config::default() - }); - - let acceptor = TcpListener::new(config.host.clone() + ":" + &config.port.to_string()) - .bind() - .await; - - let router = Router::with_path("api").push(make_route(&config).await); - - // TODO: http3 - let server = Server::new(acceptor); - let handle = server.handle(); - - // graceful shutdown - tokio::spawn(async move { - signal::ctrl_c().await.expect("failed to listen for event"); - handle.stop_graceful(None); - }); - - server.serve(router).await; - Ok(()) -} - -// Break this test into smaller routes. -#[cfg(test)] -mod tests { - use salvo::prelude::*; - use salvo::test::{ResponseExt, TestClient}; - - use crate::config::Config; - - #[tokio::test] - async fn test_basic_auth() { - let test_config = Config::default(); - let service = Service::new(super::make_route(&test_config).await); - - let url = format!("http://{}:{}/", test_config.host, test_config.port); - - let content = TestClient::get(url.clone() + "hello_admin") - .basic_auth("admin", Some("admin")) - .send(&service) - .await - .take_string() - .await - .unwrap(); - assert!(content.contains("Admin")); - - let content = TestClient::get(url.clone() + "hello_admin") - .basic_auth("admin", Some("admin2")) - .send(&service) - .await - .take_string() - .await - .unwrap(); - assert!(content.contains("Unauthorized")); - } +fn main() { + realm_api::main().unwrap(); }