Skip to content

πŸ“ˆ Open source, privacy-first web analytics β€” a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

πŸ”§πŸ‘· fixed it for you

Alper Alkanbcfe49240b37
diff --git a/.dockerignore b/.dockerignore
index 86eab01ae..6b0f76387 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -1,3 +1,4 @@
node_modules
build
-.env.example
\ No newline at end of file
+.vscode
+.env*
\ No newline at end of file
diff --git a/Dockerfile b/Dockerfile
index 8f03a407e..4c2b3607c 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,18 +1,13 @@
# Stage 1
FROM node:lts-alpine as build
ENV TZ=Etc/UTC \
- API_URL="https://example.com/api" \
REACT_APP_SELFHOSTED=true \
CHOKIDAR_USEPOLLING=true \
- GENERATE_SOURCEMAP=false \
- JQ_VERSION=1.6
-RUN wget --no-check-certificate https://github.com/stedolan/jq/releases/download/jq-${JQ_VERSION}/jq-linux64 -O /tmp/jq-linux64
-RUN cp /tmp/jq-linux64 /usr/bin/jq
-RUN chmod +x /usr/bin/jq
+ GENERATE_SOURCEMAP=false
RUN apk add --no-cache tzdata && cp /usr/share/zoneinfo/$TZ /etc/localtime
WORKDIR /app
COPY . .
-RUN jq 'to_entries | map_values({ (.key) : ("$" + .key) }) | reduce .[] as $item ({}; . + $item)' ./src/config.json > ./src/config.tmp.json && mv ./src/config.tmp.json ./src/config.json
+RUN chmod +x ./deployment/50-substitute-env-variables.sh
RUN npm install -g pnpm && pnpm install && npm run build
# Stage 2
@@ -20,10 +15,8 @@ FROM nginx:stable-alpine
ENV TZ=Etc/UTC
RUN apk add --no-cache tzdata && cp /usr/share/zoneinfo/$TZ /etc/localtime
RUN rm -rf /usr/share/nginx/html/*
-COPY --from=build /app/deployment/default.conf /etc/nginx/conf.d/default.conf
-COPY --from=build /app/deployment/start-nginx.sh /usr/bin/start-nginx.sh
-RUN chmod +x /usr/bin/start-nginx.sh
COPY --from=build /app/build /usr/share/nginx/html
-ENTRYPOINT ["/usr/bin/start-nginx.sh"]
+COPY --from=build /app/deployment/default.conf /etc/nginx/conf.d/default.conf
+COPY --from=build /app/deployment/50-substitute-env-variables.sh /docker-entrypoint.d/
EXPOSE 80
HEALTHCHECK CMD curl -f http://localhost/ || exit
diff --git a/README.md b/README.md
index 671bfac72..38474c251 100644
--- a/README.md
+++ b/README.md
@@ -27,8 +27,8 @@ To use it you just just pull `swetrix/swetrixfe` from dockerhub.
#### Docker Environment Variables
The following environment variables are available, alongside their default values.
-`API_URL` = `https://example.com/api/` | The URL of the Swetrix API
-`TZ` = `Etc/UTC` | Sets the Timezone of the Container
+`API_URL` = `https://example.com/` | The URL of the Swetrix API
+`TZ` = `Etc/UTC` | Sets the Timezone of the Container
## Bugs and security
diff --git a/deployment/50-substitute-env-variables.sh b/deployment/50-substitute-env-variables.sh
new file mode 100644
index 000000000..8078f8857
--- /dev/null
+++ b/deployment/50-substitute-env-variables.sh
@@ -0,0 +1,12 @@
+#!/bin/sh
+# WARNING: THIS FILE MUST BE SAVED WITH LF LINE ENDINGS, OTHERWISE IT WONT WORK
+set -o errexit
+set -o nounset
+set -o pipefail
+
+: "${API_URL}"
+
+cat <<EOF > /usr/share/nginx/html/env.js
+window.env = {};
+window.env.API_URL = "$API_URL";
+EOF
\ No newline at end of file
diff --git a/deployment/start-nginx.sh b/deployment/start-nginx.sh
deleted file mode 100644
index b8511eae1..000000000
--- a/deployment/start-nginx.sh
+++ /dev/null
@@ -1,7 +0,0 @@
-#!/bin/sh
-export EXISTING_VARS=$(printenv | awk -F= '{print $1}' | sed 's/^/\$/g' | paste -sd,);
-for file in /usr/share/nginx/html/static/js/*.js
-do
- cat $file | envsubst $EXISTING_VARS | tee $file > /dev/null 2>&1
-done
-nginx -g 'daemon off;'
\ No newline at end of file
diff --git a/public/env.js b/public/env.js
new file mode 100644
index 000000000..2ec3214c6
--- /dev/null
+++ b/public/env.js
@@ -0,0 +1,2 @@
+window.env = {};
+window.env.API_URL = "http://example.com/api";
diff --git a/public/index.html b/public/index.html
index d06cae8ae..f1721fb05 100644
--- a/public/index.html
+++ b/public/index.html
@@ -16,6 +16,7 @@
<meta property="og:url" content="https://swetrix.com">
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
+ <script src="%PUBLIC_URL%/env.js"></script>
<title>Swetrix Analytics</title>
</head>
<body>
diff --git a/src/api/index.js b/src/api/index.js
index 04a33592b..2b284b426 100644
--- a/src/api/index.js
+++ b/src/api/index.js
@@ -1,226 +1,258 @@
-import axios from 'axios'
-import { store } from 'redux/store'
-import Debug from 'debug'
-import _map from 'lodash/map'
-import _isEmpty from 'lodash/isEmpty'
-import { authActions } from 'redux/actions/auth'
+import axios from "axios";
+import { store } from "redux/store";
+import Debug from "debug";
+import _map from "lodash/map";
+import _isEmpty from "lodash/isEmpty";
+import { authActions } from "redux/actions/auth";
-import { getAccessToken, removeAccessToken } from 'utils/accessToken'
-import config from '../config.json'
-import { isSelfhosted } from 'redux/constants'
+import { getAccessToken, removeAccessToken } from "utils/accessToken";
+import { isSelfhosted } from "redux/constants";
-const debug = Debug('swetrix:api')
+const debug = Debug("swetrix:api");
const api = axios.create({
- baseURL: isSelfhosted ? config.API_URL : process.env.REACT_APP_API_URL,
-})
+ baseURL: isSelfhosted ? window.env.API_URL : process.env.REACT_APP_API_URL,
+});
api.interceptors.request.use(
(config) => {
- const token = getAccessToken()
+ const token = getAccessToken();
if (token) {
- config.headers['Authorization'] = `Bearer ${token}`
+ config.headers["Authorization"] = `Bearer ${token}`;
}
- return config
+ return config;
},
- error => {
- return Promise.reject(error)
+ (error) => {
+ return Promise.reject(error);
}
-)
+);
api.interceptors.response.use(
- response => response,
- error => {
+ (response) => response,
+ (error) => {
if (error.response?.data.statusCode === 401) {
- removeAccessToken()
- store.dispatch(authActions.logout())
+ removeAccessToken();
+ store.dispatch(authActions.logout());
}
- return Promise.reject(error)
+ return Promise.reject(error);
}
-)
+);
export const authMe = () =>
api
- .get('/auth/me')
- .then(response => response.data)
- .catch(error => {
- debug('%s', error)
- throw _isEmpty(error.response.data?.message) ? error.response.data : error.response.data.message
- })
+ .get("/auth/me")
+ .then((response) => response.data)
+ .catch((error) => {
+ debug("%s", error);
+ throw _isEmpty(error.response.data?.message)
+ ? error.response.data
+ : error.response.data.message;
+ });
export const login = (credentials) =>
api
- .post('/auth/login', credentials)
- .then(response => response.data)
- .catch(error => {
- debug('%s', error)
- throw _isEmpty(error.response.data?.message) ? error.response.data : error.response.data.message
- })
+ .post("/auth/login", credentials)
+ .then((response) => response.data)
+ .catch((error) => {
+ debug("%s", error);
+ throw _isEmpty(error.response.data?.message)
+ ? error.response.data
+ : error.response.data.message;
+ });
export const signup = (data) =>
api
- .post('/auth/register', data)
- .then(response => response.data)
- .catch(error => {
- const errorsArray = error.response.data.message
+ .post("/auth/register", data)
+ .then((response) => response.data)
+ .catch((error) => {
+ const errorsArray = error.response.data.message;
if (Array.isArray(errorsArray)) {
- throw errorsArray
+ throw errorsArray;
}
- throw new Error(errorsArray)
- })
+ throw new Error(errorsArray);
+ });
export const deleteUser = () =>
api
- .delete('/user')
- .then(response => response.data)
- .catch(error => {
- throw new Error(JSON.stringify(error.response.data))
- })
+ .delete("/user")
+ .then((response) => response.data)
+ .catch((error) => {
+ throw new Error(JSON.stringify(error.response.data));
+ });
export const changeUserDetails = (data) =>
api
- .put('/user', data)
- .then(response => response.data)
- .catch(error => {
- const errorsArray = error.response.data.message
+ .put("/user", data)
+ .then((response) => response.data)
+ .catch((error) => {
+ const errorsArray = error.response.data.message;
if (Array.isArray(errorsArray)) {
- throw errorsArray
+ throw errorsArray;
}
- throw new Error(errorsArray)
- })
-
+ throw new Error(errorsArray);
+ });
export const upgradePlan = (planCode) =>
- api
- .post('/user/upgrade', { planCode })
- .catch(error => {
- if (error?.response) {
- const errorsArray = error.response.data.message
- if (Array.isArray(errorsArray)) {
- throw errorsArray
- }
- throw new Error(errorsArray)
- } else {
- throw new Error(error)
+ api.post("/user/upgrade", { planCode }).catch((error) => {
+ if (error?.response) {
+ const errorsArray = error.response.data.message;
+ if (Array.isArray(errorsArray)) {
+ throw errorsArray;
}
- })
+ throw new Error(errorsArray);
+ } else {
+ throw new Error(error);
+ }
+ });
export const forgotPassword = (email) =>
api
- .post('/auth/reset-password', email)
- .then(response => response.data)
- .catch(error => {
- debug('%s', error)
- throw _isEmpty(error.response.data?.message) ? error.response.data : error.response.data.message
- })
+ .post("/auth/reset-password", email)
+ .then((response) => response.data)
+ .catch((error) => {
+ debug("%s", error);
+ throw _isEmpty(error.response.data?.message)
+ ? error.response.data
+ : error.response.data.message;
+ });
export const confirmEmail = () =>
api
- .post('/user/confirm_email')
- .then(response => response.data)
- .catch(error => {
- debug('%s', error)
- throw _isEmpty(error.response.data?.message) ? error.response.data : error.response.data.message
- })
+ .post("/user/confirm_email")
+ .then((response) => response.data)
+ .catch((error) => {
+ debug("%s", error);
+ throw _isEmpty(error.response.data?.message)
+ ? error.response.data
+ : error.response.data.message;
+ });
export const exportUserData = () =>
api
- .get('/user/export')
- .then(response => response.data)
- .catch(error => {
- debug('%s', error)
- throw _isEmpty(error.response.data?.message) ? error.response.data : error.response.data.message
- })
+ .get("/user/export")
+ .then((response) => response.data)
+ .catch((error) => {
+ debug("%s", error);
+ throw _isEmpty(error.response.data?.message)
+ ? error.response.data
+ : error.response.data.message;
+ });
export const createNewPassword = (id, password) =>
api
.post(`/auth/password-reset/${id}`, { password })
- .then(response => response.data)
- .catch(error => {
- const errorsArray = error.response.data.message
+ .then((response) => response.data)
+ .catch((error) => {
+ const errorsArray = error.response.data.message;
if (Array.isArray(errorsArray)) {
- throw errorsArray
+ throw errorsArray;
}
- throw new Error(errorsArray)
- })
+ throw new Error(errorsArray);
+ });
export const verifyEmail = ({ path, id }) =>
api
.get(`/auth/${path}/${id}`)
- .then(response => response.data)
- .catch(error => {
- debug('%s', error)
- throw _isEmpty(error.response.data?.message) ? error.response.data : error.response.data.message
- })
+ .then((response) => response.data)
+ .catch((error) => {
+ debug("%s", error);
+ throw _isEmpty(error.response.data?.message)
+ ? error.response.data
+ : error.response.data.message;
+ });
export const getProjects = () =>
api
- .get('/project')
- .then(response => response.data)
- .catch(error => {
- debug('%s', error)
- throw _isEmpty(error.response.data?.message) ? error.response.data : error.response.data.message
- })
+ .get("/project")
+ .then((response) => response.data)
+ .catch((error) => {
+ debug("%s", error);
+ throw _isEmpty(error.response.data?.message)
+ ? error.response.data
+ : error.response.data.message;
+ });
export const getProject = (pid) =>
api
.get(`/project/${pid}`)
- .then(response => response.data)
- .catch(error => {
- debug('%s', error)
- throw _isEmpty(error.response.data?.message) ? error.response.data : error.response.data.message
- })
+ .then((response) => response.data)
+ .catch((error) => {
+ debug("%s", error);
+ throw _isEmpty(error.response.data?.message)
+ ? error.response.data
+ : error.response.data.message;
+ });
export const createProject = (data) =>
api
.post(`/project`, data)
- .then(response => response.data)
- .catch(error => {
- debug('%s', error)
- throw _isEmpty(error.response.data?.message) ? error.response.data : error.response.data.message
- })
+ .then((response) => response.data)
+ .catch((error) => {
+ debug("%s", error);
+ throw _isEmpty(error.response.data?.message)
+ ? error.response.data
+ : error.response.data.message;
+ });
export const updateProject = (id, data) =>
api
.put(`/project/${id}`, data)
- .then(response => response.data)
- .catch(error => {
- debug('%s', error)
- throw _isEmpty(error.response.data?.message) ? error.response.data : error.response.data.message
- })
+ .then((response) => response.data)
+ .catch((error) => {
+ debug("%s", error);
+ throw _isEmpty(error.response.data?.message)
+ ? error.response.data
+ : error.response.data.message;
+ });
export const deleteProject = (id) =>
api
.delete(`/project/${id}`)
- .then(response => response.data)
- .catch(error => {
- debug('%s', error)
- throw _isEmpty(error.response.data?.message) ? error.response.data : error.response.data.message
- })
+ .then((response) => response.data)
+ .catch((error) => {
+ debug("%s", error);
+ throw _isEmpty(error.response.data?.message)
+ ? error.response.data
+ : error.response.data.message;
+ });
-export const getProjectData = (pid, tb = 'hour', period = '3d', from = '', to = '') =>
+export const getProjectData = (
+ pid,
+ tb = "hour",
+ period = "3d",
+ from = "",
+ to = ""
+) =>
api
- .get(`log?pid=${pid}&timeBucket=${tb}&period=${period}&from=${from}&to=${to}`)
- .then(response => response.data)
- .catch(error => {
- debug('%s', error)
- throw _isEmpty(error.response.data?.message) ? error.response.data : error.response.data.message
- })
+ .get(
+ `log?pid=${pid}&timeBucket=${tb}&period=${period}&from=${from}&to=${to}`
+ )
+ .then((response) => response.data)
+ .catch((error) => {
+ debug("%s", error);
+ throw _isEmpty(error.response.data?.message)
+ ? error.response.data
+ : error.response.data.message;
+ });
export const getOverallStats = (pids) =>
api
- .get(`log/birdseye?pids=[${_map(pids, (pid) => `"${pid}"`).join(',')}]`)
- .then(response => response.data)
- .catch(error => {
- debug('%s', error)
- throw _isEmpty(error.response.data?.message) ? error.response.data : error.response.data.message
- })
+ .get(`log/birdseye?pids=[${_map(pids, (pid) => `"${pid}"`).join(",")}]`)
+ .then((response) => response.data)
+ .catch((error) => {
+ debug("%s", error);
+ throw _isEmpty(error.response.data?.message)
+ ? error.response.data
+ : error.response.data.message;
+ });
export const getLiveVisitors = (pids) =>
api
- .get(`log/hb?pids=[${_map(pids, (pid) => `"${pid}"`).join(',')}]`)
- .then(response => response.data)
- .catch(error => {
- debug('%s', error)
- throw _isEmpty(error.response.data?.message) ? error.response.data : error.response.data.message
- })
+ .get(`log/hb?pids=[${_map(pids, (pid) => `"${pid}"`).join(",")}]`)
+ .then((response) => response.data)
+ .catch((error) => {
+ debug("%s", error);
+ throw _isEmpty(error.response.data?.message)
+ ? error.response.data
+ : error.response.data.message;
+ });
diff --git a/src/config.json b/src/config.json
deleted file mode 100644
index ea22a011c..000000000
--- a/src/config.json
+++ /dev/null
@@ -1,3 +0,0 @@
-{
- "API_URL": "$API_URL"
-}
\ No newline at end of file