Skip to content

Commit 331dfe8

Browse files
Clean commit history from feature/keycloak-middleware
1 parent fe0e630 commit 331dfe8

32 files changed

+1118
-150
lines changed

docker/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ COPY frontend/ ./
1414
# Define build arguments
1515
ARG ML_SERVICE_URL
1616
ARG ML_WEB_ROOT_PATH
17+
ARG ML_KEYCLOAK_URL
1718

1819
RUN ML_SERVICE_URL=${ML_SERVICE_URL:-/services/ml-forecast} \
1920
ML_WEB_ROOT_PATH=${ML_WEB_ROOT_PATH:-/services/ml-forecast/ui} \
21+
ML_KEYCLOAK_URL=${ML_KEYCLOAK_URL:-/auth} \
2022
npm run build:prod
2123

2224
# --- Python Build Phase -------------------------------------------------------

docker/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ services:
66
context: ..
77
dockerfile: docker/Dockerfile
88
args:
9+
ML_KEYCLOAK_URL: ${ML_KEYCLOAK_URL:-/auth} # OpenRemote Keycloak URL
910
ML_SERVICE_URL: ${ML_SERVICE_URL:-/services/ml-forecast} # Url to reach the back-end service, should be the same as ML_API_ROOT_PATH
1011
ML_WEB_ROOT_PATH: ${ML_WEB_ROOT_PATH:-/services/ml-forecast/ui} # Public path for the front-end (e.g. when behind a reverse proxy)
1112
container_name: service-ml-forecast

frontend/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
<head>
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<!-- Prevent index.html from being cached -->
7+
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
8+
<meta http-equiv="Pragma" content="no-cache" />
9+
<meta http-equiv="Expires" content="0" />
610
<link rel="icon" href="<%= templateRootPath %>/assets/images/logo.svg" />
711
<style>
812
/* Global styles */

frontend/package-lock.json

