diff --git a/deepaas/api/__init__.py b/deepaas/api/__init__.py index 9a12e939..e1b99e4a 100644 --- a/deepaas/api/__init__.py +++ b/deepaas/api/__init__.py @@ -14,6 +14,7 @@ # License for the specific language governing permissions and limitations # under the License. +import pathlib from aiohttp import web import aiohttp_apispec @@ -75,10 +76,22 @@ async def get_app( model.register_v2_models(APP) v2app = v2.get_app(enable_train=enable_train, enable_predict=enable_predict) - APP.add_subapp("/v2", v2app) + if base_path: + path = str(pathlib.Path(base_path) / "v2") + else: + path = "/v2" + APP.add_subapp(path, v2app) versions.register_version("stable", v2.get_version) - APP.add_routes(versions.routes) + if base_path: + # Get versions.routes, and transform them to have the base_path, as we cannot + # directly modify the routes already created and stored in the RouteTableDef + for route in versions.routes: + APP.router.add_route( + route.method, str(pathlib.Path(base_path + route.path)), route.handler + ) + else: + APP.add_routes(versions.routes) LOG.info("Serving loaded V2 models: %s", list(model.V2_MODELS.keys())) @@ -88,6 +101,9 @@ async def get_app( await m.warm() if swagger: + doc = str(pathlib.Path(base_path + doc)) + swagger = str(pathlib.Path(base_path + "/swagger.json")) + # init docs with all parameters, usual for ApiSpec aiohttp_apispec.setup_aiohttp_apispec( app=APP, @@ -101,7 +117,7 @@ async def get_app( }, basePath=base_path, version=deepaas.extract_version(), - url="/swagger.json", + url=swagger, swagger_path=doc if enable_doc else None, prefix=prefix, static_path=static_path, diff --git a/deepaas/api/versions.py b/deepaas/api/versions.py index d6241533..48df6c8d 100644 --- a/deepaas/api/versions.py +++ b/deepaas/api/versions.py @@ -25,7 +25,7 @@ routes = web.RouteTableDef() -@routes.view("/") +@routes.view("/", name="versions") class Versions(web.View): versions = {} diff --git a/deepaas/cmd/run.py b/deepaas/cmd/run.py index e1a9d0d3..5f616860 100644 --- a/deepaas/cmd/run.py +++ b/deepaas/cmd/run.py @@ -15,6 +15,7 @@ # License for the specific language governing permissions and limitations # under the License. +import pathlib import sys from aiohttp import web @@ -92,7 +93,17 @@ def main(): config.config_and_logging(sys.argv) log = oslo_log.getLogger("deepaas") - base = "http://{}:{}".format(CONF.listen_ip, CONF.listen_port) + base_path = CONF.base_path + + if base_path and not base_path.startswith("/"): + print("Base path should start with a '/'.", file=sys.stderr) + sys.exit(1) + elif base_path: + base_path = str(pathlib.Path(base_path)) + else: + base_path = "" + + base = "http://{}:{}{}".format(CONF.listen_ip, CONF.listen_port, base_path) spec = "{}/swagger.json".format(base) docs = "{}/api".format(base) v2 = "{}/v2".format(base)