From 9b9961e350cad6a4d4a7873b7b71d29e81e4264c Mon Sep 17 00:00:00 2001 From: thuan2172001 Date: Thu, 28 Mar 2024 23:01:06 +0700 Subject: [PATCH] fix: structure --- Cargo.lock | 215 +++++++++--------- Cargo.toml | 2 +- src/adapter/Cargo.toml | 16 +- src/adapter/src/repositories/grpc/config.rs | 15 -- .../repositories/grpc/gpt_answer_client.rs | 85 +++++++ src/adapter/src/repositories/grpc/mod.rs | 3 +- .../repositories/grpc/models/gpt_answer.rs | 40 ---- .../repositories/postgres/models/question.rs | 9 +- .../src/repositories/postgres/question_db.rs | 18 +- .../src/repositories/repository_test.rs | 2 +- src/common/Cargo.toml | 32 ++- src/{grpc => common}/build.rs | 0 src/{grpc => common}/proto/gpt_answer.proto | 0 src/common/src/grpc/gpt_answer.rs | 9 + .../grpc/models => common/src/grpc}/mod.rs | 0 src/common/src/lib.rs | 1 + src/core/Cargo.toml | 4 +- src/core/src/common/errors.rs | 10 +- src/core/src/entities/pagination_entity.rs | 44 +--- src/core/src/entities/question_filter.rs | 49 ++-- src/core/src/ports/gpt_answer.rs | 8 + src/core/src/ports/mod.rs | 1 + src/core/src/ports/question.rs | 4 +- .../Cargo.toml | 32 +-- .../build.rs | 0 .../config/00-default.toml | 4 +- .../src/controllers/gpt_answer.rs | 49 ++++ .../src/controllers}/mod.rs | 0 .../src/lib.rs | 0 .../src/main.rs | 24 +- .../src/options.rs | 23 +- src/grpc/Cargo.toml | 38 ---- src/grpc/src/interfaces/gpt_answer.rs | 3 - src/grpc/src/lib.rs | 1 - src/grpc_server/src/controllers/gpt_answer.rs | 52 ----- src/grpc_server/src/controllers/mod.rs | 1 - src/public/Cargo.toml | 39 ++-- src/public/config/00-default.toml | 3 +- src/public/src/controllers/question.rs | 52 ++--- src/public/src/errors.rs | 3 +- src/public/src/main.rs | 18 +- src/public/src/options.rs | 19 +- src/public/src/router.rs | 33 +-- src/public/tests/questions_router_test.rs | 15 +- 44 files changed, 472 insertions(+), 504 deletions(-) delete mode 100644 src/adapter/src/repositories/grpc/config.rs create mode 100644 src/adapter/src/repositories/grpc/gpt_answer_client.rs delete mode 100644 src/adapter/src/repositories/grpc/models/gpt_answer.rs rename src/{grpc => common}/build.rs (100%) rename src/{grpc => common}/proto/gpt_answer.proto (100%) create mode 100644 src/common/src/grpc/gpt_answer.rs rename src/{adapter/src/repositories/grpc/models => common/src/grpc}/mod.rs (100%) create mode 100644 src/core/src/ports/gpt_answer.rs rename src/{grpc_server => gpt_answer_server}/Cargo.toml (76%) rename src/{grpc_server => gpt_answer_server}/build.rs (100%) rename src/{grpc_server => gpt_answer_server}/config/00-default.toml (60%) create mode 100644 src/gpt_answer_server/src/controllers/gpt_answer.rs rename src/{grpc/src/interfaces => gpt_answer_server/src/controllers}/mod.rs (100%) rename src/{grpc_server => gpt_answer_server}/src/lib.rs (100%) rename src/{grpc_server => gpt_answer_server}/src/main.rs (63%) rename src/{grpc_server => gpt_answer_server}/src/options.rs (50%) delete mode 100644 src/grpc/Cargo.toml delete mode 100644 src/grpc/src/interfaces/gpt_answer.rs delete mode 100644 src/grpc/src/lib.rs delete mode 100644 src/grpc_server/src/controllers/gpt_answer.rs delete mode 100644 src/grpc_server/src/controllers/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 41b2555..43e0b85 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,10 +8,10 @@ version = "0.0.1" dependencies = [ "anyhow", "async-trait", + "common", "deadpool-diesel", "diesel", "diesel_migrations", - "grpc_interface", "rust_core", "serde", "testcontainers-modules", @@ -49,9 +49,9 @@ dependencies = [ [[package]] name = "aho-corasick" -version = "1.1.2" +version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" dependencies = [ "memchr", ] @@ -129,25 +129,25 @@ checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.57", ] [[package]] name = "async-trait" -version = "0.1.77" +version = "0.1.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9" +checksum = "a507401cad91ec6a857ed5513a2073c82a9b9048762b885bb98655b306964681" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.57", ] [[package]] name = "autocfg" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80" [[package]] name = "axum" @@ -196,9 +196,9 @@ dependencies = [ [[package]] name = "backtrace" -version = "0.3.69" +version = "0.3.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837" +checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d" dependencies = [ "addr2line", "cc", @@ -223,9 +223,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.4.2" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" dependencies = [ "serde", ] @@ -263,9 +263,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.5.0" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" +checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" [[package]] name = "cc" @@ -281,9 +281,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "4.5.2" +version = "4.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b230ab84b0ffdf890d5a10abdbc8b83ae1c4918275daea1ab8801f71536b2651" +checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" dependencies = [ "clap_builder", "clap_derive", @@ -303,14 +303,14 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.0" +version = "4.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "307bc0538d5f0f83b8248db3087aa92fe504e4691294d0c96c0eabc33f47ba47" +checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" dependencies = [ - "heck", + "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.57", ] [[package]] @@ -330,7 +330,6 @@ dependencies = [ "deadpool-diesel", "diesel", "diesel_migrations", - "grpc_interface", "openssl", "opentelemetry", "rand", @@ -361,7 +360,10 @@ dependencies = [ "opentelemetry-otlp", "opentelemetry-semantic-conventions", "opentelemetry_sdk", + "prost", "serde", + "tonic", + "tonic-build", "tracing", "tracing-bunyan-formatter", "tracing-opentelemetry", @@ -384,7 +386,7 @@ dependencies = [ "rust-ini", "serde", "serde_json", - "toml 0.8.11", + "toml 0.8.12", "yaml-rust", ] @@ -551,11 +553,11 @@ dependencies = [ [[package]] name = "diesel" -version = "2.1.4" +version = "2.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62c6fcf842f17f8c78ecf7c81d75c5ce84436b41ee07e03f490fbb5f5a8731d8" +checksum = "03fc05c17098f21b89bc7d98fe1dd3cce2c11c2ad8e145f2a44fe08ed28eb559" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", "byteorder", "diesel_derives", "itoa", @@ -565,14 +567,14 @@ dependencies = [ [[package]] name = "diesel_derives" -version = "2.1.2" +version = "2.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef8337737574f55a468005a83499da720f20c65586241ffea339db9ecdfd2b44" +checksum = "5d02eecb814ae714ffe61ddc2db2dd03e6c49a42e269b5001355500d431cce0c" dependencies = [ "diesel_table_macro_syntax", "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.57", ] [[package]] @@ -592,7 +594,7 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc5557efc453706fed5e4fa85006fe9817c224c3f480a34c7e5959fd700921c5" dependencies = [ - "syn 2.0.52", + "syn 2.0.57", ] [[package]] @@ -648,9 +650,9 @@ dependencies = [ [[package]] name = "fastrand" -version = "2.0.1" +version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" +checksum = "658bd65b1cf4c852a3cc96f18a8ce7b5640f6b703f905c7d74532294c2a63984" [[package]] name = "fixedbitset" @@ -744,7 +746,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.57", ] [[package]] @@ -821,22 +823,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" [[package]] -name = "grpc_interface" -version = "0.0.1" -dependencies = [ - "glob", - "prost", - "tonic", - "tonic-build", -] - -[[package]] -name = "grpc_server" +name = "gpt_answer_server" version = "0.0.1" dependencies = [ "clap", "common", - "grpc_interface", "opentelemetry", "prost", "readonly", @@ -851,9 +842,9 @@ dependencies = [ [[package]] name = "h2" -version = "0.3.24" +version = "0.3.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb2c4422095b67ee78da96fbb51a4cc413b3b25883c7717ff7ca1ab31022c9c9" +checksum = "4fbd2820c5e49886948654ab546d0688ff24530286bdcf8fca3cefb16d4618eb" dependencies = [ "bytes", "fnv", @@ -861,7 +852,7 @@ dependencies = [ "futures-sink", "futures-util", "http", - "indexmap 2.2.5", + "indexmap 2.2.6", "slab", "tokio", "tokio-util", @@ -916,6 +907,12 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hermit-abi" version = "0.3.9" @@ -1044,9 +1041,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.2.5" +version = "2.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b0b929d511467233429c45a44ac1dcaa21ba0f5ba11e4879e6ed28ddb4f9df4" +checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" dependencies = [ "equivalent", "hashbrown 0.14.3", @@ -1063,9 +1060,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.10" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "js-sys" @@ -1144,9 +1141,9 @@ checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94" [[package]] name = "memchr" -version = "2.7.1" +version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" +checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" [[package]] name = "migrations_internals" @@ -1301,7 +1298,7 @@ version = "0.10.64" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95a0481286a310808298130d22dd1fef0fa571e05a8f44ec801801e84b216b1f" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", "cfg-if", "foreign-types", "libc", @@ -1318,14 +1315,14 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.57", ] [[package]] name = "openssl-sys" -version = "0.9.101" +version = "0.9.102" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dda2b0f344e78efc2facf7d195d098df0dd72151b26ab98da807afc26c198dff" +checksum = "c597637d56fbc83893a35eb0dd04b2b8e7a50c91e64e9493e398b5df4fb45fa2" dependencies = [ "cc", "libc", @@ -1498,7 +1495,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.57", ] [[package]] @@ -1519,7 +1516,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9" dependencies = [ "fixedbitset", - "indexmap 2.2.5", + "indexmap 2.2.6", ] [[package]] @@ -1539,14 +1536,14 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.57", ] [[package]] name = "pin-project-lite" -version = "0.2.13" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" [[package]] name = "pin-utils" @@ -1583,12 +1580,12 @@ dependencies = [ [[package]] name = "prettyplease" -version = "0.2.16" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a41cf62165e97c7f814d2221421dbb9afcbcdb0a88068e5ea206e19951c2cbb5" +checksum = "8d3928fb5db768cb86f891ff014f0144589297e3c6a1aba6ed7cecfdace270c7" dependencies = [ "proc-macro2", - "syn 2.0.52", + "syn 2.0.57", ] [[package]] @@ -1617,7 +1614,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c55e02e35260070b6f716a2423c2ff1c3bb1642ddca6f99e1f26d06268a0e2d2" dependencies = [ "bytes", - "heck", + "heck 0.4.1", "itertools", "log", "multimap", @@ -1627,7 +1624,7 @@ dependencies = [ "prost", "prost-types", "regex", - "syn 2.0.52", + "syn 2.0.57", "tempfile", "which", ] @@ -1642,7 +1639,7 @@ dependencies = [ "itertools", "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.57", ] [[package]] @@ -1701,7 +1698,7 @@ checksum = "a25d631e41bfb5fdcde1d4e2215f62f7f0afa3ff11e26563765bd6ea1d229aeb" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.57", ] [[package]] @@ -1715,14 +1712,14 @@ dependencies = [ [[package]] name = "regex" -version = "1.10.3" +version = "1.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" +checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" dependencies = [ "aho-corasick", "memchr", "regex-automata 0.4.6", - "regex-syntax 0.8.2", + "regex-syntax 0.8.3", ] [[package]] @@ -1742,7 +1739,7 @@ checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" dependencies = [ "aho-corasick", "memchr", - "regex-syntax 0.8.2", + "regex-syntax 0.8.3", ] [[package]] @@ -1753,9 +1750,9 @@ checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" [[package]] name = "regex-syntax" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" +checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" [[package]] name = "ron" @@ -1764,7 +1761,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b91f7eff05f748767f183df4320a63d6936e9c6107d97c9e6bdd9784f4289c94" dependencies = [ "base64", - "bitflags 2.4.2", + "bitflags 2.5.0", "serde", "serde_derive", ] @@ -1797,11 +1794,11 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] name = "rustix" -version = "0.38.31" +version = "0.38.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949" +checksum = "65e04861e65f21776e67888bfbea442b3642beaa0138fdb1dd7a84a52dffdb89" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", "errno", "libc", "linux-raw-sys", @@ -1858,14 +1855,14 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.57", ] [[package]] name = "serde_json" -version = "1.0.114" +version = "1.0.115" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0" +checksum = "12dc5c46daa8e9fdf4f5e71b6cf9a53f2487da0e86e55808e2d35539666497dd" dependencies = [ "itoa", "ryu", @@ -1966,9 +1963,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.13.1" +version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "socket2" @@ -2017,9 +2014,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.52" +version = "2.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07" +checksum = "11a6ae1e52eb25aab8f3fb9fca13be982a373b8f1157ca14b897a825ba4a2d35" dependencies = [ "proc-macro2", "quote", @@ -2063,9 +2060,9 @@ dependencies = [ [[package]] name = "testcontainers-modules" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d0334776e1e8ee7c504a922c5236daf865ffe413aa630d84ae91dcce0b10bc3" +checksum = "204d1c7516bfdc8a01bb85d3e30145e5bbeb2351812e5e8aa6971769109b45b5" dependencies = [ "testcontainers", ] @@ -2087,7 +2084,7 @@ checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.57", ] [[package]] @@ -2157,9 +2154,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.36.0" +version = "1.37.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61285f6515fa018fb2d1e46eb21223fff441ee8db5d0f1435e8ab4f5cdb80931" +checksum = "1adbebffeca75fcfd058afa480fb6c0b81e165a0323f9c9d39c9697e37c46787" dependencies = [ "backtrace", "bytes", @@ -2192,14 +2189,14 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.57", ] [[package]] name = "tokio-stream" -version = "0.1.14" +version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "397c988d37662c7dda6d2208364a706264bf3d6138b11d436cbac0ad38832842" +checksum = "267ac89e0bec6e691e5813911606935d77c476ff49024f98abcea3e7b15e37af" dependencies = [ "futures-core", "pin-project-lite", @@ -2246,14 +2243,14 @@ dependencies = [ [[package]] name = "toml" -version = "0.8.11" +version = "0.8.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af06656561d28735e9c1cd63dfd57132c8155426aa6af24f36a00a351f88c48e" +checksum = "e9dd1545e8208b4a5af1aa9bbd0b4cf7e9ea08fabc5d0a5c67fcaafa17433aa3" dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit 0.22.7", + "toml_edit 0.22.9", ] [[package]] @@ -2271,7 +2268,7 @@ version = "0.19.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" dependencies = [ - "indexmap 2.2.5", + "indexmap 2.2.6", "serde", "serde_spanned", "toml_datetime", @@ -2280,11 +2277,11 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.7" +version = "0.22.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18769cd1cec395d70860ceb4d932812a0b4d06b1a4bb336745a4d21b9496e992" +checksum = "8e40bb779c5187258fd7aad0eb68cb8706a0a81fa712fbea808ab43c4b8374c4" dependencies = [ - "indexmap 2.2.5", + "indexmap 2.2.6", "serde", "serde_spanned", "toml_datetime", @@ -2328,7 +2325,7 @@ dependencies = [ "proc-macro2", "prost-build", "quote", - "syn 2.0.52", + "syn 2.0.57", ] [[package]] @@ -2383,7 +2380,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.57", ] [[package]] @@ -2576,9 +2573,9 @@ checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" [[package]] name = "uuid" -version = "1.7.0" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f00cc9702ca12d3c81455259621e676d0f7251cec66a21e98fe2e9a37db93b2a" +checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0" [[package]] name = "valuable" @@ -2665,7 +2662,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.57", "wasm-bindgen-shared", ] @@ -2687,7 +2684,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.57", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -2918,5 +2915,5 @@ checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.57", ] diff --git a/Cargo.toml b/Cargo.toml index 25646c6..e9e3db1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,3 @@ [workspace] -members = ["src/public", "src/grpc_server"] +members = ["src/public", "src/gpt_answer_server"] resolver = "2" diff --git a/src/adapter/Cargo.toml b/src/adapter/Cargo.toml index d04f690..4996b2e 100644 --- a/src/adapter/Cargo.toml +++ b/src/adapter/Cargo.toml @@ -12,8 +12,14 @@ autoexamples = true autotests = true autobenches = true -[dependencies] -diesel_migrations = "2.1.0" +[dependencies.rust_core] +path = "../core" + +[dependencies.common] +path = "../common" + +[dependencies.diesel_migrations] +version = "2.1.0" [dependencies.async-trait] version = "0.1.77" @@ -26,9 +32,6 @@ features = ["postgres", "serde"] version = "2.1.4" features = ["postgres", "postgres_backend", "uuid"] -[dependencies.rust_core] -path = "../core" - [dependencies.serde] version = "1.0" features = ["derive"] @@ -46,6 +49,3 @@ version = "1.0.80" [dependencies.tonic] version = "0.11.0" - -[dependencies.grpc_interface] -path = "../grpc" diff --git a/src/adapter/src/repositories/grpc/config.rs b/src/adapter/src/repositories/grpc/config.rs deleted file mode 100644 index c078b18..0000000 --- a/src/adapter/src/repositories/grpc/config.rs +++ /dev/null @@ -1,15 +0,0 @@ -use serde::Deserialize; - -/// Represents servers configuration options. -#[derive(Deserialize, Debug)] -pub struct GrpcServers { - /// Configuration for using in-memory database. - pub gpt_answer_service: Option, -} - -/// Represents service server configuration. -#[derive(Debug, Deserialize, Clone)] -pub struct ServiceServer { - /// URL for the server. - pub url: String, -} diff --git a/src/adapter/src/repositories/grpc/gpt_answer_client.rs b/src/adapter/src/repositories/grpc/gpt_answer_client.rs new file mode 100644 index 0000000..1b4cc20 --- /dev/null +++ b/src/adapter/src/repositories/grpc/gpt_answer_client.rs @@ -0,0 +1,85 @@ +use tonic::{async_trait, transport::Channel}; + +use common::grpc::gpt_answer::gpt_answer::{ + gpt_answer_service_client::GptAnswerServiceClient, GetAnswerPayload, +}; +use rust_core::{common::errors::CoreError, ports::gpt_answer::GptAnswerPort}; + +/// gRPC client for interacting with a GPT (Generative Pre-trained Transformer) answer service. +/// +/// This struct represents a client for making gRPC calls to a GPT answer service. It provides +/// methods for connecting to the service, sending a question, and receiving an answer. +#[derive(Clone)] +pub struct GptAnswerClient { + client: GptAnswerServiceClient, +} + +impl GptAnswerClient { + /// Creates a new `GptAnswerClient` instance with the provided gRPC channel. + /// + /// # Arguments + /// + /// * `channel`: A `Channel` representing the gRPC communication channel. + /// + /// # Returns + /// + /// Returns a new instance of `GptAnswerClient`. + fn new(channel: Channel) -> Self { + let client = GptAnswerServiceClient::new(channel); + Self { client } + } + + /// Establishes a connection to the GPT answer service at the specified URI. + /// + /// # Arguments + /// + /// * `uri`: The URI of the GPT answer service. + /// + /// # Returns + /// + /// Returns a `Result` containing the connected `GptAnswerClient` if successful, + /// or a `CoreError` if an error occurs during connection. + pub async fn connect(uri: String) -> Result { + // Establish connection to the gRPC server + let channel: Channel = Channel::from_shared(uri) + .map_err(|err| CoreError::InternalError(err.into()))? + .connect() + .await + .map_err(|err| CoreError::InternalError(err.into()))?; + + // Create a new client instance with the connected channel + let client = Self::new(channel); + Ok(client) + } +} + +#[async_trait] +impl GptAnswerPort for GptAnswerClient { + /// Sends a question to the GPT answer service and retrieves the generated answer. + /// + /// # Arguments + /// + /// * `question`: A `&str` representing the question to be sent to the service. + /// + /// # Returns + /// + /// Returns a `Result` containing the generated answer as a `String` if successful, + /// or a `CoreError` if an error occurs during communication with the service. + async fn get_answer(&self, question: &str) -> Result { + // Create a gRPC request with the question payload + let request = tonic::Request::new(GetAnswerPayload { + question: question.to_string(), + }); + + // Send the request to the gRPC server and await the response + let response = self + .client + .clone() + .get_answer(request) + .await + .map_err(|err| CoreError::InternalError(err.into()))?; + + // Extract and return the answer from the response + Ok(response.into_inner().answer) + } +} diff --git a/src/adapter/src/repositories/grpc/mod.rs b/src/adapter/src/repositories/grpc/mod.rs index b6a44ac..cbab5c2 100644 --- a/src/adapter/src/repositories/grpc/mod.rs +++ b/src/adapter/src/repositories/grpc/mod.rs @@ -1,2 +1 @@ -pub mod config; -pub mod models; +pub mod gpt_answer_client; diff --git a/src/adapter/src/repositories/grpc/models/gpt_answer.rs b/src/adapter/src/repositories/grpc/models/gpt_answer.rs deleted file mode 100644 index 4e42922..0000000 --- a/src/adapter/src/repositories/grpc/models/gpt_answer.rs +++ /dev/null @@ -1,40 +0,0 @@ -use rust_core::common::errors::CoreError; -use tonic::transport::Channel; - -use grpc_interface::interfaces::gpt_answer::gpt_answer::{ - gpt_answer_service_client::GptAnswerServiceClient, GetAnswerPayload, -}; - -pub struct GptAnswerGrpcClient { - client: GptAnswerServiceClient, -} - -impl GptAnswerGrpcClient { - fn new(channel: Channel) -> Self { - let client = GptAnswerServiceClient::new(channel); - Self { client } - } - - pub async fn get_instance(uri: &'static str) -> Result { - let channel = Channel::from_static(uri).connect().await.map_err(|err| { - eprintln!("Error connecting to GPT: {:?}", err); - CoreError::InternalError - })?; - - let client = Self::new(channel); - Ok(client) - } - - pub async fn get_answer(&mut self, question: &str) -> Result { - let request = tonic::Request::new(GetAnswerPayload { - question: question.to_string(), - }); - - let response = self.client.get_answer(request).await.map_err(|err| { - eprintln!("Error getting answer from GPT: {:?}", err); - CoreError::InternalError - })?; - - Ok(response.into_inner().answer) - } -} diff --git a/src/adapter/src/repositories/postgres/models/question.rs b/src/adapter/src/repositories/postgres/models/question.rs index 93e804c..ad2aa18 100644 --- a/src/adapter/src/repositories/postgres/models/question.rs +++ b/src/adapter/src/repositories/postgres/models/question.rs @@ -1,12 +1,11 @@ -use std::{ - io::{Error, ErrorKind}, - time::SystemTime, -}; +use std::io::{Error, ErrorKind}; +use std::time::SystemTime; use diesel::{AsChangeset, Identifiable, Insertable, Queryable, Selectable}; -use rust_core::entities::question::{QuestionEntity, QuestionId}; use serde::Serialize; +use rust_core::entities::question::{QuestionEntity, QuestionId}; + #[derive(Debug, Queryable, Serialize, Selectable, Insertable, AsChangeset, Identifiable)] #[diesel(table_name = super::super::schema::questions)] #[cfg_attr(feature = "postgres", derive(diesel::pg::Pg))] diff --git a/src/adapter/src/repositories/postgres/question_db.rs b/src/adapter/src/repositories/postgres/question_db.rs index 9b3b40d..9627a7a 100644 --- a/src/adapter/src/repositories/postgres/question_db.rs +++ b/src/adapter/src/repositories/postgres/question_db.rs @@ -37,14 +37,14 @@ impl QuestionPort for QuestionDBRepository { .await .unwrap() .interact(move |conn| { - let question = - QuestionModel::try_from(question).map_err(|_| CoreError::InternalError)?; + let question = QuestionModel::try_from(question) + .map_err(|err| CoreError::InternalError(err.into()))?; let response = insert_into(questions) .values(&question) .get_result::(conn) .map_err(|err| match err { diesel::result::Error::NotFound => CoreError::NotFound, - _ => CoreError::InternalError, + _ => CoreError::InternalError(err.into()), }) .unwrap(); Ok(response.into()) @@ -59,14 +59,14 @@ impl QuestionPort for QuestionDBRepository { .await .unwrap() .interact(move |conn| { - let question = - QuestionModel::try_from(question).map_err(|_| CoreError::InternalError)?; + let question = QuestionModel::try_from(question) + .map_err(|err| CoreError::InternalError(err.into()))?; let response = update(questions.filter(id.eq(question.id))) .set(&question) .get_result::(conn) .map_err(|err| match err { diesel::result::Error::NotFound => CoreError::NotFound, - _ => CoreError::InternalError, + _ => CoreError::InternalError(err.into()), })? .into(); @@ -87,7 +87,7 @@ impl QuestionPort for QuestionDBRepository { .execute(conn) .map_err(|err| match err { diesel::result::Error::NotFound => CoreError::NotFound, - _ => CoreError::InternalError, + _ => CoreError::InternalError(err.into()), })?; Ok(()) @@ -109,7 +109,7 @@ impl QuestionPort for QuestionDBRepository { .first(conn) .map_err(|err| match err { diesel::result::Error::NotFound => CoreError::NotFound, - _ => CoreError::InternalError, + _ => CoreError::InternalError(err.into()), })? .into(); @@ -133,7 +133,7 @@ impl QuestionPort for QuestionDBRepository { .load(conn) .map_err(|err| match err { diesel::result::Error::NotFound => CoreError::NotFound, - _ => CoreError::InternalError, + _ => CoreError::InternalError(err.into()), })?; Ok(question_list diff --git a/src/adapter/src/repositories/repository_test.rs b/src/adapter/src/repositories/repository_test.rs index 495e8bb..1b12f5a 100644 --- a/src/adapter/src/repositories/repository_test.rs +++ b/src/adapter/src/repositories/repository_test.rs @@ -51,7 +51,7 @@ mod tests { query_params.insert("start".to_string(), "0".to_string()); query_params.insert("end".to_string(), "10".to_string()); - let pagination = match PaginationEntity::from_query(&query_params) { + let pagination = match PaginationEntity::try_from(query_params) { Ok(pagination_entity) => pagination_entity, Err(err) => { panic!("Failed to parse pagination entity: {:?}", err); diff --git a/src/common/Cargo.toml b/src/common/Cargo.toml index c727049..4e8982e 100644 --- a/src/common/Cargo.toml +++ b/src/common/Cargo.toml @@ -11,12 +11,20 @@ autoexamples = true autotests = true autobenches = true -[dependencies] -config = "0.14.0" -glob = "0.3.1" -tracing = "0.1" -tracing-bunyan-formatter = "0.3.9" -tracing-opentelemetry = "0.23.0" +[dependencies.config] +version = "0.14.0" + +[dependencies.glob] +version = "0.3.1" + +[dependencies.tracing] +version = "0.1" + +[dependencies.tracing-bunyan-formatter] +version = "0.3.9" + +[dependencies.tracing-opentelemetry] +version = "0.23.0" [dependencies.opentelemetry] version = "0.22.0" @@ -39,3 +47,15 @@ features = ["derive"] [dependencies.tracing-subscriber] version = "0.3.18" features = ["env-filter"] + +[dependencies.tonic] +version = "0.11.0" + +[dependencies.prost] +version = "0.12.3" + +[build-dependencies.tonic-build] +version = "0.11.0" + +[build-dependencies.glob] +version = "0.3.1" diff --git a/src/grpc/build.rs b/src/common/build.rs similarity index 100% rename from src/grpc/build.rs rename to src/common/build.rs diff --git a/src/grpc/proto/gpt_answer.proto b/src/common/proto/gpt_answer.proto similarity index 100% rename from src/grpc/proto/gpt_answer.proto rename to src/common/proto/gpt_answer.proto diff --git a/src/common/src/grpc/gpt_answer.rs b/src/common/src/grpc/gpt_answer.rs new file mode 100644 index 0000000..9bdbe8f --- /dev/null +++ b/src/common/src/grpc/gpt_answer.rs @@ -0,0 +1,9 @@ +/// Module for gRPC service definitions related to answering questions with GPT (Generative Pre-trained Transformer) models. +/// +/// This module includes generated gRPC service definitions for answering questions using GPT models. +/// The `tonic::include_proto!` macro is used to include the protobuf definitions, enabling easy +/// integration of gRPC services into Rust code. +pub mod gpt_answer { + // Include the protobuf definitions for the gpt_answer service. + tonic::include_proto!("gpt_answer"); +} diff --git a/src/adapter/src/repositories/grpc/models/mod.rs b/src/common/src/grpc/mod.rs similarity index 100% rename from src/adapter/src/repositories/grpc/models/mod.rs rename to src/common/src/grpc/mod.rs diff --git a/src/common/src/lib.rs b/src/common/src/lib.rs index c9499bd..4598395 100644 --- a/src/common/src/lib.rs +++ b/src/common/src/lib.rs @@ -1,2 +1,3 @@ +pub mod grpc; pub mod loggers; pub mod options; diff --git a/src/core/Cargo.toml b/src/core/Cargo.toml index 189d377..61da5a9 100644 --- a/src/core/Cargo.toml +++ b/src/core/Cargo.toml @@ -22,5 +22,5 @@ features = ["derive"] [dependencies.thiserror] version = "1.0.57" -[dependencies] -anyhow = "1.0.80" \ No newline at end of file +[dependencies.anyhow] +version = "1.0.80" diff --git a/src/core/src/common/errors.rs b/src/core/src/common/errors.rs index 0f2d689..4cd8cf8 100644 --- a/src/core/src/common/errors.rs +++ b/src/core/src/common/errors.rs @@ -1,3 +1,5 @@ +use anyhow::Error; + #[derive(thiserror::Error, Debug)] pub enum CoreError { #[error("parse error {0}")] @@ -8,10 +10,10 @@ pub enum CoreError { #[error("missing parameters")] MissingParameters, + #[error("not found")] NotFound, - #[error("transparent")] - InternalError, - #[error("unknown data store error")] - Unknown, + + #[error("internal error {0}")] + InternalError(#[from] Error), } diff --git a/src/core/src/entities/pagination_entity.rs b/src/core/src/entities/pagination_entity.rs index a2d38c9..944eb5e 100644 --- a/src/core/src/entities/pagination_entity.rs +++ b/src/core/src/entities/pagination_entity.rs @@ -15,37 +15,15 @@ pub struct PaginationEntity { pub sort: Option>, } -impl PaginationEntity { - /// Constructs a `PaginationEntity` from query parameters. - /// - /// # Arguments - /// - /// * `query` - A HashMap containing query parameters. - /// - /// # Returns - /// - /// A Result containing the constructed `PaginationEntity` or an `Error` if parsing fails. - /// - /// # Example - /// - /// ```rust - /// use std::collections::HashMap; - /// use rust_core::entities::pagination_entity::PaginationEntity; - /// - /// let mut query_params = HashMap::new(); - /// query_params.insert("start".to_string(), "0".to_string()); - /// query_params.insert("end".to_string(), "10".to_string()); - /// - /// match PaginationEntity::from_query(&query_params) { - /// Ok(pagination_entity) => { - /// println!("Parsed pagination entity: {:?}", pagination_entity); - /// } - /// Err(err) => { - /// eprintln!("Failed to parse pagination entity: {:?}", err); - /// } - /// } - /// ``` - pub fn from_query(query: &HashMap) -> Result { +/// Implementation of the `TryFrom` trait to convert a HashMap into a `PaginationEntity`. +/// +/// This implementation allows converting a HashMap containing query parameters into a `PaginationEntity`. +/// It attempts to parse the start and end pagination parameters from the HashMap and constructs a `PaginationEntity` +/// instance. If parsing fails for any reason, it returns a `CoreError`. +impl TryFrom> for PaginationEntity { + type Error = CoreError; + + fn try_from(query: HashMap) -> Result { let start = query .get("start") .unwrap_or(&"0".to_string()) @@ -73,7 +51,7 @@ mod tests { query_params_1.insert("start".to_string(), "0".to_string()); query_params_1.insert("end".to_string(), "10".to_string()); - match PaginationEntity::from_query(&query_params_1) { + match PaginationEntity::try_from(query_params_1) { Ok(pagination_entity) => { assert_eq!(pagination_entity.start, 0); assert_eq!(pagination_entity.end, 10); @@ -91,7 +69,7 @@ mod tests { let mut query_params_2 = HashMap::new(); query_params_2.insert("start".to_string(), "abs".to_string()); query_params_2.insert("end".to_string(), "10".to_string()); - match PaginationEntity::from_query(&query_params_2) { + match PaginationEntity::try_from(query_params_2) { Ok(_) => { panic!("Expected an error, but got Ok"); } diff --git a/src/core/src/entities/question_filter.rs b/src/core/src/entities/question_filter.rs index 0e43dc6..53f3054 100644 --- a/src/core/src/entities/question_filter.rs +++ b/src/core/src/entities/question_filter.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::convert::TryFrom; use serde::{Deserialize, Serialize}; @@ -13,39 +14,17 @@ pub struct QuestionFilter { pub pagination: PaginationEntity, } -impl QuestionFilter { - /// Constructs a `QuestionFilter` from query parameters. - /// - /// # Arguments - /// - /// * `query` - A HashMap containing query parameters. - /// - /// # Returns - /// - /// A Result containing the constructed `QuestionFilter` or an `Error` if parsing fails. - /// - /// # Example - /// - /// ```rust - /// use std::collections::HashMap; - /// use rust_core::entities::question_filter::QuestionFilter; - /// - /// let mut query_params = HashMap::new(); - /// query_params.insert("start".to_string(), "0".to_string()); - /// query_params.insert("end".to_string(), "10".to_string()); - /// - /// match QuestionFilter::from_query(&query_params) { - /// Ok(question_filter) => { - /// println!("Parsed question filter: {:?}", question_filter); - /// } - /// Err(err) => { - /// eprintln!("Failed to parse question filter: {:?}", err); - /// } - /// } - /// ``` - pub fn from_query(query: &HashMap) -> Result { +/// Implementation of the `TryFrom` trait to convert a HashMap into a `QuestionFilter`. +/// +/// This implementation allows converting a HashMap containing query parameters into a `QuestionFilter`. +/// It attempts to parse the pagination parameters from the HashMap and constructs a `QuestionFilter` +/// instance. If parsing fails for any reason, it returns a `CoreError`. +impl TryFrom> for QuestionFilter { + type Error = CoreError; + + fn try_from(query: HashMap) -> Result { Ok(QuestionFilter { - pagination: PaginationEntity::from_query(query)?, + pagination: PaginationEntity::try_from(query)?, }) } } @@ -63,7 +42,7 @@ mod tests { query_params_1.insert("start".to_string(), "0".to_string()); query_params_1.insert("end".to_string(), "10".to_string()); - match QuestionFilter::from_query(&query_params_1) { + match QuestionFilter::try_from(query_params_1) { Ok(question_filter) => { assert_eq!(question_filter.pagination.start, 0); assert_eq!(question_filter.pagination.end, 10); @@ -81,14 +60,14 @@ mod tests { query_params_2.insert("start".to_string(), "asd".to_string()); query_params_2.insert("end".to_string(), "10".to_string()); - match QuestionFilter::from_query(&query_params_2) { + match QuestionFilter::try_from(query_params_2) { Ok(_) => { panic!("Expected an error, but got Ok"); } Err(err) => match err { CoreError::ParseError(_) => {} _ => { - panic!("Expected MissingParameters error, but got {:?}", err); + panic!("Expected ParseError error, but got {:?}", err); } }, } diff --git a/src/core/src/ports/gpt_answer.rs b/src/core/src/ports/gpt_answer.rs new file mode 100644 index 0000000..07a2baa --- /dev/null +++ b/src/core/src/ports/gpt_answer.rs @@ -0,0 +1,8 @@ +use async_trait::async_trait; + +use crate::common::errors::CoreError; + +#[async_trait] +pub trait GptAnswerPort { + async fn get_answer(&self, question: &str) -> Result; +} diff --git a/src/core/src/ports/mod.rs b/src/core/src/ports/mod.rs index b44264d..d2e9d02 100644 --- a/src/core/src/ports/mod.rs +++ b/src/core/src/ports/mod.rs @@ -1 +1,2 @@ +pub mod gpt_answer; pub mod question; diff --git a/src/core/src/ports/question.rs b/src/core/src/ports/question.rs index b2e69f3..dca3f3a 100644 --- a/src/core/src/ports/question.rs +++ b/src/core/src/ports/question.rs @@ -1,9 +1,9 @@ +use async_trait::async_trait; + use crate::common::errors::CoreError; use crate::entities::question::{QuestionEntity, QuestionId}; use crate::entities::question_filter::QuestionFilter; -use async_trait::async_trait; - #[async_trait] pub trait QuestionPort { async fn add(&self, question: QuestionEntity) -> Result; diff --git a/src/grpc_server/Cargo.toml b/src/gpt_answer_server/Cargo.toml similarity index 76% rename from src/grpc_server/Cargo.toml rename to src/gpt_answer_server/Cargo.toml index d6a048b..f4f8e6a 100644 --- a/src/grpc_server/Cargo.toml +++ b/src/gpt_answer_server/Cargo.toml @@ -4,7 +4,7 @@ test = [] example = [] [package] -name = "grpc_server" +name = "gpt_answer_server" edition = "2021" version = "0.0.1" autobins = true @@ -12,15 +12,18 @@ autoexamples = true autotests = true autobenches = true +[dependencies.rust_core] +path = "../core" + +[dependencies.common] +path = "../common" + [dependencies.tonic] version = "0.11.0" [dependencies.prost] version = "0.12.3" -[dependencies.rust_core] -path = "../core" - [dependencies.serde] version = "1.0" features = ["derive"] @@ -36,24 +39,21 @@ features = ["full"] [dependencies.opentelemetry] version = "0.22.0" -[dependencies] -serde_json = "1.0" -readonly = "0.2.12" -tracing = "0.1" - -[dependencies.common] -path = "../common" +[dependencies.serde_json] +version = "1.0" -[dependencies.grpc_interface] -path = "../grpc" +[dependencies.readonly] +version = "0.2.12" -[build-dependencies] -tonic-build = "0.11.0" +[dependencies.tracing] +version = "0.1" +[build-dependencies.tonic-build] +version = "0.11.0" [lib] path = "src/lib.rs" -name = "grpc_server" +name = "gpt_answer_server" test = true doctest = true bench = true diff --git a/src/grpc_server/build.rs b/src/gpt_answer_server/build.rs similarity index 100% rename from src/grpc_server/build.rs rename to src/gpt_answer_server/build.rs diff --git a/src/grpc_server/config/00-default.toml b/src/gpt_answer_server/config/00-default.toml similarity index 60% rename from src/grpc_server/config/00-default.toml rename to src/gpt_answer_server/config/00-default.toml index 1f4048f..baf3a2b 100644 --- a/src/grpc_server/config/00-default.toml +++ b/src/gpt_answer_server/config/00-default.toml @@ -1,5 +1,3 @@ service_name = "rust-grpc-server" exporter_endpoint = "http://localhost:7281" - -[servers.gpt_answer_service] -url = "0.0.0.0:50051" \ No newline at end of file +server_endpoint = "0.0.0.0:50051" \ No newline at end of file diff --git a/src/gpt_answer_server/src/controllers/gpt_answer.rs b/src/gpt_answer_server/src/controllers/gpt_answer.rs new file mode 100644 index 0000000..86f96e7 --- /dev/null +++ b/src/gpt_answer_server/src/controllers/gpt_answer.rs @@ -0,0 +1,49 @@ +use tonic::{Request, Response, Status}; + +use common::grpc::gpt_answer::gpt_answer::{ + gpt_answer_service_server::GptAnswerService, GetAnswerPayload, GetAnswerResponse, +}; + +/// Implementation of the gRPC service for generating answers to questions. +/// +/// This struct represents the gRPC server implementation for answering questions. It implements +/// the `GptAnswerService` trait generated by Tonic, which defines the RPC methods for answering +/// questions. +#[derive(Debug, Default)] +pub struct GptAnswerServer; + +#[tonic::async_trait] +impl GptAnswerService for GptAnswerServer { + /// Handle the gRPC `get_answer` request. + /// + /// This method is called when a gRPC client sends a request to get an answer to a question. + /// It receives a request containing the question payload and generates an answer based on + /// the received question. The logic for generating the answer is yet to be implemented. + /// + /// # Arguments + /// + /// * `request`: A `Request` containing the `GetAnswerPayload` with the question. + /// + /// # Returns + /// + /// Returns a `Result` containing a `Response` with the `GetAnswerResponse` containing the + /// generated answer if successful. If there's an error during processing, it returns a + /// `Status` indicating the error. + async fn get_answer( + &self, + request: Request, + ) -> Result, Status> { + // Extract the payload containing the question from the request + let payload = request.into_inner(); + + // TODO: Implement your logic to generate an answer based on the question. + // Placeholder logic: Generate an answer string + let answer = format!("Answer to: {}", payload.question); + + // Construct a response containing the generated answer + let response = GetAnswerResponse { answer }; + + // Return the response + Ok(Response::new(response)) + } +} diff --git a/src/grpc/src/interfaces/mod.rs b/src/gpt_answer_server/src/controllers/mod.rs similarity index 100% rename from src/grpc/src/interfaces/mod.rs rename to src/gpt_answer_server/src/controllers/mod.rs diff --git a/src/grpc_server/src/lib.rs b/src/gpt_answer_server/src/lib.rs similarity index 100% rename from src/grpc_server/src/lib.rs rename to src/gpt_answer_server/src/lib.rs diff --git a/src/grpc_server/src/main.rs b/src/gpt_answer_server/src/main.rs similarity index 63% rename from src/grpc_server/src/main.rs rename to src/gpt_answer_server/src/main.rs index 201752b..7d1ff32 100644 --- a/src/grpc_server/src/main.rs +++ b/src/gpt_answer_server/src/main.rs @@ -1,11 +1,25 @@ use clap::{Parser, Subcommand}; -use grpc_server::{controllers, options}; use opentelemetry::global; +use tonic::transport::Server; +use common::grpc::gpt_answer::gpt_answer::gpt_answer_service_server::GptAnswerServiceServer; use common::loggers::telemetry::init_telemetry; use common::options::parse_options; -use controllers::gpt_answer::init_gpt_answer_server; -use options::Options; +use gpt_answer_server::controllers::gpt_answer::GptAnswerServer; +use gpt_answer_server::options::Options; + +pub async fn init_grpc_server(options: Options) { + let server_endpoint = &options.server_endpoint; + let address = server_endpoint.parse().unwrap(); + println!("Starting GPT Answer server at {}", server_endpoint); + + let gpt_answer_server = GptAnswerServer::default(); + Server::builder() + .add_service(GptAnswerServiceServer::new(gpt_answer_server)) + .serve(address) + .await + .unwrap(); +} #[tokio::main] async fn main() { @@ -34,14 +48,14 @@ async fn main() { options.log.level.as_str(), ); - let gpt_answer_server = tokio::spawn(init_gpt_answer_server(options)); + let gpt_answer_server = tokio::spawn(init_grpc_server(options)); tokio::try_join!(gpt_answer_server).expect("Failed to run servers"); global::shutdown_tracer_provider(); } -/// Simple REST server. +/// GPT Answer GRPC server. #[derive(Parser, Debug)] #[command(about, long_about = None)] struct Args { diff --git a/src/grpc_server/src/options.rs b/src/gpt_answer_server/src/options.rs similarity index 50% rename from src/grpc_server/src/options.rs rename to src/gpt_answer_server/src/options.rs index f660c2c..f32eb2b 100644 --- a/src/grpc_server/src/options.rs +++ b/src/gpt_answer_server/src/options.rs @@ -1,15 +1,16 @@ -use common::options::{default_log, Log}; use serde::Deserialize; +use common::options::{default_log, Log}; + /// Configuration options for the application. /// /// This struct represents the configuration options for the application, including server settings, -/// database configuration, endpoint for the exporter, service name, and logging configuration. +/// endpoint for server, endpoint for the exporter, service name, and logging configuration. #[readonly::make] #[derive(Deserialize, Debug)] pub struct Options { - /// Configuration for the servers. - pub servers: GrpcServers, + /// Configuration for the grpc server endpoint. + pub server_endpoint: String, /// The endpoint for the exporter. pub exporter_endpoint: String, /// The name of the service. @@ -18,17 +19,3 @@ pub struct Options { #[serde(default = "default_log")] pub log: Log, } - -/// Represents servers configuration options. -#[derive(Deserialize, Debug)] -pub struct GrpcServers { - /// Configuration for using in-memory database. - pub gpt_answer_service: Option, -} - -/// Represents service server configuration. -#[derive(Debug, Deserialize, Clone)] -pub struct ServiceServer { - /// URL for the server. - pub url: String, -} diff --git a/src/grpc/Cargo.toml b/src/grpc/Cargo.toml deleted file mode 100644 index 652ee3f..0000000 --- a/src/grpc/Cargo.toml +++ /dev/null @@ -1,38 +0,0 @@ -bin = [] -bench = [] -test = [] -example = [] - -[package] -name = "grpc_interface" -edition = "2021" -version = "0.0.1" -autobins = true -autoexamples = true -autotests = true -autobenches = true - -[dependencies.tonic] -version = "0.11.0" - -[dependencies.prost] -version = "0.12.3" - -[build-dependencies] -tonic-build = "0.11.0" -glob = "0.3.1" - - -[lib] -path = "src/lib.rs" -name = "grpc_interface" -test = true -doctest = true -bench = true -doc = true -plugin = false -proc-macro = false -harness = true -edition = "2021" -required-features = [] -crate-type = ["rlib"] diff --git a/src/grpc/src/interfaces/gpt_answer.rs b/src/grpc/src/interfaces/gpt_answer.rs deleted file mode 100644 index ef738af..0000000 --- a/src/grpc/src/interfaces/gpt_answer.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod gpt_answer { - tonic::include_proto!("gpt_answer"); -} diff --git a/src/grpc/src/lib.rs b/src/grpc/src/lib.rs deleted file mode 100644 index 43b15ec..0000000 --- a/src/grpc/src/lib.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod interfaces; diff --git a/src/grpc_server/src/controllers/gpt_answer.rs b/src/grpc_server/src/controllers/gpt_answer.rs deleted file mode 100644 index 7aae3d4..0000000 --- a/src/grpc_server/src/controllers/gpt_answer.rs +++ /dev/null @@ -1,52 +0,0 @@ -use grpc_interface::interfaces::gpt_answer::gpt_answer::gpt_answer_service_server::{ - GptAnswerService, GptAnswerServiceServer, -}; -use grpc_interface::interfaces::gpt_answer::gpt_answer::{GetAnswerPayload, GetAnswerResponse}; -use rust_core::common::errors::CoreError; -use tonic::{transport::Server, Request, Response, Status}; - -use crate::options::Options; - -#[derive(Debug, Default)] -pub struct GptAnswerServer; - -#[tonic::async_trait] -impl GptAnswerService for GptAnswerServer { - async fn get_answer( - &self, - request: Request, - ) -> Result, Status> { - let payload = request.into_inner(); - // TODO: Implement your logic to generate an answer based on the question. - let answer = format!("Answer to: {}", payload.question); - - let response = GetAnswerResponse { answer }; - Ok(Response::new(response)) - } -} - -pub async fn init_gpt_answer_server(options: Options) { - let gpt_answer_config = options.servers.gpt_answer_service.clone().unwrap(); - let result = gpt_answer_config.url.parse().map_err(|err| { - eprintln!("Error: {:?}", err); - CoreError::InternalError - }); - - if result.is_ok() { - let addr = result.unwrap(); - - println!("GPT Answer server config at {}", addr); - - let gpt_answer_server = GptAnswerServer::default(); - - Server::builder() - .add_service(GptAnswerServiceServer::new(gpt_answer_server)) - .serve(addr) - .await - .unwrap(); - - println!("GPT Answer server started at {}", addr); - } else { - eprintln!("GPT Answer server failed to start"); - } -} diff --git a/src/grpc_server/src/controllers/mod.rs b/src/grpc_server/src/controllers/mod.rs deleted file mode 100644 index bec2060..0000000 --- a/src/grpc_server/src/controllers/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod gpt_answer; diff --git a/src/public/Cargo.toml b/src/public/Cargo.toml index 74b8a55..38ae337 100644 --- a/src/public/Cargo.toml +++ b/src/public/Cargo.toml @@ -50,24 +50,37 @@ autoexamples = true autotests = true autobenches = true -[dependencies] -diesel_migrations = "2.1.0" -rand = "0.8.4" -readonly = "0.2.12" -serde_json = "1.0" -tracing = "0.1" -warp = "0.3.6" +[dependencies.common] +path = "../common" [dependencies.adapter] path = "../adapter" +[dependencies.rust_core] +path = "../core" + +[dependencies.diesel_migrations] +version = "2.1.0" + +[dependencies.rand] +version = "0.8.4" + +[dependencies.readonly] +version = "0.2.12" + +[dependencies.serde_json] +version = "1.0" + +[dependencies.tracing] +version = "0.1" + +[dependencies.warp] +version = "0.3.6" + [dependencies.clap] version = "4.4.7" features = ["derive"] -[dependencies.common] -path = "../common" - [dependencies.deadpool-diesel] version = "0.5.0" features = ["postgres", "serde"] @@ -82,9 +95,6 @@ version = "0.10.64" [dependencies.opentelemetry] version = "0.22.0" -[dependencies.rust_core] -path = "../core" - [dependencies.serde] version = "1.0" features = ["derive"] @@ -103,9 +113,6 @@ version = "1.0.57" [dependencies.anyhow] version = "1.0.80" -[dependencies.grpc_interface] -path = "../grpc" - [lib] path = "src/lib.rs" name = "cli" diff --git a/src/public/config/00-default.toml b/src/public/config/00-default.toml index 029a598..2c40e55 100644 --- a/src/public/config/00-default.toml +++ b/src/public/config/00-default.toml @@ -1,9 +1,10 @@ service_name = "rust-api-server" exporter_endpoint = "http://localhost:7281" +gpt_answer_service_url = "grpc://0.0.0.0:50051" [db] [[inmemory]] [server] url = "0.0.0.0" -port = 8000 \ No newline at end of file +port = 8000 diff --git a/src/public/src/controllers/question.rs b/src/public/src/controllers/question.rs index 6bb8797..133b479 100644 --- a/src/public/src/controllers/question.rs +++ b/src/public/src/controllers/question.rs @@ -2,18 +2,20 @@ use std::collections::HashMap; use std::str::FromStr; use std::sync::Arc; -use adapter::repositories::grpc::models::gpt_answer::GptAnswerGrpcClient; +use anyhow::Error; use tracing::instrument; use warp::http::StatusCode; use warp::reject::Rejection; use warp::Reply; +use adapter::repositories::grpc::gpt_answer_client::GptAnswerClient; +use rust_core::common::errors::CoreError; use rust_core::entities::question::{QuestionEntity, QuestionId}; use rust_core::entities::question_filter::QuestionFilter; +use rust_core::ports::gpt_answer::GptAnswerPort; use rust_core::ports::question::QuestionPort; use crate::errors::WarpError; -use crate::options::GrpcClients; /// Handler for retrieving questions based on query parameters. /// @@ -23,10 +25,9 @@ use crate::options::GrpcClients; #[instrument(level = "info", skip(question_port))] pub async fn get_questions( question_port: Arc, - server_config: Arc, query: HashMap, ) -> Result { - let question_filter = QuestionFilter::from_query(&query).map_err(WarpError::from)?; + let question_filter = QuestionFilter::try_from(query).map_err(WarpError::from)?; let questions = question_port .list(&question_filter) @@ -44,7 +45,6 @@ pub async fn get_questions( #[instrument(level = "info", skip(question_port))] pub async fn get_question( question_port: Arc, - server_config: Arc, id: String, ) -> Result { let question_id = QuestionId::from_str(id.as_str()).map_err(WarpError::from)?; @@ -65,7 +65,6 @@ pub async fn get_question( #[instrument(level = "info", skip(question_port))] pub async fn add_question( question_port: Arc, - server_config: Arc, question: QuestionEntity, ) -> Result { question_port.add(question).await.map_err(WarpError::from)?; @@ -81,7 +80,6 @@ pub async fn add_question( #[instrument(level = "info", skip(question_port))] pub async fn delete_question( question_port: Arc, - server_config: Arc, id: String, ) -> Result { let question_id = QuestionId::from_str(id.as_str()).map_err(WarpError::from)?; @@ -103,12 +101,10 @@ pub async fn delete_question( #[instrument(level = "info", skip(question_port))] pub async fn update_question( question_port: Arc, - server_config: Arc, id: String, mut question: QuestionEntity, ) -> Result { - let question_id = QuestionId::from_str(id.as_str()).map_err(WarpError::from)?; - question.id = question_id; + question.id = QuestionId::from_str(id.as_str()).map_err(WarpError::from)?; question_port .update(question) @@ -121,39 +117,43 @@ pub async fn update_question( /// Controller for handling HTTP GET requests to fetch answers for a given question ID. /// /// This controller retrieves a question from the provided `QuestionPort` based on the -/// specified ID, calls the gRPC client (`GrpcClient`) to get an answer using the -/// question's text, and responds with the answer in a JSON format. +/// specified ID, calls the gRPC client (`GptAnswerClient`) to get an answer using the +/// question's content, and responds with the answer in a JSON format. /// /// # Arguments /// /// * `question_port`: A trait object implementing `QuestionPort` for interacting with questions. -/// * `gpt_client`: An instance of `GrpcClient` used for communication with the gRPC server. +/// * `gpt_answer_client`: The gRPC client instance for answering questions. /// * `id`: The ID of the question to fetch the answer for. /// /// # Returns /// -/// Returns a `Result` containing the HTTP response. If successful, responds with a String -/// representation of the answer and a status code of 200 OK. If there's an error during -/// question retrieval, gRPC communication, or response construction, it returns a Warp Rejection. -#[instrument(level = "info", skip(question_port))] -pub async fn get_question_answer_controller( +/// Returns a `Result` containing the HTTP response. If successful, responds with the answer +/// as a JSON string and a status code of `200 OK`. If there's an error during +/// question retrieval, gRPC communication, or response construction, it returns a Warp `Rejection`. +#[instrument(level = "info", skip(question_port, gpt_answer_client))] +pub async fn get_question_answer( question_port: Arc, - server_config: Arc, + gpt_answer_client: Option, id: String, ) -> Result { + let question_id = QuestionId::from_str(&id).map_err(WarpError::from)?; + let question = question_port - .get(&QuestionId::from_str(id.as_str()).unwrap()) + .get(&question_id) .await .map_err(WarpError::from)?; - // Clone the gpt_client string to have a static lifetime - let gpt_client = server_config.gpt_answer_service.clone().unwrap().url; - - // Use the cloned gpt_client string - let client = GptAnswerGrpcClient::get_instance(gpt_client.leak()).await; + let client = match gpt_answer_client { + Some(client) => client, + None => { + return Err(warp::reject::custom(WarpError::from( + CoreError::InternalError(Error::msg("Failed to connect to GPT answer service")), + ))) + } + }; let answer = client - .unwrap() .get_answer(&question.content) .await .map_err(WarpError::from)?; diff --git a/src/public/src/errors.rs b/src/public/src/errors.rs index 555164c..e92437f 100644 --- a/src/public/src/errors.rs +++ b/src/public/src/errors.rs @@ -1,4 +1,5 @@ use std::io; + use thiserror::Error; use warp::body::BodyDeserializeError; use warp::cors::CorsForbidden; @@ -34,7 +35,7 @@ pub async fn return_error(r: Rejection) -> Result { "MissingParameters".to_string(), StatusCode::BAD_REQUEST, )), - CoreError::InternalError => Ok(warp::reply::with_status( + CoreError::InternalError(_) => Ok(warp::reply::with_status( "InternalError".to_string(), StatusCode::INTERNAL_SERVER_ERROR, )), diff --git a/src/public/src/main.rs b/src/public/src/main.rs index a5c0302..8a4f1b8 100644 --- a/src/public/src/main.rs +++ b/src/public/src/main.rs @@ -1,13 +1,12 @@ +use std::net::{Ipv4Addr, SocketAddrV4}; +use std::str::FromStr; +use std::sync::Arc; + #[cfg_attr(debug_assertions, allow(dead_code, unused_imports))] use openssl; #[rustfmt::skip] #[cfg_attr(debug_assertions, allow(dead_code, unused_imports))] use diesel; - -use std::net::{Ipv4Addr, SocketAddrV4}; -use std::str::FromStr; -use std::sync::Arc; - use clap::{Parser, Subcommand}; use deadpool_diesel::postgres::Pool; use deadpool_diesel::{Manager, Runtime}; @@ -93,12 +92,13 @@ pub async fn run_warp_server(options: Options) { Arc::new(QuestionInMemoryRepository::new()) }; - let grpc_clients = options.grpc_clients.clone(); - let router = Router::new(question_port, Arc::new(grpc_clients)); - + let grpc_client = options.gpt_answer_service_url.clone(); + let router = Router::new(question_port, grpc_client.into()); + let routers = router.routes().await; let address = SocketAddrV4::new( Ipv4Addr::from_str(options.server.url.as_str()).unwrap(), options.server.port, ); - warp::serve(router.routes()).run(address).await + + warp::serve(routers).run(address).await } diff --git a/src/public/src/options.rs b/src/public/src/options.rs index ad1c30a..a31729f 100644 --- a/src/public/src/options.rs +++ b/src/public/src/options.rs @@ -12,9 +12,9 @@ use common::options::{default_log, Log}; pub struct Options { /// Configuration for the server. pub server: Server, - /// Specifies the backend database will be used. - pub grpc_clients: GrpcClients, - /// Specifies the backend database will be used. + /// URL for the GPT Answer gRPC client. + pub gpt_answer_service_url: String, + /// Specifies the configuration of database will be connected. pub db: Database, /// The endpoint for the exporter. pub exporter_endpoint: String, @@ -46,16 +46,3 @@ pub struct Server { /// URL for the server. pub url: String, } - -/// Represents server configuration. -#[derive(Debug, Deserialize, Clone)] -pub struct GrpcClients { - pub gpt_answer_service: Option, -} - -/// Represents service server configuration. -#[derive(Debug, Deserialize, Clone)] -pub struct ServiceServer { - /// URL for the server. - pub url: String, -} diff --git a/src/public/src/router.rs b/src/public/src/router.rs index 98c33a6..9b4b5f8 100644 --- a/src/public/src/router.rs +++ b/src/public/src/router.rs @@ -1,55 +1,61 @@ use std::sync::Arc; - use warp::http::Method; use warp::{Filter, Rejection, Reply}; +use adapter::repositories::grpc::gpt_answer_client::GptAnswerClient; use rust_core::ports::question::QuestionPort; use crate::controllers::question::{ - add_question, delete_question, get_question, get_question_answer_controller, get_questions, + add_question, delete_question, get_question, get_question_answer, get_questions, update_question, }; use crate::errors::return_error; -use crate::options::GrpcClients; /// Router for handling HTTP requests related to questions. pub struct Router { question_port: Arc, - server_config: Arc, + gpt_answer_service_url: Arc, } impl Router { /// Creates a new Router instance with the specified QuestionPort. pub fn new( question_port: Arc, - server_config: Arc, + gpt_answer_service_url: Arc, ) -> Self { Router { question_port: question_port.clone(), - server_config: server_config.clone(), + gpt_answer_service_url: gpt_answer_service_url.clone(), } } /// Configures and returns the Warp filter for handling HTTP requests. - pub fn routes(self) -> impl Filter + Clone { + pub async fn routes(self) -> impl Filter + Clone { let store_filter = warp::any().map(move || self.question_port.clone()); - let server_config = warp::any().map(move || self.server_config.clone()); + + let gpt_answer_client = GptAnswerClient::connect(self.gpt_answer_service_url.to_string()) + .await + .map_err(|err| { + tracing::error!("Failed to connect to GPT answer service: {:?}", err); + err + }) + .ok(); + let cors = warp::cors() .allow_any_origin() .allow_header("content-type") .allow_methods(&[Method::PUT, Method::DELETE, Method::GET, Method::POST]); + let get_questions = warp::get() .and(warp::path("questions")) .and(warp::path::end()) .and(store_filter.clone()) - .and(server_config.clone()) .and(warp::query()) .and_then(get_questions); let get_question = warp::get() .and(warp::path("questions")) .and(store_filter.clone()) - .and(server_config.clone()) .and(warp::path::param::()) .and(warp::path::end()) .and_then(get_question); @@ -58,14 +64,12 @@ impl Router { .and(warp::path("questions")) .and(warp::path::end()) .and(store_filter.clone()) - .and(server_config.clone()) .and(warp::body::json()) .and_then(add_question); let update_question = warp::put() .and(warp::path("questions")) .and(store_filter.clone()) - .and(server_config.clone()) .and(warp::path::param::()) .and(warp::path::end()) .and(warp::body::json()) @@ -74,7 +78,6 @@ impl Router { let delete_question = warp::delete() .and(warp::path("questions")) .and(store_filter.clone()) - .and(server_config.clone()) .and(warp::path::param::()) .and(warp::path::end()) .and_then(delete_question); @@ -82,10 +85,10 @@ impl Router { let get_question_answer = warp::get() .and(warp::path("questions")) .and(store_filter.clone()) - .and(server_config.clone()) + .and(warp::any().map(move || gpt_answer_client.clone())) .and(warp::path::param::()) .and(warp::path("answer")) - .and_then(get_question_answer_controller); + .and_then(get_question_answer); get_questions .with(cors) diff --git a/src/public/tests/questions_router_test.rs b/src/public/tests/questions_router_test.rs index 0672db1..57ed367 100644 --- a/src/public/tests/questions_router_test.rs +++ b/src/public/tests/questions_router_test.rs @@ -15,10 +15,7 @@ mod tests { in_memory::question::QuestionInMemoryRepository, postgres::question_db::{QuestionDBRepository, MIGRATIONS}, }; - use cli::{ - options::{GrpcClients, ServiceServer}, - router::Router, - }; + use cli::router::Router; use rust_core::{ entities::question::{QuestionEntity, QuestionId}, ports::question::QuestionPort, @@ -33,14 +30,10 @@ mod tests { where T: QuestionPort + Send + Sync + 'static, { - let server_config: GrpcClients = GrpcClients { - gpt_answer_service: Some(ServiceServer { - url: "http://localhost:50051".to_string(), - }), - }; + let gpt_answer_service_url = "grpc://0.0.0.0:50051".to_string(); - let router = Router::new(question_port, Arc::new(server_config)); - let routers = router.routes(); + let router = Router::new(question_port, gpt_answer_service_url.into()); + let routers = router.routes().await; let raw_question_id: String = rand::thread_rng().gen_range(1..=1000).to_string(); let question_id = QuestionId::from_str(&raw_question_id.clone()).unwrap();