Skip to content

Commit

Permalink
Feature/multidomain (#500)
Browse files Browse the repository at this point in the history
* multidomain

* multidomain

* multidomain

* multidomain

---------

Co-authored-by: Mateusz Wojczal <mateusz@wojczal.com>
  • Loading branch information
qunabu and Mateusz Wojczal authored Sep 30, 2024
1 parent 9d54fd4 commit 95c75a5
Show file tree
Hide file tree
Showing 5 changed files with 9,085 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ node_modules
build
dist
.idea
yarn.lock
!yarn.lock
.env.production
.DS_Store
*/**/.DS_Store
Expand Down
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#test with `docker build -t foo . && docker run -p 8080:80 -it foo`
FROM node:20-buster AS base

ARG APP_VERSION="xxx"
Expand All @@ -7,12 +8,14 @@ RUN yarn install --network-timeout 1000000000 && yarn run build && cp dist/inde
RUN echo $APP_VERSION >> dist/version.html


FROM httpd:latest AS httpd
FROM php:apache AS httpd
ENV API_URL="http://localhost:1000"
ENV SENTRYDSN=""
ENV YBUG_ID=""
ENV BASENAME=""
ENV ROUTING_TYPE="HashRouter"

COPY entrypoint.sh /usr/local/bin/httpd-foreground
COPY --from=base /home/node/app/dist /usr/local/apache2/htdocs/
ENTRYPOINT /usr/local/bin/httpd-foreground
COPY --from=base /home/node/app/dist /var/www/html
COPY config/php/index.php /var/www/html/index.php
52 changes: 52 additions & 0 deletions config/php/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

$content = file_get_contents("index.html");

// check variables first

$domains = [];

if (isset($_ENV['MULTI_DOMAINS'])) {
foreach (explode(",", $_ENV['MULTI_DOMAINS']) as $domain) {
$domain = trim($domain);
$domainKey = str_replace(['.', '-'], '_', strtoupper($domain));
foreach (getenv() as $key => $value) {
if (str_starts_with($key, $domainKey)) {
$variableKey = str_replace($domainKey . '_', '', $key);
$domains[$domain][$variableKey] = $value;
}
}
}
}

if (is_file(dirname(__FILE__) . "/env_config.php")) {
include_once "env_config.php";
}

if (key_exists($_SERVER['HTTP_HOST'], $domains) || key_exists($_SERVER['SERVER_NAME'], $domains)) {

$setup = key_exists($_SERVER['HTTP_HOST'], $domains) ? $domains[$_SERVER['HTTP_HOST']] : $domains[$_SERVER['SERVER_NAME']];

if (isset($setup)) {
if (isset($setup['VITE_APP_API_URL'])) {
$content = preg_replace('/(?<=window.VITE_APP_API_URL=")(.*)(?=")/', $setup['VITE_APP_API_URL'], $content);
}
if (isset($setup['VITE_APP_SENTRYDSN'])) {
$content = preg_replace('/(?<=window.VITE_APP_SENTRYDSN=")(.*)(?=")/', $setup['VITE_APP_SENTRYDSN'], $content);
}
if (isset($setup['VITE_APP_YBUG_ID'])) {
$content = preg_replace('/(?<=window.VITE_APP_YBUG_ID=")(.*)(?=")/', $setup['VITE_APP_YBUG_ID'], $content);
}
if (isset($setup['VITE_APP_BASENAME'])) {
$content = preg_replace('/(?<=window.VITE_APP_BASENAME=")(.*)(?=")/', $setup['VITE_APP_BASENAME'], $content);
}
if (isset($setup['VITE_APP_ROUTING_TYPE'])) {
$content = preg_replace('/(?<=window.VITE_APP_ROUTING_TYPE=")(.*)(?=")/', $setup['VITE_APP_ROUTING_TYPE'], $content);
}
if (isset($setup['VITE_APP_URL'])) {
$content = preg_replace('/(?<=window.VITE_APP_URL=")(.*)(?=")/', $setup['VITE_APP_URL'], $content);
}
}
}

echo $content;
34 changes: 17 additions & 17 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ set -e
# Apache gets grumpy about PID files pre-existing
rm -f /usr/local/apache2/logs/httpd.pid

cp /usr/local/apache2/htdocs/tpl.html /usr/local/apache2/htdocs/index.html
sed -ie "s~API_URL = null~API_URL=\"$API_URL\"~" /usr/local/apache2/htdocs/index.html
sed -ie "s~SENTRYDSN = null~SENTRYDSN=\"$SENTRYDSN\"~" /usr/local/apache2/htdocs/index.html
sed -ie "s~YBUG_ID = null~YBUG_ID=\"$YBUG_ID\"~" /usr/local/apache2/htdocs/index.html
sed -ie "s~BASENAME = null~BASENAME=\"$BASENAME\"~" /usr/local/apache2/htdocs/index.html
sed -ie "s~ROUTING_TYPE = null~ROUTING_TYPE=\"$ROUTING_TYPE\"~" /usr/local/apache2/htdocs/index.html
sed -ie "s~APP_URL = null~APP_URL=\"$APP_URL\"~" /usr/local/apache2/htdocs/index.html
sed -ie "s~APP_FIREBASE_VAPID_KEY = null~APP_FIREBASE_VAPID_KEY=\"$APP_FIREBASE_VAPID_KEY\"~" /usr/local/apache2/htdocs/index.html
sed -ie "s~APP_FIREBASE_APIKEY = null~APP_FIREBASE_APIKEY=\"$APP_FIREBASE_APIKEY\"~" /usr/local/apache2/htdocs/index.html
sed -ie "s~APP_FIREBASE_AUTHDOMAIN = null~APP_FIREBASE_AUTHDOMAIN=\"$APP_FIREBASE_AUTHDOMAIN\"~" /usr/local/apache2/htdocs/index.html
sed -ie "s~APP_FIREBASE_PROJECTID = null~APP_FIREBASE_PROJECTID=\"$APP_FIREBASE_PROJECTID\"~" /usr/local/apache2/htdocs/index.html
sed -ie "s~APP_FIREBASE_STORAGEBUCKET = null~APP_FIREBASE_STORAGEBUCKET=\"$APP_FIREBASE_STORAGEBUCKET\"~" /usr/local/apache2/htdocs/index.html
sed -ie "s~APP_FIREBASE_MESSAGINGSENDERID = null~APP_FIREBASE_MESSAGINGSENDERID=\"$APP_FIREBASE_MESSAGINGSENDERID\"~" /usr/local/apache2/htdocs/index.html
sed -ie "s~APP_FIREBASE_APPID = null~APP_FIREBASE_APPID=\"$APP_FIREBASE_APPID\"~" /usr/local/apache2/htdocs/index.html
sed -ie "s~APP_IOS_APIKEY = null~APP_IOS_APIKEY=\"$APP_IOS_APIKEY\"~" /usr/local/apache2/htdocs/index.html
sed -ie "s~APP_ANDROID_APIKEY = null~APP_ANDROID_APIKEY=\"$APP_ANDROID_APIKEY\"~" /usr/local/apache2/htdocs/index.html
cp /var/www/html/tpl.html /var/www/html/index.html
sed -ie "s~API_URL = null~API_URL=\"$API_URL\"~" /var/www/html/index.html
sed -ie "s~SENTRYDSN = null~SENTRYDSN=\"$SENTRYDSN\"~" /var/www/html/index.html
sed -ie "s~YBUG_ID = null~YBUG_ID=\"$YBUG_ID\"~" /var/www/html/index.html
sed -ie "s~BASENAME = null~BASENAME=\"$BASENAME\"~" /var/www/html/index.html
sed -ie "s~ROUTING_TYPE = null~ROUTING_TYPE=\"$ROUTING_TYPE\"~" /var/www/html/index.html
sed -ie "s~APP_URL = null~APP_URL=\"$APP_URL\"~" /var/www/html/index.html
sed -ie "s~APP_FIREBASE_VAPID_KEY = null~APP_FIREBASE_VAPID_KEY=\"$APP_FIREBASE_VAPID_KEY\"~" /var/www/html/index.html
sed -ie "s~APP_FIREBASE_APIKEY = null~APP_FIREBASE_APIKEY=\"$APP_FIREBASE_APIKEY\"~" /var/www/html/index.html
sed -ie "s~APP_FIREBASE_AUTHDOMAIN = null~APP_FIREBASE_AUTHDOMAIN=\"$APP_FIREBASE_AUTHDOMAIN\"~" /var/www/html/index.html
sed -ie "s~APP_FIREBASE_PROJECTID = null~APP_FIREBASE_PROJECTID=\"$APP_FIREBASE_PROJECTID\"~" /var/www/html/index.html
sed -ie "s~APP_FIREBASE_STORAGEBUCKET = null~APP_FIREBASE_STORAGEBUCKET=\"$APP_FIREBASE_STORAGEBUCKET\"~" /var/www/html/index.html
sed -ie "s~APP_FIREBASE_MESSAGINGSENDERID = null~APP_FIREBASE_MESSAGINGSENDERID=\"$APP_FIREBASE_MESSAGINGSENDERID\"~" /var/www/html/index.html
sed -ie "s~APP_FIREBASE_APPID = null~APP_FIREBASE_APPID=\"$APP_FIREBASE_APPID\"~" /var/www/html/index.html
sed -ie "s~APP_IOS_APIKEY = null~APP_IOS_APIKEY=\"$APP_IOS_APIKEY\"~" /var/www/html/index.html
sed -ie "s~APP_ANDROID_APIKEY = null~APP_ANDROID_APIKEY=\"$APP_ANDROID_APIKEY\"~" /var/www/html/index.html

echo "API URL= " $API_URL
echo "SENTRYDSN= " $SENTRYDSN
Expand All @@ -37,4 +37,4 @@ echo "APP_FIREBASE_APPID= " $APP_FIREBASE_APPID
echo "APP_IOS_APIKEY= " $APP_IOS_APIKEY
echo "APP_ANDROID_APIKEY= " $APP_ANDROID_APIKEY

exec httpd -DFOREGROUND "$@"
exec apache2-foreground
Loading

0 comments on commit 95c75a5

Please sign in to comment.