[P UI] Make base url configurable (#5577)

* made url_base configurable for P UI

* fixed test for configurable base url
This commit is contained in:
Matthias Mair 2023-09-20 19:39:05 -04:00 committed by GitHub
parent 6c5b9e0108
commit 8432297d3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 24 additions and 13 deletions

View File

@ -64,7 +64,7 @@ class AuthRequiredMiddleware(object):
elif request.path_info.startswith('/accounts/'):
authorized = True
elif request.path_info.startswith('/platform/') or request.path_info.startswith('/assets/') or request.path_info == '/platform':
elif request.path_info.startswith(f'/{settings.PUI_URL_BASE}/') or request.path_info.startswith('/assets/') or request.path_info == f'/{settings.PUI_URL_BASE}':
authorized = True
elif 'Authorization' in request.headers.keys() or 'authorization' in request.headers.keys():

View File

@ -606,11 +606,6 @@ DATABASES = {
REMOTE_LOGIN = get_boolean_setting('INVENTREE_REMOTE_LOGIN', 'remote_login_enabled', False)
REMOTE_LOGIN_HEADER = get_setting('INVENTREE_REMOTE_LOGIN_HEADER', 'remote_login_header', 'REMOTE_USER')
# Magic login django-sesame
SESAME_MAX_AGE = 300
# LOGIN_REDIRECT_URL = "/platform/logged-in/"
LOGIN_REDIRECT_URL = "/index/"
# sentry.io integration for error reporting
SENTRY_ENABLED = get_boolean_setting('INVENTREE_SENTRY_ENABLED', 'sentry_enabled', False)
@ -981,6 +976,7 @@ CUSTOM_SPLASH = get_custom_file('INVENTREE_CUSTOM_SPLASH', 'customize.splash', '
CUSTOMIZE = get_setting('INVENTREE_CUSTOMIZE', 'customize', {})
# Frontend settings
PUI_URL_BASE = get_setting('INVENTREE_PUI_URL_BASE', 'pui_url_base', 'platform')
PUI_SETTINGS = get_setting("INVENTREE_PUI_SETTINGS", "pui_settings", {})
if DEBUG:
@ -1008,3 +1004,8 @@ if CUSTOM_FLAGS:
else:
logger.info(f"Custom flags: {CUSTOM_FLAGS}")
FLAGS.update(CUSTOM_FLAGS)
# Magic login django-sesame
SESAME_MAX_AGE = 300
# LOGIN_REDIRECT_URL = f"/{PUI_URL_BASE}/logged-in/"
LOGIN_REDIRECT_URL = "/index/"

View File

@ -1211,6 +1211,6 @@ class MagicLoginTest(InvenTreeTestCase):
self.assertEqual(resp.url, '/index/')
# Note: 2023-08-08 - This test has been changed because "platform UI" is not generally available yet
# TODO: In the future, the URL comparison will need to be reverted
# self.assertEqual(resp.url, '/platform/logged-in/')
# self.assertEqual(resp.url, f'/{settings.PUI_URL_BASE}/logged-in/')
# And we should be logged in again
self.assertEqual(resp.wsgi_request.user, self.user)

View File

@ -250,6 +250,8 @@ remote_login_header: HTTP_REMOTE_USER
# show_server_selector: false
# sentry_dsn: https://84f0c3ea90c64e5092e2bf5dfe325725@o1047628.ingest.sentry.io/4504160008273920
# environment: development
# Base URL for serving Platform UI
# pui_url_base: 'platform'
# Custom flags
# InvenTree uses django-flags; read more in their docs at https://cfpb.github.io/django-flags/conditions/

View File

@ -10,7 +10,11 @@ from django.utils.safestring import mark_safe
logger = getLogger("InvenTree")
register = template.Library()
PUI_SETTINGS = json.dumps(settings.PUI_SETTINGS)
PUI_DEFAULTS = {
'url_base': settings.PUI_URL_BASE,
}
PUI_DEFAULTS.update(getattr(settings, 'PUI_SETTINGS', {}))
PUI_SETTINGS = json.dumps(PUI_DEFAULTS)
@register.simple_tag

View File

@ -20,12 +20,12 @@ spa_view = ensure_csrf_cookie(TemplateView.as_view(template_name="web/index.html
urlpatterns = [
path('platform/', include([
path(f'{settings.PUI_URL_BASE}/', include([
path("assets/<path:path>", RedirectAssetView.as_view()),
re_path(r"^(?P<path>.*)/$", spa_view),
path("set-password?uid=<uid>&token=<token>", spa_view, name="password_reset_confirm"),
path("", spa_view),]
)),
re_path(r'^platform', spa_view, name='platform'),
path(settings.PUI_URL_BASE, spa_view, name='platform'),
path("assets/<path:path>", RedirectAssetView.as_view()),
]

View File

@ -14,6 +14,7 @@ declare global {
server_list: HostList;
default_server: string;
show_server_selector: boolean;
url_base: string;
sentry_dsn?: string;
environment?: string;
};
@ -55,13 +56,15 @@ if (window.INVENTREE_SETTINGS.sentry_dsn) {
});
}
export const url_base = window.INVENTREE_SETTINGS.url_base || 'platform';
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
// Redirect to /platform if on /
// Redirect to base url if on /
if (window.location.pathname === '/') {
window.location.replace('/platform');
window.location.replace(`/${url_base}`);
}

View File

@ -5,6 +5,7 @@ import { BrowserRouter } from 'react-router-dom';
import { queryClient, setApiDefaults } from '../App';
import { BaseContext } from '../contexts/BaseContext';
import { defaultHostList } from '../defaults/defaultHostList';
import { url_base } from '../main';
import { routes } from '../router';
import { useApiState } from '../states/ApiState';
import { useLocalState } from '../states/LocalState';
@ -35,7 +36,7 @@ export default function DesktopAppView() {
return (
<BaseContext>
<QueryClientProvider client={queryClient}>
<BrowserRouter basename="platform">{routes}</BrowserRouter>
<BrowserRouter basename={url_base}>{routes}</BrowserRouter>
</QueryClientProvider>
</BaseContext>
);