From a1871e89659689359396ba9f87886b568b8be3f4 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Tue, 4 Feb 2020 14:11:42 +0100
Subject: [PATCH] Implement support to overwrite apache configuration

---
 src/Handler.php                   | 16 +++++++++++++---
 templates/docker-compose.yml.twig |  3 +++
 templates/vhost.conf              | 15 +++++++++++++++
 3 files changed, 31 insertions(+), 3 deletions(-)
 create mode 100644 templates/vhost.conf

diff --git a/src/Handler.php b/src/Handler.php
index 48954ec..c4171ce 100644
--- a/src/Handler.php
+++ b/src/Handler.php
@@ -79,7 +79,10 @@ class Handler extends BaseHandler {
     $twig = new \Twig_Environment($twig_loader);
     $options['webRoot'] = $webRoot . '/';
     $orig_ignored = FALSE;
-    foreach ($this->getFiles($projectRoot, $webRoot, $settingsPath) as $template => $def) {
+    foreach ($this->getFiles($projectRoot, $webRoot, $settingsPath, $options) as $template => $def) {
+      if (isset($def['condition']) && !$def['condition']) {
+        continue;
+      }
       if (!$fs->exists($def['dest'])) {
         $fs->mkdir($def['dest']);
       }
@@ -161,11 +164,13 @@ class Handler extends BaseHandler {
    *   Name of the web's root directory.
    * @param string $settingsPath
    *   Name of the settings directory.
+   * @param array $options
+   *   Keyed array with all current options.
    *
    * @return array
    *   List of files.
    */
-  protected function getFiles($projectRoot, $webRoot, $settingsPath): array {
+  protected function getFiles($projectRoot, $webRoot, $settingsPath, $options): array {
     return [
       'settings.docker.php' => [
         'dest' => $projectRoot . '/' . $settingsPath,
@@ -194,7 +199,11 @@ class Handler extends BaseHandler {
         'dest' => $projectRoot . '/tests/backstop',
         'add2yaml' => TRUE,
         'add2git' => TRUE,
-      ]
+      ],
+      'vhost.conf' => [
+        'dest' => $projectRoot . '/apache',
+        'condition' => $options['webserver']['overwriteconfig'],
+      ],
     ];
   }
 
@@ -256,6 +265,7 @@ class Handler extends BaseHandler {
         ],
         'webserver' => [
           'type' => 'apache',
+          'overwriteconfig' => (bool) $env->receiveGlobal('WEBSERVER_CONFIG_OVERWRITE', 'Overwrite webserver config (0 or 1)', '0'),
         ],
         'varnish' => [
           'enable' => 0,
diff --git a/templates/docker-compose.yml.twig b/templates/docker-compose.yml.twig
index 666dfa5..9848332 100644
--- a/templates/docker-compose.yml.twig
+++ b/templates/docker-compose.yml.twig
@@ -71,6 +71,9 @@ services:
 {% elseif webserver.type == 'apache' %}
       APACHE_VHOST_PRESET: php
       APACHE_LOG_LEVEL: debug
+{% if webserver.overwriteconfig %}
+      APACHE_INCLUDE_CONF: /var/www/html/apache/vhost.conf
+{% endif %}
 {% endif %}
       {{ webserver.type|upper }}_BACKEND_HOST: php
       {{ webserver.type|upper }}_SERVER_ROOT: /var/www/html/{{ webRoot }}
diff --git a/templates/vhost.conf b/templates/vhost.conf
new file mode 100644
index 0000000..07750ae
--- /dev/null
+++ b/templates/vhost.conf
@@ -0,0 +1,15 @@
+<VirtualHost *:80>
+    DocumentRoot "/var/www/html/{{ webRoot }}/"
+    ServerName default
+    Include conf/preset.conf
+    <Location />
+        Require all granted
+    </Location>
+    SetEnv HTTPS on
+    SetEnvIf X-Forwarded-Proto https HTTPS=on
+    SetEnvIf Request_URI "^/\.healthz$" dontlog
+    RedirectMatch 204 .healthz
+    <IfModule mod_proxy_http.c>
+        SSLProxyEngine on
+    </IfModule>
+</VirtualHost>
-- 
GitLab