-
Notifications
You must be signed in to change notification settings - Fork 1
/
processor_server.py
56 lines (42 loc) · 1.62 KB
/
processor_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
Новая версия модуля запуска API RPC Flask сервера processor
"""
from typing import Tuple
from flask import Flask
from flask import Response as FlaskResponse
from flask import request as flask_request
from jsonrpcserver import dispatch
from jsonrpcserver.methods import global_methods as methods
from nlab.db import create_sessionmaker
from sqlalchemy.engine.base import Engine
import settings
from components import ApiWorld
HOST = '0.0.0.0' # TODO: Наверное, хосту тоже место в файле settings
PORT = settings.PROCESSOR_PORT
POSTGRES_PREFIX = settings.POSTGRES_PREFIX
app = Flask(__name__)
@app.route("/", methods=["POST"])
def index():
"""
Обработка всех запросов к api процессора по протоколу jsonrpcserver'a
"""
request = flask_request.get_data().decode()
response = dispatch(
request=request, methods=methods, debug=True, basic_logging=True
)
return FlaskResponse(
str(response), response.http_status, mimetype="application/json"
)
def get_server_params(app=app, pg_prefix=POSTGRES_PREFIX,
methods=methods) -> Tuple[Flask, ApiWorld, Engine]:
"""
Функция создает rpc-сервер и отдает его реквизиты.
"""
sessionmaker = create_sessionmaker(env_prefix=pg_prefix)
api = ApiWorld(sessionmaker)
api.install(methods)
database_engine = sessionmaker().session.bind
return app, api, database_engine
if __name__ == "__main__":
app, _, _ = get_server_params()
app.run(host=HOST, port=PORT)