예제 #1
0
def create_web_app(base_folder, spec_file, additional_middlewares=None):
    # create the default mapping of the operationId to the implementation code in handlers
    opmap = __create_default_operation_mapping(Path(base_folder / spec_file))

    # generate a version 3 of the API documentation
    router = SwaggerRouter(
        swagger_ui='/apidoc/',
        version_ui=3,  # forces the use of version 3 by default
        search_dirs=[base_folder],
        default_validate=True,
    )

    # add automatic jsonification of the models located in generated code
    jsonify.singleton = Jsonify(indent=3, ensure_ascii=False)
    jsonify.singleton.add_converter(Model, lambda o: o.to_dict(), score=0)

    middlewares = [jsonify, __handle_errors]
    if additional_middlewares:
        middlewares.extend(additional_middlewares)
    # create the web application using the API
    app = web.Application(
        router=router,
        middlewares=middlewares,
    )
    router.set_cors(app,
                    domains='*',
                    headers=((hdrs.ACCESS_CONTROL_EXPOSE_HEADERS,
                              hdrs.AUTHORIZATION), ))

    # Include our specifications in a router,
    # is now available in the swagger-ui to the address http://localhost:8080/swagger/?spec=v1
    router.include(
        spec=Path(base_folder / spec_file),
        operationId_mapping=opmap,
        name='v0',  # name to access in swagger-ui,
        basePath=
        "/v0"  # BUG: in apiset with openapi 3.0.0 [Github bug entry](https://github.com/aamalev/aiohttp_apiset/issues/45)
    )

    return app
예제 #2
0
파일: main.py 프로젝트: hh-h/aiohttp_apiset
def main():
    router = SwaggerRouter(
        swagger_ui='/swagger/',
        search_dirs=[BASE],
    )

    app = web.Application(
        router=router,
        middlewares=[jsonify],
    )
    router.set_cors(app,
                    domains='*',
                    headers=((hdrs.ACCESS_CONTROL_EXPOSE_HEADERS,
                              hdrs.AUTHORIZATION), ))

    # Include our specifications in a router,
    # is now available in the swagger-ui to the address http://localhost:8080/swagger/?spec=v1
    router.include(
        spec='swagger.yaml',
        operationId_mapping=opmap,
        name='v1',  # name to access in swagger-ui
    )

    web.run_app(app)
예제 #3
0
from pathlib import Path

from aiohttp import web, hdrs
from aiohttp_apiset import SwaggerRouter
from aiohttp_apiset.middlewares import jsonify

BASE = Path(__file__).parent

router = SwaggerRouter(
    swagger_ui='/swagger/',
    search_dirs=[BASE],
)
router.add_post('/api/v1/doc/{doc_id:\d+}', handler='handlers.set_document')

app = web.Application(
    router=router,
    middlewares=[jsonify],
)
router.set_cors(app,
                domains='*',
                headers=((hdrs.ACCESS_CONTROL_EXPOSE_HEADERS,
                          hdrs.AUTHORIZATION), ))

# Include our specifications in a router
router.include(spec='swagger.yaml', basePath='/api/v1')

if __name__ == '__main__':
    # now available swagger-ui to the address http://localhost:8080/swagger/
    web.run_app(app)