Skip to content

Commit

Permalink
fix: allow users to setup base_path for serving under a different prefix
Browse files Browse the repository at this point in the history
Fixes #111
  • Loading branch information
alvarolopez committed Aug 8, 2024
1 parent 71e8230 commit 39fa910
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
22 changes: 19 additions & 3 deletions deepaas/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()))

Expand All @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion deepaas/api/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
routes = web.RouteTableDef()


@routes.view("/")
@routes.view("/", name="versions")
class Versions(web.View):
versions = {}

Expand Down
13 changes: 12 additions & 1 deletion deepaas/cmd/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# License for the specific language governing permissions and limitations
# under the License.

import pathlib
import sys

from aiohttp import web
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 39fa910

Please sign in to comment.