Skip to content

Commit

Permalink
feat: add ability to setup base path to serve the API from a custom path
Browse files Browse the repository at this point in the history
Fixes #111
  • Loading branch information
alvarolopez committed Mar 19, 2024
1 parent 2253338 commit 6b7874a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
12 changes: 12 additions & 0 deletions deepaas/cmd/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@
The DEEPaaS API service listens on this port number for incoming
requests.
""",
),
# Allow to define base path for the API
cfg.StrOpt(
"base-path",
default="",
help="""
Base path for the API. This is useful when the API is served behind a
reverse proxy that is not at the root of the domain. For example, if
the API is served at https://example.com/deepaas, then the base path
should be set to /deepaas. Defaults to the root of the domain.
""",
),
]
Expand Down Expand Up @@ -95,6 +106,7 @@ def main():
enable_doc=CONF.doc_endpoint,
enable_train=CONF.train_endpoint,
enable_predict=CONF.predict_endpoint,
base_path=CONF.base_path,
)
web.run_app(
app,
Expand Down
53 changes: 53 additions & 0 deletions deepaas/tests/test_v2_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,59 @@ async def test_not_found(self):
self.assertEqual(404, ret.status)


class TestApiV2CusomBasePath(base.TestCase):
async def get_application(self):
app = web.Application(debug=True)
app.middlewares.append(web.normalize_path_middleware())

deepaas.model.v2.register_models(app)

v2app = v2.get_app(base_path="/custom")
app.add_subapp("/v2", v2app)

return app

def setUp(self):
super(TestApiV2CusomBasePath, self).setUp()

self.maxDiff = None

self.flags(debug=True)

def assert_ok(self, response):
self.assertIn(response.status, [200, 201])

async def test_predict_data(self):
f = six.BytesIO(b"foo")
ret = await self.client.post(
"/custom/v2/models/deepaas-test/predict/",
data={"data": (f, "foo.txt"), "parameter": 1},
)
json = await ret.json()
self.assertEqual(200, ret.status)
self.assertDictEqual(fake_responses.deepaas_test_predict, json)

async def test_train(self):
ret = await self.client.post(
"/custom/v2/models/deepaas-test/train/", data={"sleep": 0}
)
self.assertEqual(200, ret.status)
json = await ret.json()
json.pop("date")
self.assertDictEqual(fake_responses.deepaas_test_train, json)

async def test_get_metadata(self):
meta = fake_responses.models_meta

ret = await self.client.get("/custom/v2/models/")
self.assert_ok(ret)
self.assertDictEqual(meta, await ret.json())

ret = await self.client.get("/custom/v2/models/deepaas-test/")
self.assert_ok(ret)
self.assertDictEqual(meta["models"][0], await ret.json())


class TestApiV2(base.TestCase):
async def get_application(self):
app = web.Application(debug=True)
Expand Down

0 comments on commit 6b7874a

Please sign in to comment.