Skip to content

Provides microservices for the NEXT REST endpoints. Utilises python for the gRPC servers. Utilises PostgreSQL for data persistence. Mock node program is included to demonstrate client RPC calls from a node environment such as NEXT.

Notifications You must be signed in to change notification settings

cs261-2022-group22/grpc-backend

Repository files navigation

gRPC Backend

Original Author:

Varun Chodanker

Disclaimer:

This is written according to my interpretation of gRPC. As I have no experience with gRPC prior to this project, there may be errors contained within this document. Please see https://grpc.io/docs/ for the official documentation which should serve as the canonical source regarding this topic.

Project File Sturcture

common/                     # The Git submodule, holding common proto files
compiled_protos/            # Where the compiled protobuf files are located
services/                   # Directory for each microservices
   ...  /*Service.py        # Service wrapper code, contains the gRPC class wrapper and the
                            # begin/endServe functions.
   ...  /*ServiceImpl.py    # Service implementation code, the actual logic being executed is here
                            # "Impl" for Implementation.

*_server.py                 # The entry point of each service.

gRPC

  • gRPC defines services. These services provide remote procedural calls (RPCs). A server corresponds to a particular service and implements its RPCs so that clients can send call requests for these and receive responses. For each RPC, the request and response type is declared. This specifies the parameters of the call requests, from the client, and the variables that are returned in responses, from the server. Services are defined with .proto files. gRPC is beneficial due to its high performance.

It's a drop-in replacement of Google's protobuf library, it provides:

  • Better type-hinting
  • Cleaner generated source files.
  • and finally, async support (but trapped by an upstream Python bug)

The generated code goes to compiled_protos/, as stated below:

Python gRPC Server

  • See gRPC QuickStart for how supporting code files are generated. This also illustrates how Python gRPC servers are built.
  • Create a virtual environment and activate it.
  • Set up according to requirements.txt by installing the required dependencies.
    • pip install -r ./requirements.txt
  • The supporting code is generated from the root directory of the project by using the following command:
    • python -m grpc_tools.protoc -I./common/ --python_betterproto_out=./compiled_protos/ ./common/*.proto
  • Start the the server so that the gRPC calls, of its corresponding service, can be serviced.
    • python <proto_name>_server.py
  • Hit Ctrl-C to stop the server.

Node.js Javascript GRPC Client

  • See https://grpc.io/docs/languages/node/quickstart/ for how Node gRPC clients are built.
  • Install dependencies according to package.json.
    npm install
  • After ensuring the server is already running, run the client. It will make gRPC calls requests that are received by the server. The server runs these and returns a response. The client can perform appropriate actions based on the response.
    node <filename>

PostgreSQL Database

Password Handling

  • The passwords of accounts are hashed with salts before being stored in the database. Then, passwords are compared with the hashes to check that they match.
  • In Python, bcrypt is used to generate the salt and hash the password with it. Then it is also used to check whether a given password matches with the stored hash - to allow the user to be logged in. See https://pypi.org/project/bcrypt/ and https://docs.python.org/3/library/stdtypes.html.

About

Provides microservices for the NEXT REST endpoints. Utilises python for the gRPC servers. Utilises PostgreSQL for data persistence. Mock node program is included to demonstrate client RPC calls from a node environment such as NEXT.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •