Exemple #1
0
def main(global_config, **settings):
    config = Configurator(settings=settings)
    config.include("cornice")
    config.add_cornice_service(api_items)
    config.add_view(app, route_name='app')
    config.add_route('app', '/*subpath')
    return config.make_wsgi_app()
Exemple #2
0
def main(global_config, **settings):
    # settings may be defined in the global configuration
    settings = dict([(key, value) for key, value in global_config.items()
                     if key not in ('__file__', 'here')], **settings)

    engine = sqlalchemy_engine_from_config(settings)
    Session.configure(bind=engine)
    Base.metadata.bind = engine

    # XXX: Basic authentication and authorization omitted purposefully,
    # unneeded
    config = Configurator(settings=settings,
                          authentication_policy=None,
                          authorization_policy=None)

    # Disable Cornice's built-in exception handling
    config.add_settings(handle_exceptions=False)
    config.include('cornice')
    config.include('pyramid_jinja2')
    config.include('pyramid_tm')
    config.include('demo.api.common.pyramid.assets')
    config.include(add_routes)
    config.include(add_views)
    config.include(add_request_methods)
    config.include(add_jinja2)
    config.include(add_renderers)

    config.add_assets_mapping(
        json.load(pkg_resources.resource_stream(__name__,
                                                'static/assets.json')))

    config.add_cornice_deserializer('application/json',
                                    extract_json_data_factory())
    config.add_cornice_deserializer('application/json-patch+json',
                                    extract_json_data_factory())

    for service in create_cornice_services(path_prefix='/api'):
        config.add_cornice_service(service)

    return config.make_wsgi_app()
Exemple #3
0
def main(_global_config, **settings):

    print("*****running main of API****")
    load_cors_origins(settings, 'cors_policy.origins')

    config = Configurator(settings=settings)

    attach_pymongo(config, settings)

    config.add_request_method(get_json, 'json', reify=True)
    renderer = JSONRenderer(serializer=lambda v, **_kw: json.dumps(
        v, default=json_datetime_part, sort_keys=True, indent=4))

    config.add_renderer('json', renderer)

    config.include("cornice")
    # config.scan("adios_db_api.views")

    # Attempt to set up the rest by hand -- scanning is not working when
    # bundled by py2app
    print("adding all the API rest services")
    from .views import oil
    config.add_cornice_service(oil.oil_api)

    from .views.label import label_api
    config.add_cornice_service(label_api)

    from .views.product_types import product_types_api
    config.add_cornice_service(product_types_api)

    from .views.capabilities import capabilities_api
    config.add_cornice_service(capabilities_api)

    # from .views.query import query_api
    # config.add_cornice_service(query_api)

    user_docs_dir = settings.get('user_docs_dir')

    config.add_static_view(name='/docs',
                           path=user_docs_dir,
                           cache_max_age=12 * 3600)

    # add static file serving if we are running the standalone
    client_path = settings.get('client_path')
    if client_path:
        print("adding client code to serve:\n", client_path)
        if not os.path.isdir(client_path):
            raise ValueError(f"client path: {client_path} does not exist")

        # this would be cleaner, but couldn't get it to work with Ember
        config.add_static_view(name='/', path=client_path)

        # mike's suggestion for how to serve up the index.html page
        print("about to set up home view")

        def home(request):
            """serving up index.html"""
            return FileResponse(client_path + "/index.html", request)

        # this way has not been tested.
        config.add_route("home", "/")
        config.add_view(home, route_name="home")

        # attempt to put the client files in a sub-dir
        #  to avoid clashes with the API
        # config.add_static_view(name='client',
        #                        path=client_path)

    else:
        # serve up the basic hello message at the root
        print("no client_path: not serving any static files")
        config.add_route('home', '/')
        config.add_view(about, route_name='home')

    # setup the about endpoint
    config.add_route('about', '/about')
    config.add_view(about, route_name='about')
    config.add_notfound_view(default_not_found_response, append_slash=True)

    return config.make_wsgi_app()