forked from lolcat/4get
1
0
Fork 0

pass array as comma separated env

This commit is contained in:
throwaway 2024-04-19 13:47:02 -07:00
parent 4afec792c4
commit e31b9494af
3 changed files with 33 additions and 17 deletions

View File

@ -5,6 +5,10 @@ set -e
FOURGET_PROTO="${FOURGET_PROTO%\"}"
FOURGET_PROTO="${FOURGET_PROTO#\"}"
# make lowercase
FOURGET_PROTO=`echo $FOURGET_PROTO | awk '{print tolower($0)}'`
if [ "$FOURGET_PROTO" = "https" ] || [ -f /etc/4get/certs/fullchain.pem ] || [ -f /etc/4get/certs/privkey.pem ]; then
echo "Using https configuration"
cp /etc/apache2/https.conf /etc/apache2/httpd.conf

View File

@ -3,19 +3,21 @@
include "/var/www/html/4get/data/config.php";
$refl = new ReflectionClass('config');
$config = ($refl->getConstants());
$from_config = ($refl->getConstants());
$from_env = array();
$env = getenv();
$fourget_env = array_filter($env, function($v, $k) {
return str_starts_with($k, "FOURGET");
}, ARRAY_FILTER_USE_BOTH);
foreach($fourget_env as $key => $val) {
$target_key = preg_replace('/^FOURGET_/', '', $key);
$config[$target_key] = trim($val, '\'"');
$from_env[$target_key] = trim($val, '\'"');
};
$merged_config = array_merge($from_config, $from_env);
function type_to_string($n) {
$type = gettype($n);
if ($type === "NULL") {
@ -52,14 +54,24 @@ function detect_captcha_dirs() {
}
$special_keys = ["CAPTCHA_DATASET", "INSTANCES"];
$special_keys = ["PROTO", "CAPTCHA_DATASET"];
$output = "<?php\n // This file was generated by docker/gen_config.php\n";
$output = $output . "class config {\n";
foreach(($config) as $key => $val){
foreach(($merged_config) as $key => $val){
if(!in_array($key, $special_keys)) {
// conversion between arrays and comma separated env value.
// If original type of field is array and there is a type mismatch such as when a comma separted string is passed,
// then split on comma if string and not numeric
if(gettype($from_config[$key]) != gettype($val) && !is_numeric($val)) {
$data = gettype($val) === "string" ? explode(",", $val) : $val;
$output = $output . "\tconst " . $key . " = " . type_to_string($data) . ";\n";
} else {
$output = $output . "\tconst " . $key . " = " . type_to_string($val) . ";\n";
}
continue;
}
@ -67,11 +79,6 @@ continue;
if($key === "CAPTCHA_DATASET") {
$output = $output . "\tconst " . $key . " = " . type_to_string(detect_captcha_dirs()) . ";\n";
}
if($key === "INSTANCES") {
$instances_list = gettype($val) === "string" ? explode(",", $val) : $val;
$output = $output . "\tconst " . $key . " = " . type_to_string($instances_list) . ";\n";
}
}
$output = $output . "}\n";

View File

@ -2,12 +2,17 @@
When using docker container any environment variables prefixed with `FOURGET_` will be added to the generated config located at `/var/www/html/4get/data/config.php`
When lists of data is expected in [data/config.php](../data/config.php), such as `INSTANCES`, you can pass in a comma separated string via environment variable.
Example:
`FOURGET_INSTANCES="https://4get.ca,https://domain.tld"`
#### Special environment variables
| Name | value | Example |
| - | - | - |
| FOURGET_PROTO | "http" or "https" | "https" |
| FOURGET_INSTANCES | comma separated string of urls | "https://4get.ca,https://domain.tld" |
#### Important directories
@ -65,7 +70,7 @@ version: "3.7"
services:
fourget:
image: luuul/4get:latest
restart: always
restart: unless-stopped
environment:
- FOURGET_VERSION=6
- FOURGET_PROTO=http
@ -84,7 +89,7 @@ version: "3.7"
services:
fourget:
image: luuul/4get:latest
restart: always
restart: unless-stopped
environment:
- FOURGET_VERSION=6
- FOURGET_PROTO=https
@ -110,7 +115,7 @@ version: "3.7"
services:
fourget:
image: luuul/4get:latest
restart: always
restart: unless-stopped
environment:
- FOURGET_VERSION=6
- FOURGET_PROTO=http
@ -133,7 +138,7 @@ version: "3.7"
services:
fourget:
image: luuul/4get:latest
restart: always
restart: unless-stopped
environment:
- FOURGET_VERSION=6
- FOURGET_PROTO=http