Skip to content

Commit

Permalink
✨🚧Trying to support hosting
Browse files Browse the repository at this point in the history
  • Loading branch information
carefree0910 committed Apr 17, 2023
1 parent e6e589f commit 767422c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 deletions.
3 changes: 2 additions & 1 deletion cfdraw/.web/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
VITE_CFDRAW_FE_PORT = ${CFDRAW_FE_PORT}
VITE_CFDRAW_BE_PORT = ${CFDRAW_BE_PORT}
VITE_CFDRAW_BE_PORT = ${CFDRAW_BE_PORT}
VITE_CFDRAW_API_URL = ${CFDRAW_API_URL}
12 changes: 8 additions & 4 deletions cfdraw/.web/src/requests/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ import { useInceptors } from "./interceptors";
// cannot use `useMemo` here
export function useAPI<T extends APISources>(source: T): APIs[T] {
const timeout = pythonStore.globalSettings.timeout ?? 300000;
let backendPort = import.meta.env.VITE_CFDRAW_BE_PORT;
if (!backendPort) {
backendPort = 8123;
let baseURL = import.meta.env.VITE_CFDRAW_API_URL;
if (!baseURL) {
let backendPort = import.meta.env.VITE_CFDRAW_BE_PORT;
if (!backendPort) {
backendPort = 8123;
}
baseURL = `http://localhost:${backendPort}`;
}
const apis = {
_python: axios.create({ baseURL: `http://localhost:${backendPort}`, timeout }),
_python: axios.create({ baseURL, timeout }),
};
useInceptors(apis);
return apis[source];
Expand Down
8 changes: 3 additions & 5 deletions cfdraw/app/endpoints/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,9 @@ async def fetch_project(uid: str) -> ProjectModel: # type: ignore
if node.renderParams is None:
raise ValueError("`ImageNode` should have `renderParams`")
src = node.renderParams.src
if (
src
and isinstance(src, str)
and src.startswith(app.config.api_host)
):
# TODO: this kind of transformation should be included in the
# migration stage, not runtime stage. Will be fixed in the future
if src and isinstance(src, str) and src.startswith("http://"):
pivot = constants.UPLOAD_IMAGE_FOLDER_NAME
_, path = src.split(pivot)
api_url = app.config.api_url
Expand Down
5 changes: 4 additions & 1 deletion cfdraw/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Optional
from pathlib import Path
from importlib import import_module
from dataclasses import dataclass
Expand All @@ -14,6 +15,7 @@ class Config:
frontend_port: str = constants.FRONTEND_PORT
# api
backend_port: str = constants.BACKEND_PORT
backend_hosting_url: Optional[str] = None # this must be provided for hosting
# upload
upload_root: str = str(constants.UPLOAD_ROOT)
# misc
Expand All @@ -25,7 +27,8 @@ def prod(self) -> bool:

@property
def api_url(self) -> str:
return f"http://localhost:{self.backend_port}"
api_url = self.backend_hosting_url or f"http://localhost:{self.backend_port}"
return api_url.rstrip("/").rstrip("\\")

@property
def upload_root_path(self) -> Path:
Expand Down
7 changes: 3 additions & 4 deletions cfdraw/utils/exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@

def setup_frontend() -> None:
config = get_config()
fe_port = config.frontend_port
be_port = config.backend_port
prerequisites.install_frontend_packages()
console.rule("[bold green]Launching App")
os.environ["CFDRAW_FE_PORT"] = fe_port
os.environ["CFDRAW_BE_PORT"] = be_port
os.environ["CFDRAW_FE_PORT"] = config.frontend_port
os.environ["CFDRAW_BE_PORT"] = config.backend_port
os.environ["CFDRAW_API_URL"] = config.api_url


def run_frontend() -> None:
Expand Down

0 comments on commit 767422c

Please sign in to comment.