Skip to content

Commit cddd240

Browse files
chore: more changes related to reverse proxy
1 parent 56e6a81 commit cddd240

File tree

4 files changed

+43
-35
lines changed

4 files changed

+43
-35
lines changed

Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ WORKDIR /app
1313
ENV NODE_ENV production
1414
ENV NEXT_TELEMETRY_DISABLED 1
1515
ENV PORT 8080
16-
ENV API_URI "/"
16+
ENV API_URI ""
1717
ENV API_VERSION 1
18+
ENV REVERSE_PROXY false
1819

1920
COPY --from=modules /etc/passwd /etc/passwd
2021
COPY --from=modules /etc/group /etc/group
@@ -27,5 +28,5 @@ RUN apk update && apk upgrade && apk add --no-cache curl ca-certificates bash
2728
USER appuser
2829
EXPOSE 8080
2930

30-
CMD ["/bin/bash", "-c", "REACT_APP_API_URI=$API_URI REACT_APP_API_VERSION=$API_VERSION npx react-inject-env set && \
31+
CMD ["/bin/bash", "-c", "REACT_APP_API_URI=$API_URI REACT_APP_API_VERSION=$API_VERSION REACT_APP_REVERSE_PROXY=$REVERSE_PROXY npx react-inject-env set && \
3132
npm run server-prod"]

Dockerfile.Caddy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ WORKDIR /app
1414
ENV NODE_ENV production
1515
ENV NEXT_TELEMETRY_DISABLED 1
1616
ENV PORT 8080
17-
ENV API_URI http://localhost:3000
17+
ENV API_URI ""
1818
ENV API_VERSION 1
19+
ENV REVERSE_PROXY true
1920

2021
COPY --from=modules /etc/passwd /etc/passwd
2122
COPY --from=modules /etc/group /etc/group
@@ -32,5 +33,5 @@ chmod -R 700 /var/log/caddy/
3233

3334
USER appuser
3435
EXPOSE 8080 3000
35-
CMD ["/bin/bash", "-c", "REACT_APP_API_URI=$API_URI REACT_APP_API_VERSION=$API_VERSION npx react-inject-env set && \
36+
CMD ["/bin/bash", "-c", "REACT_APP_API_URI=$API_URI REACT_APP_API_VERSION=$API_VERSION REACT_APP_REVERSE_PROXY=$REVERSE_PROXY npx react-inject-env set && \
3637
caddy run --config /etc/caddy/Caddyfile --adapter caddyfile"]

src/.DS_Store

-8 KB
Binary file not shown.

src/App.js

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,28 @@ import { getCounter, postCounter } from "./utilities/requests";
1212
import Menu from "./components/Animated/Menu/Menu";
1313

1414
function App() {
15-
const countapiKey = "f3dceeba-1841-42cf-b76c-26e9026dc0cf";
15+
const countapiKey = "f3dceeba-1841-42cf-b76c-26e9026dc0cf"; // Yes, it's an API key but it's not a secret. It's used to identify the counter.
1616
const countapiNamespace = "spectrocloud.com";
1717
const [isLogoVisible, setIsLogoVisible] = useState(false);
1818
const [clickCount, setClickCount] = useState(0);
1919
const [connected, setConnected] = useState(false);
2020
const [firstLoad, setFirstLoad] = useState(true);
2121
let API_URI = env.REACT_APP_API_URI;
2222
let API_VERSION = env.REACT_APP_API_VERSION;
23-
24-
// This is the default value for the API_URI env variable
25-
// In this scenario, either the global API is used OR local storage is used
26-
if (API_URI === "/") {
23+
let REVERSE_PROXY = env.REACT_APP_REVERSE_PROXY;
24+
25+
if (API_URI == "undefined") {
2726
API_URI = ""
2827
}
2928

30-
// If no API_URI is provided, then assume the user is running the API locally
31-
if (API_URI === "") {
32-
API_URI = "http://localhost:3000"
33-
}
29+
if (REVERSE_PROXY) {
30+
API_URI = "http://0.0.0.0:3000"
31+
}
3432

3533
if (API_VERSION === "" || API_VERSION == "undefined") {
3634
API_VERSION = 1
3735
}
36+
3837
useEffect(() => {
3938
// Checks if internet is connected by attempting to load an image
4039
const image = new Image();
@@ -47,18 +46,8 @@ function App() {
4746
}, [connected])
4847

4948
async function loadCount() {
50-
// If not connected to the internet, fallback to using local storage for count
51-
// Otherwise, use Count API
52-
if (connected && !API_URI || API_URI === "undefined") {
53-
try {
54-
const count = await countapi.get(countapiNamespace, countapiKey);
55-
setClickCount(count?.value || clickCount);
56-
} catch (error) {
57-
setClickCount(clickCount + 1);
58-
localStorage.setItem("clickCount", clickCount);
59-
}
60-
}
61-
49+
50+
// If an API URI is provided, use that to get the count
6251
if (API_URI) {
6352
try {
6453
const databaseCount = await getCounter(API_URI, API_VERSION);
@@ -69,6 +58,20 @@ function App() {
6958
}
7059
}
7160

61+
62+
// If connected to the internet, and no API URI is provided then use the Global Counter API.
63+
// Fallback to using local storage for count if the Global Counter API is unavailable.
64+
if (connected && !API_URI || API_URI === "undefined") {
65+
try {
66+
const count = await countapi.get(countapiNamespace, countapiKey);
67+
setClickCount(count?.value || clickCount);
68+
} catch (error) {
69+
setClickCount(clickCount + 1);
70+
localStorage.setItem("clickCount", clickCount);
71+
}
72+
}
73+
74+
// If not connected to the internet, and no API URI is provided then use local storage for count.
7275
if (!connected && !API_URI || API_URI === "undefined") {
7376
const count = localStorage.getItem("clickCount");
7477
setClickCount(parseInt(count) || 0);
@@ -77,25 +80,28 @@ function App() {
7780
}
7881

7982
async function countUp() {
80-
if (connected && !API_URI || API_URI === "undefined") {
81-
try {
82-
await countapi.update(countapiNamespace, countapiKey, +1);
83-
await loadCount();
84-
} catch(error) {
85-
console.log(error);
86-
}
87-
}
8883

84+
// If an API URI is provided, use that to update the count
8985
if (API_URI) {
9086
try {
9187
const databaseCount = await postCounter(API_URI, API_VERSION);
9288
setClickCount(databaseCount);
9389
} catch (error) {
9490
alert(`Error: Unable to connect to database on ${API_URI}. Please try again later. 😢`)
9591
}
96-
9792
}
98-
93+
94+
// If connected to the internet, and no API URI is provided then use the Global Counter API.
95+
if (connected && !API_URI || API_URI === "undefined") {
96+
try {
97+
await countapi.update(countapiNamespace, countapiKey, +1);
98+
await loadCount();
99+
} catch(error) {
100+
console.log(error);
101+
}
102+
}
103+
104+
// If not connected to the internet, and no API URI is provided then use local storage for count.
99105
if(!connected && !API_URI || API_URI === "undefined") {
100106
setClickCount(clickCount + 1);
101107
localStorage.setItem("clickCount", clickCount);

0 commit comments

Comments
 (0)