diff --git a/app/config.py b/app/config.py index e93bd6b..d5b2a85 100644 --- a/app/config.py +++ b/app/config.py @@ -8,7 +8,7 @@ APPFACTORY_ENV = "/opt/appfactory/config/appfactory.env" DEFAULT_BACKUP_DIR = "/opt/appfactory/backups" DEFAULT_GITEA_ORG = "appfactory" -DEFAULT_APPFACTORY_HOST = "192.168.66.130" +INTERNAL_GITEA_URL = "http://appfactory-gitea:3000" PORTAL_PREFIX = "/portal" @@ -58,6 +58,47 @@ def get_google_redirect_uri() -> str: return "" +def get_gitea_public_url() -> str: + return (read_env_value("GITEA_URL", "") or read_env_value("GITEA_ROOT_URL", "")).rstrip("/") + + +def get_gitea_server_url() -> str: + return get_gitea_public_url() or INTERNAL_GITEA_URL + + +def get_gitea_redirect_uri() -> str: + redirect_uri = read_env_value("GITEA_OAUTH_REDIRECT_URI", "").strip() + if redirect_uri: + return redirect_uri + + public_url = get_portal_public_url() + if public_url: + return f"{public_url}/auth/gitea/callback" + + return "" + + +def is_gitea_oauth_button_enabled() -> bool: + return bool( + get_gitea_public_url() + and read_env_value("GITEA_OAUTH_CLIENT_ID", "") + and read_env_value("GITEA_OAUTH_CLIENT_SECRET", "") + and get_gitea_redirect_uri() + ) + + +def get_appfactory_host(request_host: str = "") -> str: + configured_host = read_env_value("APPFACTORY_HOST", "").strip() + if configured_host: + return configured_host + + public_url = get_portal_public_url() + if public_url: + return public_url.split("://", 1)[-1].split("/", 1)[0].split(":", 1)[0] + + return (request_host or "").split(":", 1)[0] + + def get_auth_domain_readiness() -> dict[str, bool]: google_enabled = read_env_bool("GOOGLE_OAUTH_ENABLED") google_client_id = read_env_value("GOOGLE_CLIENT_ID", "") diff --git a/app/routes/apps.py b/app/routes/apps.py index 58f8be5..35d2dc8 100644 --- a/app/routes/apps.py +++ b/app/routes/apps.py @@ -7,11 +7,12 @@ from fastapi.responses import HTMLResponse, RedirectResponse from ..auth import require_user from ..config import ( - DEFAULT_APPFACTORY_HOST, DEFAULT_GITEA_ORG, DELETE_APP_SCRIPT, DEPLOY_SCRIPT, GENERATE_COMPOSE_SCRIPT, + get_appfactory_host, + get_gitea_public_url, read_env_value, ) from ..db.apps import ( @@ -206,9 +207,9 @@ def apps_page( offset = (page_number - 1) * DEFAULT_PAGE_SIZE apps = apps[offset : offset + DEFAULT_PAGE_SIZE] - gitea_url = read_env_value("GITEA_URL", "") + gitea_url = get_gitea_public_url() gitea_org = read_env_value("GITEA_ORG", DEFAULT_GITEA_ORG) - host = read_env_value("APPFACTORY_HOST", DEFAULT_APPFACTORY_HOST) + host = get_appfactory_host(request.url.hostname or "") rows = "" @@ -226,8 +227,8 @@ def apps_page( memory_label = memory or "výchozí" cpus_label = cpus or "výchozí" - http_clone = html.escape(f"git clone {gitea_url}/{gitea_org}/{app_id}.git") - ssh_clone = html.escape(f"git clone ssh://git@{host}:2222/{gitea_org}/{app_id}.git") + http_clone = html.escape(f"git clone {gitea_url}/{gitea_org}/{app_id}.git") if gitea_url else "" + ssh_clone = html.escape(f"git clone ssh://git@{host}:2222/{gitea_org}/{app_id}.git") if host else "" rows += f"""
{html.escape(error)}
' + gitea_login_html = "" + if is_gitea_oauth_button_enabled(): + gitea_login_html = """ + + """ google_login_html = "" if is_google_oauth_button_enabled(): google_login_html = """ @@ -365,10 +380,7 @@ def _render_login(error: str | None = None) -> str:Doporučené přihlášení je přes Gitea. Lokální účet slouží pouze jako nouzový administrátorský přístup.
{error_html} - - + {gitea_login_html} {google_login_html}