Skip to content

Commit

Permalink
Documentation: how define proto and service in a shared library
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Couderc committed Nov 28, 2023
1 parent 8fc89b9 commit 443471a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
31 changes: 31 additions & 0 deletions docs/how-to/define-proto-and-service-in-a-shared-library.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
.. _define-proto-and-service-in-a-shared-library:


Define proto and service in a shared library
=============================================

Description
-----------
The objective of this documentation is to demonstrate how to create a service within a library and share it among various applications.

Django, by definition, is a modular system that allows you to divide a project into several applications. This functionality is important to us,
this is why in django-socio-grpc we left the possibility for users to maintain this behavior.

This behavior can lead us to have services on an external application or library that we wish to reuse in another. Follow
this guide to importing external services to your application with django-socio-grpc.

Usage
-----
To define a shared service in a library that can be reused across different projects, you will first need to create your service with its functions.
To ensure the service can be shared, you must use the `to_root_grpc` argument in the handler and set it to "True,"
as shown in the example below.

This argument allows the proto generation to create a .proto and pb2 file in a special folder at the root of the library, with the path defined
in grpc_settings.ROOT_GRPC_FOLDER from django-socio-grpc. (by default, the name of this folder will be grpc_folder at the root of your application)

:ref:`settings<_root_grpc_folder_settings>`.

Example
-------

In this specific case, my_external_library is an external library.

.. code-block:: python
from my_external_library import ExternalService
def handler(server):
registry = AppHandlerRegistry(
app_name="my_external_library", server=server, to_root_grpc=True
)
app_registry.register(ExternalService)
By defining here, "to_root_grpc=True", when you generate your protos, they will be added in a folder with the name and path defined in your
setting grpc_settings.ROOT_GRPC_FOLDER. (by default grpc_folder at the root of your application).
2 changes: 2 additions & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ For instance, you could have a generic logging middleware that logs every gRPC r
ROOT_GRPC_FOLDER
^^^^^^^^^^^^^^^^

.. _root_grpc_folder_settings:

This setting specifies the root directory name where all the
generated proto files of external services are outputted.
More details about
Expand Down

0 comments on commit 443471a

Please sign in to comment.