Skip to content

Commit

Permalink
Allow bare metal deployment
Browse files Browse the repository at this point in the history
The UI is currently hardcoded to support two environments: a development
deployment from `localhost:3000` to `localhost:8000`, and the API explicitly
allows `http://localhost:3000` as a cross-site origin; and an OpenShift
deployment where cross-site scripting is unnecessary as the cluster API
reverse proxy hides the port numbers.

Partly for more general testing and deployment, but specifically because the
RHEL AI InstructLab project requires CPT dashboard access now before our code
has been integrated into the production OpenShift deployment, it's convenient
to support a third "bare metal" mode where the containerized UI and backend
are hosted at ports 3000 and 8000 on some host (e.g., in the RDU3 Performance
Lab).

For this, the UI needs to recognize that a non-`localhost` `window.location`
with a `3000` port needs to call the API at port `8000` on the same host (for
our "bare metal" deployment) while an empty port indicates we're using the
OpenShift API reverse proxy routing.

Similarly, the backend code cross-site scripting protection needs to allow
port 3000 from the same host as a valid origin.
  • Loading branch information
dbutenhof committed Nov 20, 2024
1 parent e0daae0 commit 7ece5ed
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
9 changes: 7 additions & 2 deletions backend/app/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import socket
import typing

from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
import orjson

Expand All @@ -17,9 +18,11 @@ def render(self, content: typing.Any) -> bytes:

origins = [
"http://localhost:3000",
"localhost:3000"
"localhost:3000",
f"http://{socket.gethostname()}:3000",
]


app = FastAPI(default_response_class=ORJSONResponse,
docs_url="/docs",
redoc_url=None,
Expand All @@ -46,6 +49,8 @@ def render(self, content: typing.Any) -> bytes:

@app.middleware('http')
async def some_middleware(request: Request, call_next):
print(f"origin: {origins}, request: {request.headers}")
print(f"{request.app.user_middleware}")
if request.url.path in routes_to_reroute:
request.scope['path'] = '/docs'
headers = dict(request.scope['headers'])
Expand Down
20 changes: 16 additions & 4 deletions frontend/src/utils/apiConstants.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
export const getUrl = () => {
const { hostname, protocol } = window.location;
return hostname === "localhost"
? "http://0.0.0.0:8000"
: `${protocol}//${hostname}`;
const { protocol, hostname, port } = window.location;

// Try to support three environments. If we're running a development
// environment on localhost, map to the API port. If we're running on
// the product OpenShift environment with an API reverse proxy, we
// won't see a port, and the API calls will be routed correctly without
// a port. If we're running a "product" deployment outside of OpenShift
// and without a routing reverse proxy, we'll see the origin port 3000
// and route to the same protocol/host with port 8000.
if (hostname === "localhost") {
return "http://0.0.0.0:8000";
} else if (port === "3000") {
return `${protocol}//${hostname}:8000`;
} else {
return `${protocol}//${hostname}`;
}
};

export const BASE_URL = getUrl();
Expand Down

0 comments on commit 7ece5ed

Please sign in to comment.