Esempio n. 1
0
def api(title, version):
    log = Log.get('api')

    middlewares = [Negotiation_Middleware()]

    if Arg_Reader.db.auth:
        log.notice('JWT authentication enabled')
        middlewares.append(
            Falcon_Auth_Middleware(
                JWT_Auth_Backend(
                    user_loader=lambda token: {'user': token},
                    secret_key=Arg_Reader.db.auth_secret_key,
                    auth_header_prefix=Arg_Reader.db.auth_header_prefix),
                exempt_routes=['/api/doc', '/api/doc/swagger.json']))
    else:
        log.notice('JWT authentication disabled')

    if Arg_Reader.db.https:
        log.notice('Force to use HTTPS instead of HTTP')
        middlewares.append(RequireHTTPS())
    else:
        log.notice('HTTPS not set')

    if Arg_Reader.db.apm_enabled:
        log.notice('Elastic APM enabled')
        middlewares.append(
            Elastic_Apm_Middleware(service_name='lcp-apm',
                                   server_url=Arg_Reader.db.apm_server))
    else:
        log.notice('Elastic APM disabled')

    instance = API(middleware=middlewares)

    media_handlers = {
        falcon.MEDIA_JSON:
        JSON_Handler(loads=loads,
                     dumps=partial(dumps, ensure_ascii=False, sort_keys=True)),
        falcon.MEDIA_MSGPACK:
        Message_Pack_Handler(),
        falcon.MEDIA_XML:
        XML_Handler(),
        falcon.MEDIA_YAML:
        YAML_Handler()
    }
    instance.req_options.media_handlers.update(media_handlers)
    instance.resp_options.media_handlers.update(media_handlers)

    instance.add_error_handler(*Bad_Request_Handler.get())
    instance.add_error_handler(*Internal_Server_Error_Handler.get())
    instance.add_error_handler(*Unsupported_Media_Type_Handler.get())

    api_spec = Spec(api=instance, title=title, version=version)
    routes(api=instance, spec=api_spec.get())
    falcon_api_doc(instance,
                   config_path='./swagger/schema.json',
                   url_prefix='/api/doc',
                   title='API doc')
    api_spec.write()

    return instance
def simulate():
    app = falcon.API(middleware=[RequireHTTPS()])
    app.add_route(_TEST_PATH, testing.TestResource())

    def simulate(protocol, headers=None):
        env = testing.create_environ(
            method='GET',
            scheme=protocol,
            path=_TEST_PATH,
            query_string='',
            headers=headers,
        )

        srmock = testing.StartResponseMock()
        iterable = app(env, srmock)

        return testing.Result(iterable, srmock.status, srmock.headers)

    return simulate
Esempio n. 3
0
from falcon_require_https import RequireHTTPS
from random import randint
from matchzoo import engine

logger = logging.getLogger(__name__)

pkg_dir = os.path.join(config.WORKBUDDY_DIR, 'src')
sys.path.append(pkg_dir)

from officeanswers.preprocess import build_document_embeddings
from officeanswers.model import get_inference_model
from officeanswers.search import build_search_index
from officeanswers.util import Config

api = hug.API(__name__)
api.http.add_middleware(RequireHTTPS())


_PROJECT = 'grounded-gizmo-187521'

logger.info("Build search index...")

config_path = config.WORKBUDDY_CONFIG
if not config_path or not os.path.exists(config_path):
    raise IOError("Config file not or envionmental variable not set." +
                  "Make sure OA_CONFIG is set to config file path" +
                  f"Path supplied {config_path}")

base_dir = config.WORKBUDDY_DIR

config = Config()
Esempio n. 4
0
"""An example of using a middleware to require HTTPS connections.
    requires https://github.com/falconry/falcon-require-https to be installed via
    pip install falcon-require-https
"""
import hug
from falcon_require_https import RequireHTTPS

hug.API(__name__).http.add_middleware(RequireHTTPS())


@hug.get()
def my_endpoint():
    return "Success!"