switch back to uwsgi

This commit is contained in:
William Luke 2021-11-18 15:38:20 +03:00
parent 18efb9fcf3
commit a379065891
4 changed files with 111 additions and 10 deletions

View File

@ -4,10 +4,23 @@ import logging
import os import os
import sys import sys
import uuid import uuid
from os import path
from urllib.parse import parse_qsl, urlparse from urllib.parse import parse_qsl, urlparse
import cic_eth.cli import cic_eth.cli
import redis import redis
from cic_eth.api.api_task import Api
from cic_eth.server.UWSGIOpenAPIRequest import UWSGIOpenAPIRequest
from openapi_core import create_spec
from openapi_core.validation.request.datatypes import OpenAPIRequest
from openapi_core.validation.request.validators import RequestValidator
from openapi_spec_validator.schemas import read_yaml_file
from werkzeug.wrappers import Request
spec_dict = read_yaml_file(path.join(path.dirname(
__file__), '../../server/openapi/server.yaml'))
spec = create_spec(spec_dict)
logging.basicConfig(level=logging.WARNING) logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger() logg = logging.getLogger()
@ -21,16 +34,35 @@ args = argparser.parse_args()
config = cic_eth.cli.Config.from_args(args, arg_flags, local_arg_flags) config = cic_eth.cli.Config.from_args(args, arg_flags, local_arg_flags)
celery_app = cic_eth.cli.CeleryApp.from_config(config) celery_app = cic_eth.cli.CeleryApp.from_config(config)
from cic_eth.api.api_task import Api
# uwsgi application # uwsgi application
def application(env, start_response): def application(env, start_response):
parsed_url = urlparse(env['REQUEST_URI']) # /api print(spec)
request = Request(env)
oAPIRequest = UWSGIOpenAPIRequest(request)
validator = RequestValidator(spec)
result = validator.validate(oAPIRequest)
# raise errors if request invalid
# result.raise_for_errors()
if result.errors:
# get list of errors
json_data = json.dumps(list(map(lambda e: str(e), result.errors)))
content = json_data.encode('utf-8')
headers = []
headers.append(('Content-Length', str(len(content))),)
headers.append(('Access-Control-Allow-Origin', '*',))
headers.append(('Content-Type', 'application/json',))
start_response('400 Invalid Request', headers)
return [content]
parsed_url = urlparse(env.get('REQUEST_URI')) # /api
path = parsed_url.path path = parsed_url.path
parsed_url.params
params = dict(parse_qsl(parsed_url.query)) params = dict(parse_qsl(parsed_url.query))
request_method = env['REQUEST_METHOD'] request_method = env.get('REQUEST_METHOD')
chain_spec = config.get('CHAIN_SPEC') chain_spec = config.get('CHAIN_SPEC')
redis_host = config.get('REDIS_HOST') redis_host = config.get('REDIS_HOST')
redis_port = config.get('REDIS_PORT') redis_port = config.get('REDIS_PORT')
@ -86,8 +118,11 @@ def application(env, start_response):
m = json.loads(o['data']) m = json.loads(o['data'])
print(m['result']) print(m['result'])
data = {"path": path, data = {
"request_method": request_method, "result": m} "path": path,
"request_method": request_method,
"result": m
}
json_data = json.dumps(data) json_data = json.dumps(data)
content = json_data.encode('utf-8') content = json_data.encode('utf-8')
headers = [] headers = []

View File

@ -0,0 +1,63 @@
"""OpenAPI core contrib requests requests module"""
from urllib.parse import parse_qs
from urllib.parse import urlparse
from requests import Request
from werkzeug.datastructures import Headers
from werkzeug.datastructures import ImmutableMultiDict
from openapi_core.validation.request.datatypes import OpenAPIRequest
from openapi_core.validation.request.datatypes import RequestParameters
class UWSGIOpenAPIRequestFactory:
@classmethod
def create(cls, request):
"""
Converts a requests request to an OpenAPI one
Internally converts to a `PreparedRequest` first to parse the exact
payload being sent
"""
if isinstance(request, Request):
request = request.prepare()
# Method
method = request.method.lower()
# Preparing a request formats the URL with params, strip them out again
o = urlparse(request.url)
params = parse_qs(o.query)
# extract the URL without query parameters
url = o._replace(query=None).geturl()
# Order matters because all python requests issued from a session
# include Accept */* which does not necessarily match the content type
mimetype = request.headers.get("Content-Type") or request.headers.get(
"Accept"
)
# Headers - request.headers is not an instance of Headers
# which is expected
header = Headers(dict(request.headers))
# Body
# TODO: figure out if request._body_position is relevant
body = request.get_data()
# Path gets deduced by path finder against spec
parameters = RequestParameters(
query=ImmutableMultiDict(params),
header=header,
cookie=request.cookies,
)
return OpenAPIRequest(
full_url_pattern=url,
method=method,
parameters=parameters,
body=body,
mimetype=mimetype,
)
UWSGIOpenAPIRequest = UWSGIOpenAPIRequestFactory.create

View File

@ -3,9 +3,10 @@ chainlib-eth>=0.0.10a16,<0.1.0
semver==2.13.0 semver==2.13.0
crypto-dev-signer>=0.4.15rc2,<0.5.0 crypto-dev-signer>=0.4.15rc2,<0.5.0
uwsgi==2.0.19.1 uwsgi==2.0.19.1
graphene>=2.0
flask>=2.0.2 flask>=2.0.2
Flask-GraphQL>=2.0.1
connexion[swagger-ui] == 2.6.0 connexion[swagger-ui] == 2.6.0
python_dateutil == 2.6.0 python_dateutil == 2.6.0
setuptools >= 21.0.0 setuptools >= 21.0.0
openapi-core >= 0.14.2
openapi-spec-validator >= 0.3.1
Werkzeug>=2.0.2

View File

@ -218,8 +218,10 @@ services:
set -a set -a
if [[ -f /tmp/cic/config/env_reset ]]; then source /tmp/cic/config/env_reset; fi if [[ -f /tmp/cic/config/env_reset ]]; then source /tmp/cic/config/env_reset; fi
set +a set +a
pip install -r test_requirements.txt /usr/local/bin/uwsgi \
tail -F ./requirements.txt --wsgi-file /root/cic_eth/runnable/daemons/server.py \
--http :5000 \
--pyargv "-vv"
cic-eth-tracker: cic-eth-tracker:
image: ${DEV_DOCKER_REGISTRY:-registry.gitlab.com/grassrootseconomics/cic-internal-integration}/cic-eth:${TAG:-latest} image: ${DEV_DOCKER_REGISTRY:-registry.gitlab.com/grassrootseconomics/cic-internal-integration}/cic-eth:${TAG:-latest}