Verified Commit 74f690b3 authored by Sli's avatar Sli
Browse files

server: add runserver command and CounterList endpoint

parent 53435183
server/settings_custom.py
*.sqlite
\ No newline at end of file
*.sqlite
*.pyc
\ No newline at end of file
......@@ -7,32 +7,51 @@ import click
@click.group()
def protoc_group():
def default_group():
pass
@click.group()
def setup_group():
pass
@default_group.command(name="runserver", help="Run the server")
@click.option(
"--host", "-h", default="", type=str, help='Host to listen from (default "")'
)
@click.option(
"--port", "-p", default=50051, type=int, help="Port to listen from (default 50051)"
)
@click.option(
"--reflect",
"-r",
default=False,
type=bool,
help="Enable server reflection for debug (default False)",
)
def runserver(host, port, reflect):
from server import server
@click.group()
def random_key_group():
pass
server.serve(host, port, reflect)
@setup_group.command(name="protoc", help="Generate protoc files")
@default_group.command(name="protoc", help="Generate protoc files")
def protoc():
from subprocess import Popen
import fileinput
import time
Popen(
p = Popen(
"python -m grpc_tools.protoc -I%s --python_out=%s --grpc_python_out=%s %s"
% ("../protos", "./server/", "./server/", "../protos/com.proto"),
shell=True,
)
p.wait()
# Fix import path issues
with fileinput.FileInput("server/com_pb2_grpc.py", inplace=True) as file:
for line in file:
print(
line.replace("import com_pb2", "import server.com_pb2"), end="",
)
@setup_group.command(name="setup", help="Generate database")
@default_group.command(name="setup", help="Generate database")
@click.option("--import", "-i", default=None, help="Import initial data from json file")
@click.option(
"--schema",
......@@ -182,4 +201,4 @@ def setup(**kwargs):
if __name__ == "__main__":
click.CommandCollection(sources=[setup_group, random_key_group])()
click.CommandCollection(sources=[default_group])()
grpcio==1.28.1
grpcio-tools==1.28.1
grpcio==1.29
grpcio-tools==1.29
grpcio-reflection=1.29
SQLAlchemy==1.3.17
click==7.1.2
jsonschema
......
#!/usr/bin/env python3
# -*- coding:utf-8 -*
from concurrent import futures
import grpc
from server import db, models, com_pb2, com_pb2_grpc
class PaymentServicer(com_pb2_grpc.PaymentProtocolServicer):
"""
Communication protocol to communicate with the cashless client
"""
def Buy(self, request, context):
"""Missing associated documentation comment in .proto file"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details("Method not implemented!")
raise NotImplementedError("Method not implemented!")
def Refill(self, request, context):
"""Missing associated documentation comment in .proto file"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details("Method not implemented!")
raise NotImplementedError("Method not implemented!")
def RefoundBuying(self, request, context):
"""Missing associated documentation comment in .proto file"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details("Method not implemented!")
raise NotImplementedError("Method not implemented!")
def CancelRefilling(self, request, context):
"""Missing associated documentation comment in .proto file"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details("Method not implemented!")
raise NotImplementedError("Method not implemented!")
def Transfert(self, request, context):
"""Missing associated documentation comment in .proto file"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details("Method not implemented!")
raise NotImplementedError("Method not implemented!")
def Balance(self, request, context):
"""Missing associated documentation comment in .proto file"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details("Method not implemented!")
raise NotImplementedError("Method not implemented!")
def History(self, request, context):
"""Missing associated documentation comment in .proto file"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details("Method not implemented!")
raise NotImplementedError("Method not implemented!")
def CounterList(self, request, context):
"""
Return a list of every counter available
"""
resp = []
for counter in db.query(models.Counter).all():
resp.append(
com_pb2.CounterListReply.Counter(id=counter.id, name=counter.name)
)
return com_pb2.CounterListReply(
status=com_pb2.CounterListReply.SUCCESS, counters=resp
)
def Products(self, request, context):
"""Missing associated documentation comment in .proto file"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details("Method not implemented!")
raise NotImplementedError("Method not implemented!")
def serve(address: str, port: int, reflect: bool):
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
com_pb2_grpc.add_PaymentProtocolServicer_to_server(PaymentServicer(), server)
server.add_insecure_port("%s:%d" % (address, port))
print("Listening from %s:%d" % (address, port))
if reflect:
from grpc_reflection.v1alpha import reflection
SERVICE_NAMES = (
com_pb2.DESCRIPTOR.services_by_name["PaymentProtocol"].full_name,
reflection.SERVICE_NAME,
)
reflection.enable_server_reflection(SERVICE_NAMES, server)
print("Reflection enabled")
server.start()
server.wait_for_termination()
#!/usr/bin/env python3
# -*- coding:utf-8 -*
print("hello")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment