Project: userver (App Definition and Development / Streaming & Messaging)
File: grpc_middleware_service.md

Make sure that you can compile and run core tests and read a basic example @ref scripts/docs/en/userver/tutorial/hello_service.md. In this example, you will write an authentication middleware for both 'GreeterService' and 'GreeterClient' of the basic grpc_service. See @ref scripts/docs/en/userver/tutorial/grpc_service.md Generate and link with necessary libraries: @snippet samples/grpcmiddlewareservice/CMakeLists.txt gRPC middleware sample - CMake Client middleware will add metadata to every `GreeterClient` call. Derive `Middleware` and `MiddlewareFactory` from the respective base class: @snippet samples/grpcmiddlewareservice/src/middlewares/client/middleware.hpp gRPC middleware sample - Middleware and MiddlewareFactory declaration Handle method of `Middleware` does the actual work: @snippet samples/grpcmiddlewareservice/src/middlewares/client/middleware.cpp gRPC middleware sample - Middleware and MiddlewareFactory implementation Then, wrap it into component, which just stores `MiddlewareFactory`: @snippet samples/grpcmiddlewareservice/src/middlewares/client/component.hpp gRPC middleware sample - Middleware component declaration Lastly, add this component to the static config: ``` components_manager: components: grpc-auth-client: ``` And connect it with `ClientFactory`: @snippet samples/grpcmiddlewareservice/static_config.yaml gRPC middleware sample - static config client middleware Server middleware, in its turn, will validate metadata that comes with an rpc. Everything is the same as it is for client middleware, except there is no factory and the component stores the middleware itself: @snippet samples/grpcmiddlewareservice/src/middlewares/server/middleware.hpp gRPC middleware sample - Middleware declaration Handle method of `Middleware` does the actual work: @snippet samples/grpcmiddlewareservice/src/middlewares/server/middleware.cpp gRPC middleware sample - Middleware implementation Respective component: @snippet samples/grpcmiddlewareservice/src/middlewares/server/component.hpp gRPC middleware sample - Middleware component declaration Lastly, add this component to the static config: ``` components_manager: components: grpc-auth-server: ``` And connect it with `Service`: @snippet samples/grpcmiddlewareservice/static_config.yaml gRPC middleware sample - static config server middleware Finally, register components and start the server. @snippet samples/grpcmiddlewareservice/src/main.cpp gRPC middleware sample - components registration To build the sample, execute the following build steps at the userver root directory: ``` mkdir build_release cd build_release cmake -DCMAKEBUILDTYPE=Release .. make userver-samples-grpcmiddlewareservice ``` The sample could be started by running `make start-userver-samples-grpcmiddlewareservice`. The command would invoke @ref scripts/docs/en/userver/functional_testing.md "testsuite start target" that sets proper paths in the configuration files and starts the service. To start the service manually run `./samples/grpcmiddlewareservice/userver-samples-grpcmiddlewareservice -c </path/to/static_config.yaml>`. The service is available locally at port 8091 (as per project `static_config.yaml`). To implement @ref scripts/docs/en/userver/functional_testing.md "Functional tests" for the service some preparational steps should be done. First of all, import the required modules and add the required pytest_userver.plugins.grpc pytest plugin: @snippet samples/grpcmiddlewareservice/tests/conftest.py Prepare modules To mock the gRPC server provide a hook for the static config to change the endpoint: @snippet samples/grpcmiddlewareservice/tests/conftest.py Prepare configs Write the mocking fixtures using @ref pytestuserver.plugins.grpcmockserver.grpcmockserver "grpcmockserver": @snippet samples/grpcmiddlewareservice/tests/conftest.py Prepare server mock To do the gRPC requests write a client fixture using @ref pytestuserver.plugins.grpcclient.grpcchannel "grpcchannel": @snippet samples/grpcmiddlewareservice/tests/conftest.py grpc client Use it to do gRPC requests to the service: @snippet samples/grpcmiddlewareservice/tests/test_middlewares.py grpc authentication tests See the full example at: @ref samples/grpcmiddlewareservice/src/middlewares/client/middleware.hpp @ref samples/grpcmiddlewareservice/src/middlewares/client/middleware.cpp @ref samples/grpcmiddlewareservice/src/middlewares/client/component.hpp @ref samples/grpcmiddlewareservice/src/middlewares/client/component.cpp @ref samples/grpcmiddlewareservice/src/middlewares/server/middleware.hpp @ref samples/grpcmiddlewareservice/src/middlewares/server/middleware.cpp @ref samples/grpcmiddlewareservice/src/middlewares/server/component.hpp @ref samples/grpcmiddlewareservice/src/middlewares/server/component.cpp @ref samples/grpcmiddlewareservice/src/middlewares/auth.hpp @ref samples/grpcmiddlewareservice/src/middlewares/auth.cpp @ref samples/grpcmiddlewareservice/src/main.cpp @ref samples/grpcmiddlewareservice/proto/samples/greeter.proto @ref samples/grpcmiddlewareservice/static_config.yaml @ref samples/grpcmiddlewareservice/tests/conftest.py @ref samples/grpcmiddlewareservice/tests/test_middlewares.py @ref samples/grpcmiddlewareservice/CMakeLists.txt @htmlonly <div class="bottom-nav"> @endhtmlonly @ref scripts/docs/en/userver/tutorial/grpcservice.md | @ref scripts/docs/en/userver/tutorial/postgresservice.md @htmlonly </div> @endhtmlonly @example samples/grpcmiddlewareservice/src/middlewares/client/middleware.hpp @example samples/grpcmiddlewareservice/src/middlewares/client/middleware.cpp @example samples/grpcmiddlewareservice/src/middlewares/client/component.hpp @example samples/grpcmiddlewareservice/src/middlewares/client/component.cpp @example samples/grpcmiddlewareservice/src/middlewares/server/middleware.hpp @example samples/grpcmiddlewareservice/src/middlewares/server/middleware.cpp @example samples/grpcmiddlewareservice/src/middlewares/server/component.hpp @example samples/grpcmiddlewareservice/src/middlewares/server/component.cpp @example samples/grpcmiddlewareservice/src/middlewares/auth.hpp @example samples/grpcmiddlewareservice/src/middlewares/auth.cpp @example samples/grpcmiddlewareservice/src/main.cpp @example samples/grpcmiddlewareservice/proto/samples/greeter.proto @example samples/grpcmiddlewareservice/static_config.yaml @example samples/grpcmiddlewareservice/tests/conftest.py @example samples/grpcmiddlewareservice/tests/test_middlewares.py @example samples/grpcmiddlewareservice/CMakeLists.txt