|
1 | 1 | #include <iostream> |
| 2 | +#include <ManapiGrpc.hpp> |
2 | 3 |
|
3 | 4 | #include <ManapiHttp.hpp> |
4 | 5 | #include <ManapiInitTools.hpp> |
| 6 | +#include <grpcpp/health_check_service_interface.h> |
| 7 | +#include <grpcpp/ext/proto_server_reflection_plugin.h> |
| 8 | + |
| 9 | +#include "ManapiFetch2.hpp" |
5 | 10 | #include "ext/pq/AsyncPostgreClient.hpp" |
6 | 11 | #include "ext/pq/AsyncPostgrePool.hpp" |
| 12 | + |
| 13 | +#include "protobuf/helloworld.grpc.pb.h" |
| 14 | + |
7 | 15 | #define FOLDER "/home/Timur/Downloads/anime-main/" |
8 | 16 |
|
| 17 | + |
| 18 | +// Logic and data behind the server's behavior. |
| 19 | +class GreeterServiceImpl final : public helloworld::Greeter::CallbackService { |
| 20 | + grpc::ServerUnaryReactor *SayHello(grpc::CallbackServerContext* context, const helloworld::HelloRequest* request, |
| 21 | + helloworld::HelloReply* reply) override { |
| 22 | + grpc::ServerUnaryReactor* reactor = context->DefaultReactor(); |
| 23 | + manapi::async::run ([reactor, reply, request] () -> manapi::future<> { |
| 24 | + try { |
| 25 | + reply->set_message(std::format("Hello, {}! Fact: {}", request->name(), "im happy")); |
| 26 | + } |
| 27 | + catch (std::exception const &e) { |
| 28 | + reply->set_message(std::format("Hello, {}! Something gets wrong: {}", request->name(), e.what())); |
| 29 | + } |
| 30 | + reactor->Finish(grpc::Status::OK); |
| 31 | + }); |
| 32 | + return reactor; |
| 33 | + } |
| 34 | +}; |
| 35 | + |
9 | 36 | int main() { |
10 | 37 | manapi::init_tools::log_trace_init(manapi::debug::LOG_TRACE_HARD); |
11 | 38 |
|
12 | 39 | manapi::async::context::threadpoolfs(4); |
13 | 40 | manapi::async::context::gbs(manapi::async::context::blockedsignals()); |
14 | 41 |
|
15 | | - auto ctx = manapi::async::context::create(0).unwrap(); |
| 42 | + grpc::EnableDefaultHealthCheckService(true); |
| 43 | + grpc::reflection::InitProtoReflectionServerBuilderPlugin(); |
| 44 | + |
| 45 | + auto ctx = manapi::async::context::create(2).unwrap(); |
| 46 | + auto grpc_server_ctx = manapi::net::wgrpc::server_ctx::create().unwrap(); |
16 | 47 |
|
17 | 48 | auto server_ctx = manapi::net::http::server_ctx::create().unwrap(); |
18 | | - ctx->run(0, [server_ctx] (auto cb) -> void { |
| 49 | + ctx->run(2, [grpc_server_ctx, server_ctx] (auto cb) -> void { |
19 | 50 | using http = manapi::net::http::server; |
20 | 51 |
|
21 | | - manapi::before_delete bd ([] () -> void { |
22 | | - manapi::async::current()->etaskpool()->append_task([] () -> void { |
23 | | - std::cout << "hello world!\n"; |
| 52 | + std::shared_ptr<GreeterServiceImpl> service; |
| 53 | + manapi::net::wgrpc::server grpc_server; |
| 54 | + |
| 55 | + auto route = manapi::net::http::server::create(server_ctx).unwrap(); |
| 56 | + auto db = manapi::ext::pq::db::create().unwrap(); |
| 57 | + |
| 58 | + service = std::make_shared<GreeterServiceImpl>(); |
| 59 | + |
| 60 | + grpc_server = manapi::net::wgrpc::server::create (grpc_server_ctx).unwrap(); |
| 61 | + manapi::async::run([grpc_server, service] () mutable -> manapi::future<> { |
| 62 | + auto res = co_await grpc_server.config_object({ |
| 63 | + {"ssl", { |
| 64 | + {"cert", "/home/Timur/Documents/ssl/quic/cert.crt"}, |
| 65 | + {"key", "/home/Timur/Documents/ssl/quic/cert.key"}, |
| 66 | + {"verify_peer", false} |
| 67 | + }}, |
| 68 | + {"address", "localhost"}, |
| 69 | + {"port", "8080"} |
24 | 70 | }); |
25 | | - }); |
26 | 71 |
|
27 | | - manapi::async::current()->eventloop()->subscribe_finish(500, [] () -> manapi::future<> { |
| 72 | + res.log(); |
| 73 | + res.unwrap(); |
28 | 74 |
|
29 | | - std::cout << "finish it!\n"; |
30 | | - co_return; |
31 | | - }); |
| 75 | + res = co_await grpc_server.start([&] (grpc::ServerBuilder &builder) -> manapi::status { |
| 76 | + builder.RegisterService(service.get()); |
| 77 | + return manapi::status_ok(); |
| 78 | + }); |
32 | 79 |
|
33 | | - auto route = manapi::net::http::server::create(server_ctx).unwrap(); |
34 | | - auto db = manapi::ext::pq::db::create().unwrap(); |
| 80 | + res.log(); |
| 81 | + res.unwrap(); |
| 82 | + }); |
35 | 83 |
|
36 | 84 | route.GET ("/", [db] (http::req &req, http::resp &resp) mutable |
37 | 85 | -> manapi::future<> { |
|
0 commit comments