Skip to content

Commit b4c8dba

Browse files
committed
servers for react apps
1 parent 104c30f commit b4c8dba

File tree

7 files changed

+84
-22
lines changed

7 files changed

+84
-22
lines changed

web-integrations/google-secure-signals/react-client-side/Dockerfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ WORKDIR /usr/src/app
55
COPY package*.json ./
66
RUN npm install
77

8-
COPY . ./
8+
COPY src ./src
9+
COPY public ./public
10+
COPY server.js ./
11+
12+
# Build the React app
13+
RUN npm run build
914

1015
EXPOSE 3044
1116
ENV PORT=3044

web-integrations/google-secure-signals/react-client-side/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"glob": "^11.1.0"
2727
},
2828
"scripts": {
29-
"start": "PORT=3044 dotenv -e ../../../.env -- react-scripts start",
29+
"start": "node server.js",
30+
"dev": "PORT=3044 dotenv -e ../../../.env -- react-scripts start",
3031
"build": "react-scripts build",
3132
"test": "react-scripts test",
3233
"eject": "react-scripts eject"

web-integrations/google-secure-signals/react-client-side/public/index.html

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
<link rel="shortcut icon" href="images/favicon.png" />
66
<meta name="viewport" content="width=device-width, initial-scale=1" />
77
<meta name="theme-color" content="#000000" />
8-
<head>
9-
<script src="%REACT_APP_UID_JS_SDK_URL%"></script>
10-
<script
11-
type="text/javascript"
12-
src="https://imasdk.googleapis.com/js/sdkloader/ima3.js"
13-
></script>
14-
</head>
15-
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
16-
8+
<script src="__UID_JS_SDK_URL_PLACEHOLDER__"></script>
9+
<script
10+
type="text/javascript"
11+
src="https://imasdk.googleapis.com/js/sdkloader/ima3.js"
12+
></script>
13+
<link rel="manifest" href="__PUBLIC_URL_PLACEHOLDER__/manifest.json" />
1714
<title>React Client-Side UID2 SDK Integration Example with Google Secure Signals</title>
1815
</head>
1916
<body>

web-integrations/google-secure-signals/react-client-side/server.js

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,75 @@ require('dotenv').config({ path: '../../../.env' });
22

33
console.log('process.env', process.env);
44

5-
const ejs = require('ejs');
6-
5+
const fs = require('fs');
6+
const path = require('path');
77
const express = require('express');
88

99
const app = express();
1010

1111
const port = process.env.PORT || 3044;
1212

13-
app.engine('.html', ejs.__express);
14-
app.set('view engine', 'html');
13+
// Healthcheck endpoint for Kubernetes probes - must be before all other routes and middleware
14+
app.get('/ops/healthcheck', (req, res) => {
15+
console.log('Healthcheck endpoint hit');
16+
res.type('text/plain');
17+
res.status(200).send('healthy\n');
18+
return; // Explicit return to prevent further processing
19+
});
20+
21+
// Helper function to serve index.html with environment variable replacement
22+
function serveIndexHtml(req, res) {
23+
// Try build directory first (production), then public (development)
24+
let indexPath = path.join(__dirname, 'build', 'index.html');
25+
if (!fs.existsSync(indexPath)) {
26+
indexPath = path.join(__dirname, 'public', 'index.html');
27+
}
28+
29+
let html = fs.readFileSync(indexPath, 'utf8');
30+
31+
// Replace environment variable placeholders
32+
const uidJsSdkUrl = process.env.REACT_APP_UID_JS_SDK_URL || 'https://cdn.integ.uidapi.com/uid2-sdk-4.0.1.js';
33+
const publicUrl = process.env.PUBLIC_URL || '';
34+
35+
// Replace placeholders (using __PLACEHOLDER__ format to avoid React build processing)
36+
html = html.replace(/__UID_JS_SDK_URL_PLACEHOLDER__/g, uidJsSdkUrl);
37+
html = html.replace(/__PUBLIC_URL_PLACEHOLDER__/g, publicUrl);
38+
39+
// Debug: log if replacement happened
40+
if (html.includes('__UID_JS_SDK_URL_PLACEHOLDER__')) {
41+
console.warn('Warning: Placeholder __UID_JS_SDK_URL_PLACEHOLDER__ was not replaced in', indexPath);
42+
}
43+
44+
res.send(html);
45+
}
46+
47+
// Route handler for index - must be before static middleware
48+
app.get('/', serveIndexHtml);
49+
50+
// Serve static files from build directory (production) or public (development)
51+
const buildPath = path.join(__dirname, 'build');
52+
const publicPath = path.join(__dirname, 'public');
1553

16-
app.use(express.static('public', { type: 'application/javascript' }));
54+
if (fs.existsSync(buildPath)) {
55+
// Production: serve from build directory (but exclude index.html to use our handler)
56+
app.use(express.static(buildPath, {
57+
index: false // Don't serve index.html automatically
58+
}));
59+
} else {
60+
// Development: serve from public directory
61+
app.use(express.static(publicPath, {
62+
index: false // Don't serve index.html automatically
63+
}));
64+
}
1765

18-
app.get('/', (req, res) => {
19-
res.render('index', {});
66+
// Catch-all handler for React Router (must be last, but excludes /ops/* routes)
67+
app.get('*', (req, res, next) => {
68+
// Don't serve index.html for /ops/* routes
69+
if (req.path.startsWith('/ops/')) {
70+
console.log('Catch-all skipping /ops/ route:', req.path);
71+
return next();
72+
}
73+
serveIndexHtml(req, res);
2074
});
2175

2276
app.listen(port, () => {

web-integrations/javascript-sdk/react-client-side/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ RUN npm install
1111
# Copy source files
1212
COPY src ./src
1313
COPY public ./public
14+
COPY server.js ./
15+
16+
# Build the React app
17+
RUN npm run build
1418

1519
# Expose port
1620
EXPOSE 3034

web-integrations/javascript-sdk/react-client-side/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"glob": "^11.1.0"
2727
},
2828
"scripts": {
29-
"start": "PORT=3034 dotenv -e ../../../.env -- react-scripts start",
29+
"start": "node server.js",
30+
"dev": "PORT=3034 dotenv -e ../../../.env -- react-scripts start",
3031
"build": "react-scripts build",
3132
"test": "react-scripts test",
3233
"eject": "react-scripts eject"

web-integrations/javascript-sdk/react-client-side/public/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
<html lang="en">
33
<head>
44
<meta charset="utf-8" />
5-
<link rel="icon" href="%PUBLIC_URL%/images/favicon.png" />
5+
<link rel="icon" href="__PUBLIC_URL_PLACEHOLDER__/images/favicon.png" />
66
<meta name="viewport" content="width=device-width, initial-scale=1" />
77
<meta name="theme-color" content="#000000" />
88
<meta
99
name="description"
1010
content="React Client-Side UID2/EUID Integration Example using JavaScript SDK"
1111
/>
12-
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
12+
<link rel="manifest" href="__PUBLIC_URL_PLACEHOLDER__/manifest.json" />
1313
<title>React Client-Side Integration Example using JavaScript SDK</title>
1414

1515
<!-- Load UID2/EUID SDK from CDN -->
16-
<script src="%REACT_APP_UID_JS_SDK_URL%"></script>
16+
<script src="__UID_JS_SDK_URL_PLACEHOLDER__"></script>
1717
</head>
1818
<body>
1919
<noscript>You need to enable JavaScript to run this app.</noscript>

0 commit comments

Comments
 (0)