Lines changed: 15 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
"type": "module",
66
"scripts": {
77
"serve": "cross-env rspack serve",
8-
"build:dev": "cross-env rspack build --mode development",
9-
"build:prod": "cross-env SERVICE_URL=${SERVICE_URL:-/services/ml-forecast} ML_WEB_ROOT_PATH=${ML_WEB_ROOT_PATH:-/services/ml-forecast/ui} rspack build --mode production",
8+
"build:prod": "cross-env ML_KEYCLOAK_URL=${ML_KEYCLOAK_URL:-/auth} ML_SERVICE_URL=${ML_SERVICE_URL:-/services/ml-forecast} ML_WEB_ROOT_PATH=${ML_WEB_ROOT_PATH:-/services/ml-forecast/ui} rspack build --mode production",
109
"build:analyze": "rspack build --mode production --analyze",
1110
"lint": "eslint && prettier . --check",
1211
"format": "prettier . --write"
@@ -16,6 +15,7 @@
1615
"@openremote/or-components": "^1.5.0",
1716
"@openremote/or-mwc-components": "^1.5.0",
1817
"@vaadin/router": "^2.0.0",
18+
"keycloak-js": "^26.1.5",
1919
"lit": "^3.2.1",
2020
"lodash": "^4.17.21",
2121
"raw-loader": "^4.0.2",

frontend/rspack.config.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const __filename = fileURLToPath(import.meta.url);
77
const __dirname = path.dirname(__filename);
88

99
const isProduction = process.env.NODE_ENV === 'production';
10-
const serviceUrl = process.env.ML_SERVICE_URL || 'http://localhost:8000'; // Default to default dev backend
10+
const serviceUrl = process.env.ML_SERVICE_URL || 'http://localhost:8000'; // Default to default service backend
11+
const keycloakUrl = process.env.ML_KEYCLOAK_URL || 'http://localhost:8081/auth'; // Default to openremote keycloak address
1112
const rootPath = process.env.ML_WEB_ROOT_PATH;
1213

1314
export default {
@@ -20,7 +21,6 @@ export default {
2021
filename: `bundle.[contenthash].js`,
2122
clean: true,
2223
path: path.resolve(__dirname, 'dist'),
23-
// prefix for the bundle, use root path or fallback to '/'
2424
publicPath: rootPath ? rootPath : '/'
2525
},
2626
resolve: {
@@ -65,7 +65,8 @@ export default {
6565
patterns: [{ from: 'assets', to: 'assets' }]
6666
}),
6767
new rspack.DefinePlugin({
68-
'process.env.ML_SERVICE_URL': JSON.stringify(serviceUrl)
68+
'process.env.ML_SERVICE_URL': JSON.stringify(serviceUrl),
69+
'process.env.ML_KEYCLOAK_URL': JSON.stringify(keycloakUrl)
6970
})
7071
],
7172
devServer: {

frontend/src/common/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
export const APP_OUTLET = document.querySelector('#outlet') as HTMLElement;
1919
export const IS_DEVELOPMENT = process.env.NODE_ENV === 'development';
2020
export const ML_SERVICE_URL = (process.env.ML_SERVICE_URL || '').replace(/\/$/, '');
21+
export const IS_EMBEDDED = window.top !== window.self;

frontend/src/common/theme.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const BASE_THEME = {
3434
};
3535

3636
/**
37+
* Setup the OR icons
38+
* Overrides the default createMdiIconSet with a function that uses the static fonts part of the build
3739
* Setup the MDI-Icons for or-icon element
3840
*/
3941
export function setupORIcons() {

frontend/src/common/util.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,37 @@ export function getRootPath() {
3232
}
3333
return '';
3434
}
35+
36+
export function isEmbedded(): boolean {
37+
return window.top !== window.self;
38+
}
39+
40+
export function setupConsoleLogging() {
41+
// Override console.log to add a prefix
42+
const originalConsoleLog = console.log;
43+
console.log = (...args) => {
44+
originalConsoleLog('[ml-forecast]', ...args);
45+
};
46+
47+
// Override console.info to add a prefix
48+
const originalConsoleInfo = console.info;
49+
console.info = (...args) => {
50+
originalConsoleInfo('[ml-forecast]', ...args);
51+
};
52+
53+
// Override console.warn to add a prefix
54+
const originalConsoleWarn = console.warn;
55+
console.warn = (...args) => {
56+
originalConsoleWarn('[ml-forecast]', ...args);
57+
};
58+
59+
// Override console.error to add a prefix
60+
const originalConsoleError = console.error;
61+
console.error = (...args) => {
62+
originalConsoleError('[ml-forecast]', ...args);
63+
};
64+
}
65+
66+
export const getRealmParam = () => {
67+
return new URLSearchParams(window.location.search).get('realm');
68+
};

frontend/src/index.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
//
1616
// SPDX-License-Identifier: AGPL-3.0-or-later
1717

18+
import { AuthService } from './services/auth-service';
19+
import { setupORIcons } from './common/theme';
20+
import { setupRouter } from './router';
21+
import { IS_EMBEDDED } from './common/constants';
22+
import { setupConsoleLogging, getRealmParam } from './common/util';
23+
1824
// Component Imports
1925
import '@openremote/or-mwc-components/or-mwc-input';
2026
import '@openremote/or-components/or-panel';
@@ -24,16 +30,22 @@ import './components/configs-table';
2430
import './components/loading-spinner';
2531
import './components/breadcrumb-nav';
2632
import './components/alert-message';
27-
import { setupORIcons } from './common/theme';
28-
import { setupRouter } from './router';
2933

30-
function init() {
34+
// Override default log statements with service prefix
35+
setupConsoleLogging();
36+
37+
async function init() {
38+
console.info('Context:', IS_EMBEDDED ? 'iframe' : 'standalone');
39+
40+
const authenticated = await AuthService.init(getRealmParam() ?? 'master');
41+
if (!authenticated) {
42+
AuthService.login();
43+
}
3144
// Setup OR icons
3245
setupORIcons();
3346

3447
// Setup the router
3548
setupRouter();
3649
}
3750

38-
// Entry point
3951
init();

0 commit comments

Comments
 (0)