Skip to content

Commit a9e4117

Browse files
authored
Merge pull request #58 from Visual-Regression-Tracker/84-url-config
REACT_APP_API_URL configuration fixed
2 parents 1aa27f0 + 4a7d1ce commit a9e4117

File tree

15 files changed

+110
-13
lines changed

15 files changed

+110
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
# production
1212
/build
13+
env-config.js
1314

1415
# misc
1516
.DS_Store

Dockerfile

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,20 @@ RUN npm run build
1717
### STAGE 2: Run ###
1818
FROM nginx:alpine
1919

20-
COPY /nginx/default.conf /etc/nginx/conf.d/default.conf
20+
RUN apk add --no-cache bash
21+
22+
COPY /nginx /etc/nginx/conf.d
2123
RUN chown -R nginx /etc/nginx /var/run /run
2224

2325
EXPOSE 8080
2426

25-
COPY --from=builder /app/build /usr/share/nginx/html
27+
COPY --from=builder /app/build /usr/share/nginx/html
28+
29+
# Copy .env file and shell script to container
30+
WORKDIR /usr/share/nginx/html
31+
COPY .env .
32+
COPY ./env.sh .
33+
RUN chmod +x env.sh
34+
35+
# Start Nginx server
36+
CMD ["/bin/bash", "-c", "/usr/share/nginx/html/env.sh && nginx -g \"daemon off;\""]

env.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
# (c) https://github.com/kunokdev/cra-runtime-environment-variables/blob/master/env.sh
3+
4+
# Recreate config file
5+
rm -rf ./env-config.js
6+
touch ./env-config.js
7+
8+
# Add assignment
9+
echo "window._env_ = {" >> ./env-config.js
10+
11+
# Read each line in .env file
12+
# Each line represents key=value pairs
13+
while read -r line || [[ -n "$line" ]];
14+
do
15+
# Split env variables by character `=`
16+
if printf '%s\n' "$line" | grep -q -e '='; then
17+
varname=$(printf '%s\n' "$line" | sed -e 's/=.*//')
18+
varvalue=$(printf '%s\n' "$line" | sed -e 's/^[^=]*=//')
19+
fi
20+
21+
# Read value of current variable if exists as Environment variable
22+
value=$(printf '%s\n' "${!varname}")
23+
# Otherwise use value from .env file
24+
[[ -z $value ]] && value=${varvalue}
25+
26+
# Append configuration property to JS file
27+
echo " $varname: \"$value\"," >> ./env-config.js
28+
done < .env
29+
30+
echo "}" >> ./env-config.js

nginx/gzip.conf

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Enable Gzip compressed.
2+
gzip on;
3+
4+
# Enable compression both for HTTP/1.0 and HTTP/1.1 (required for CloudFront).
5+
gzip_http_version 1.0;
6+
7+
# Compression level (1-9).
8+
# 5 is a perfect compromise between size and cpu usage, offering about
9+
# 75% reduction for most ascii files (almost identical to level 9).
10+
gzip_comp_level 5;
11+
12+
# Don't compress anything that's already small and unlikely to shrink much
13+
# if at all (the default is 20 bytes, which is bad as that usually leads to
14+
# larger files after gzipping).
15+
gzip_min_length 256;
16+
17+
# Compress data even for clients that are connecting to us via proxies,
18+
# identified by the "Via" header (required for CloudFront).
19+
gzip_proxied any;
20+
21+
# Tell proxies to cache both the gzipped and regular version of a resource
22+
# whenever the client's Accept-Encoding capabilities header varies;
23+
# Avoids the issue where a non-gzip capable client (which is extremely rare
24+
# today) would display gibberish if their proxy gave them the gzipped version.
25+
gzip_vary on;
26+
27+
# Compress all output labeled with one of the following MIME-types.
28+
gzip_types
29+
application/atom+xml
30+
application/javascript
31+
application/json
32+
application/rss+xml
33+
application/vnd.ms-fontobject
34+
application/x-font-ttf
35+
application/x-web-app-manifest+json
36+
application/xhtml+xml
37+
application/xml
38+
font/opentype
39+
image/svg+xml
40+
image/x-icon
41+
text/css
42+
text/plain
43+
text/x-component;
44+
# text/html is always compressed by HttpGzipModule

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"use-image": "^1.0.6"
2828
},
2929
"scripts": {
30+
"dev": "chmod +x ./env.sh && ./env.sh && cp env-config.js ./public/ && react-scripts start",
3031
"start": "react-scripts start",
3132
"build": "react-scripts build",
3233
"test": "react-scripts test"

public/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<meta name="theme-color" content="#000000" />
1111

1212
<title>Visual Regression Tracker</title>
13+
14+
<script src="%PUBLIC_URL%/env-config.js"></script>
1315
</head>
1416
<body>
1517
<noscript>You need to enable JavaScript to run this app.</noscript>

src/_config/api.config.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/_config/env.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const API_URL = window._env_.REACT_APP_API_URL;
2+
3+
declare global {
4+
interface Window {
5+
_env_: {
6+
REACT_APP_API_URL: string;
7+
};
8+
}
9+
}

src/contexts/socket.context.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from "./build.context";
99
import { Build, TestRun } from "../types";
1010
import { useTestRunDispatch, addTestRun } from "./testRun.context";
11+
import { API_URL } from "../_config/env.config";
1112

1213
interface IConnectAction {
1314
type: "connect";
@@ -100,13 +101,12 @@ function useSocketDispatch() {
100101
}
101102

102103
function connect(dispatch: Dispatch) {
103-
const apiUrl = process.env.REACT_APP_API_URL;
104-
if (apiUrl) {
105-
const socket = socketIOClient(apiUrl);
104+
if (API_URL) {
105+
const socket = socketIOClient(API_URL);
106106
dispatch({ type: "connect", payload: socket });
107107
console.log("Socket connected");
108108
} else {
109-
console.log("API url is not provided: " + apiUrl);
109+
console.log("API url is not provided");
110110
}
111111
}
112112

src/services/builds.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Build } from "../types";
22
import { handleResponse, authHeader } from "../_helpers/service.helpers";
3-
import { API_URL } from "../_config/api.config";
3+
import { API_URL } from "../_config/env.config";
44

55
const ENDPOINT_URL = "/builds";
66

0 commit comments

Comments
 (0)