Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
support socketio base path (#167)
Browse files Browse the repository at this point in the history
* support socketio base path

* Fix base path

* bump imjoy-rpc version

* Fix duplicated test name
  • Loading branch information
oeway authored May 24, 2021
1 parent ea87cb7 commit 9881e78
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 14 deletions.
2 changes: 1 addition & 1 deletion imjoy/VERSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.11.14"
"version": "0.11.15"
}
6 changes: 6 additions & 0 deletions imjoy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ def parse_cmd_line(args=None):
parser.add_argument(
"-v", "--version", action="version", version="%(prog)s " + __version__
)
parser.add_argument(
"--base-path",
type=str,
default="/",
help="the base path for the server",
)

opt = parser.parse_args(args=args)

Expand Down
24 changes: 16 additions & 8 deletions imjoy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ async def disconnect(sid):
del all_sessions[sid]


def create_application(allow_origins) -> FastAPI:
def create_application(allow_origins, base_path) -> FastAPI:
"""Set up the server application."""
# pylint: disable=unused-variable, protected-access

Expand All @@ -218,7 +218,7 @@ def create_application(allow_origins) -> FastAPI:
allow_headers=["Content-Type", "Authorization"],
)

@app.get("/")
@app.get(base_path)
async def root():
return {
"name": "ImJoy Core Server",
Expand All @@ -234,13 +234,13 @@ async def root():

def setup_socketio_server(
app: FastAPI,
mount_location: str = "/",
socketio_path: str = "socket.io",
base_path: str = "/",
allow_origins: Union[str, list] = "*",
) -> None:
"""Set up the socketio server."""
socketio_path = base_path.rstrip("/") + "/socket.io"

@app.get("/liveness")
@app.get(base_path.rstrip("/") + "/liveness")
async def liveness(req: Request) -> JSONResponse:
try:
await sio.emit("liveness")
Expand All @@ -254,7 +254,7 @@ async def liveness(req: Request) -> JSONResponse:

_app = socketio.ASGIApp(socketio_server=sio, socketio_path=socketio_path)

app.mount(mount_location, _app)
app.mount("/", _app)
app.sio = sio
core_api = CoreInterface()
initialize_socketio(sio, core_api)
Expand All @@ -268,8 +268,10 @@ def start_server(args):
allow_origin = args.allow_origin.split(",")
else:
allow_origin = env.get("ALLOW_ORIGINS", "*").split(",")
application = create_application(allow_origin)
setup_socketio_server(application, allow_origins=allow_origin)
application = create_application(allow_origin, args.base_path)
setup_socketio_server(
application, base_path=args.base_path, allow_origins=allow_origin
)
if args.host == "127.0.0.1" or args.host == "localhost":
print(
"***Note: If you want to enable access from another host,\
Expand Down Expand Up @@ -300,5 +302,11 @@ def start_server(args):
default="*",
help="origins for the socketio server",
)
parser.add_argument(
"--base-path",
type=str,
default="/",
help="the base path for the server",
)
opt = parser.parse_args()
start_server(opt)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fastapi==0.65.1
imjoy-jupyter-extension==0.2.17
imjoy-rpc==0.3.11
imjoy-rpc==0.3.12
ipykernel==5.5.5
jupyter==1.0.0
numpy==1.19.5 # needs to stay compatible with latest tensorflow
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
# pylint: disable=unused-import
import google.colab.output # noqa: F401

REQUIREMENTS = ["numpy", "imjoy-rpc>=0.3.0", "imjoy-elfinder"]
REQUIREMENTS = ["numpy", "imjoy-rpc>=0.3.12", "imjoy-elfinder"]
except ImportError:
REQUIREMENTS = [
"numpy",
"imjoy-rpc>=0.3.11",
"imjoy-rpc>=0.3.12",
"pydantic[email]>=1.8.1",
"typing-extensions>=3.7.4.3", # required by pydantic
"python-dotenv>=0.17.0",
Expand Down
54 changes: 52 additions & 2 deletions tests/test_imjoy_engine_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
pytestmark = pytest.mark.asyncio

PORT = 38283
PORT2 = 38223
SERVER_URL = f"http://127.0.0.1:{PORT}"


Expand All @@ -31,8 +32,36 @@ def socketio_server_fixture():
break
except RequestException:
pass
timeout -= 1
time.sleep(1)
timeout -= 0.1
time.sleep(0.1)
yield

proc.terminate()


@pytest.fixture(name="socketio_subpath_server")
def socketio_subpath_server_fixture():
"""Start server (under /my/engine) as test fixture and tear down after test."""
with subprocess.Popen(
[
sys.executable,
"-m",
"imjoy.server",
f"--port={PORT2}",
"--base-path=/my/engine",
]
) as proc:

timeout = 10
while timeout > 0:
try:
response = requests.get(f"http://127.0.0.1:{PORT2}/my/engine/liveness")
if response.ok:
break
except RequestException:
pass
timeout -= 0.1
time.sleep(0.1)
yield

proc.terminate()
Expand Down Expand Up @@ -87,6 +116,27 @@ def test_plugin_runner(socketio_server):
assert "echo: a message" in output


def test_plugin_runner_subpath(socketio_subpath_server):
"""Test the plugin runner with subpath server."""
with subprocess.Popen(
[
sys.executable,
"-m",
"imjoy.runner",
f"--server-url=http://127.0.0.1:{PORT2}/my/engine",
"--quit-on-ready",
os.path.join(os.path.dirname(__file__), "example_plugin.py"),
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
) as proc:
out, err = proc.communicate()
assert err.decode("utf8") == ""
output = out.decode("utf8")
assert "Generated token: imjoy@" in output
assert "echo: a message" in output


async def test_plugin_runner_workspace(socketio_server):
"""Test the plugin runner with workspace."""
api = await connect_to_server(
Expand Down

0 comments on commit 9881e78

Please # to comment.