From 4b52224aebcd59b6ab7bdbebef42acb0268f0c75 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen.haas@lakedrops.com>
Date: Sat, 16 Sep 2023 11:01:16 +0200
Subject: [PATCH] docker/l3d#133 Add support for unlighthouse

---
 .ahoy.l3d.yml                          |  4 ++++
 ahoy.unlighthouse.yml                  | 18 ++++++++++++++++++
 src/Handler.php                        | 13 +++++++++++++
 templates/docker-compose.yml.twig      | 25 +++++++++++++++++++++++++
 templates/tests/unlighthouse.config.ts | 13 +++++++++++++
 5 files changed, 73 insertions(+)
 create mode 100644 ahoy.unlighthouse.yml
 create mode 100644 templates/tests/unlighthouse.config.ts

diff --git a/.ahoy.l3d.yml b/.ahoy.l3d.yml
index de2b769..6cc76c4 100644
--- a/.ahoy.l3d.yml
+++ b/.ahoy.l3d.yml
@@ -24,3 +24,7 @@ commands:
     imports:
       - ahoy.cypress.yml
     usage: Cypress
+  unlighthouse:
+    imports:
+      - ahoy.unlighthouse.yml
+    usage: Unlighthouse
diff --git a/ahoy.unlighthouse.yml b/ahoy.unlighthouse.yml
new file mode 100644
index 0000000..c7b28d9
--- /dev/null
+++ b/ahoy.unlighthouse.yml
@@ -0,0 +1,18 @@
+ahoyapi: v2
+commands:
+  on:
+    cmd: |
+      UNLIGHTHOUSE=1
+      echo "UNLIGHTHOUSE=1" >>.env
+      env -i $(cat .env | xargs) >.env
+      composer lakedrops:docker4drupal --no-interaction
+      ahoy d4d up unlighthouse
+    usage: Turn on Unlighthouse
+  off:
+    cmd: |
+      UNLIGHTHOUSE=0
+      echo "UNLIGHTHOUSE=0" >>.env
+      env -i $(cat .env | xargs) >.env
+      composer lakedrops:docker4drupal --no-interaction
+      ahoy d4d stop unlighthouse
+    usage: Turn off Unlighthouse
diff --git a/src/Handler.php b/src/Handler.php
index 3e7d82f..128760e 100644
--- a/src/Handler.php
+++ b/src/Handler.php
@@ -201,6 +201,9 @@ class Handler extends BaseHandler {
         'enable' => $this->env->receiveGlobal('CYPRESS', 'Cypress', '0'),
         'version' => 'latest',
       ],
+      'unlighthouse' => [
+        'enable' => $this->env->receiveGlobal('UNLIGHTHOUSE', 'Unlighthouse', '0'),
+      ],
     ];
   }
 
@@ -230,6 +233,7 @@ class Handler extends BaseHandler {
     $this->config->setValue('projectroot', $projectRoot, FALSE);
     $this->config->setValue('projectdomain', str_replace('_', '-', $this->config->readValue('projectname')) . '.' . $traefik['domain'], FALSE);
     $this->config->setValue('projectprotocol', 'http' . ($traefik['usessl'] ? 's' : ''), FALSE);
+    $this->config->setValue('socketprotocol', 'ws' . ($traefik['usessl'] ? 's' : ''), FALSE);
     $projectport = '';
     if ($traefik['usessl'] && (int) $traefik['ports'] !== 443) {
       $projectport = ':' . $traefik['ports'];
@@ -344,6 +348,7 @@ class Handler extends BaseHandler {
           'projectdomain' => str_replace('_', '-', $this->config->readValue('domain') ?? ''),
           'projectprotocol' => 'https',
           'projectport' => '',
+          'socketprotocol' => 'wss',
           'extradomains' => $this->config->readValue('aliases') ?? [],
         ];
         foreach ($overwriteConfig as $key => $value) {
@@ -489,6 +494,9 @@ class Handler extends BaseHandler {
     $this->gitIgnore('tests/cypress/screenshots');
     $this->gitIgnore('tests/cypress/videos');
 
+    // Ignore some Unlighthouse directories
+    $this->gitIgnore('tests/unlighthouse');
+
     if (getenv('LAKEDROPS_BUILD_NG') !== 'yes') {
       $this->updateTraefik();
     }
@@ -626,6 +634,11 @@ class Handler extends BaseHandler {
       'dest' => $projectRoot . '/tests/cypress/support',
       'add2git' => TRUE,
     ];
+    $files['unlighthouse.config.ts'] = [
+      'source' => 'tests/',
+      'dest' => $projectRoot . '/tests',
+      'add2git' => FALSE,
+    ];
     if (getenv('LAKEDROPS_BUILD_NG') === 'yes') {
       if ($this->config->readValue(['backup', 'enable'])) {
         $files['config.yaml'] = [
diff --git a/templates/docker-compose.yml.twig b/templates/docker-compose.yml.twig
index 9fdac5c..d02410e 100644
--- a/templates/docker-compose.yml.twig
+++ b/templates/docker-compose.yml.twig
@@ -570,3 +570,28 @@ services:
     user: '1000:{{ docker_group_id }}'
 {% endif %}
 {% endif %}
+{% if unlighthouse.enable|default(0) %}
+
+  unlighthouse:
+    image: 'registry.lakedrops.com/docker/unlighthouse:latest'
+    restart: unless-stopped
+    networks:
+      - traefik-public
+      - default
+    volumes:
+      - /etc/timezone:/etc/timezone:ro
+      - /etc/localtime:/etc/localtime:ro
+      - {{ projectroot }}/tests/unlighthouse/:/data/.unlighthouse
+      - {{ projectroot }}/tests/unlighthouse.config.ts:/data/unlighthouse.config.ts
+    working_dir: /data
+    user: root
+    labels:
+      traefik.enable: 'true'
+      traefik.docker.network: traefik-public
+      traefik.http.services.{{ projectname }}_unlighthouse.loadbalancer.server.port: 5678
+      traefik.http.routers.unlighthouse-{{ projectname }}.rule: Host(`{{ projectdomain }}`)
+{% if drupal.live|default(0) or traefik.usessl %}
+      traefik.http.routers.unlighthouse-{{ projectname }}.tls: 'true'
+      traefik.http.routers.unlighthouse-{{ projectname }}.tls.certresolver: lakedrops
+{% endif %}
+{% endif %}
diff --git a/templates/tests/unlighthouse.config.ts b/templates/tests/unlighthouse.config.ts
new file mode 100644
index 0000000..4b83077
--- /dev/null
+++ b/templates/tests/unlighthouse.config.ts
@@ -0,0 +1,13 @@
+export default {
+    puppeteerOptions: {
+        args: ["--no-sandbox"],
+    },
+    server: {
+        open: false,
+    },
+    site: "{{ projectprotocol }}://{{ projectdomain }}",
+    serverUrl: "{{ projectprotocol }}:/unlighthouse-{{ projectdomain }}/",
+    clientUrl: "{{ projectprotocol }}://unlighthouse-{{ projectdomain }}/",
+    apiUrl: "{{ projectprotocol }}://unlighthouse-{{ projectdomain }}/api",
+    websocketUrl: "{{ socketprotocol }}://unlighthouse-{{ projectdomain }}/api/ws",
+}
-- 
GitLab