Browse Source

Adding caddy support

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>
Ondrej Kubik 7 years ago
parent
commit
e077c85d00
6 changed files with 63 additions and 1 deletions
  1. 5 0
      snap-src/Caddyfile
  2. 12 0
      snap-src/bin/caddy-control
  3. 11 1
      snap-src/bin/config
  4. 9 0
      snap-src/bin/wekan-read-settings
  5. 12 0
      snap/hooks/configure
  6. 14 0
      snapcraft.yaml

+ 5 - 0
snap-src/Caddyfile

@@ -0,0 +1,5 @@
+http://:8080
+proxy / localhost:3001 {
+  websocket
+  transparent
+}

+ 12 - 0
snap-src/bin/caddy-control

@@ -0,0 +1,12 @@
+#!/bin/bash
+
+# get wekan/mongo settings
+source $SNAP/bin/wekan-read-settings
+
+if [ "$CADDY_ENABLED" = "true" ]; then
+    env LC_ALL=C caddy -conf=$SNAP_DATA/Caddyfile -host=localhost:${CADDY_PORT}
+else
+    snapctl stop caddy-service 2>&1 || true
+    # sleep here, in case snapctl fails to stop service so we do not restart too often
+    sleep 60
+fi

+ 11 - 1
snap-src/bin/config

@@ -1,12 +1,14 @@
 #!/bin/sh
 
 # store here all configuration options for wekan snap
+SNAP_NAME="wekan"
+
 # read configured settings first
 SETTINGS_FILE="$SNAP_COMMON/wekan_settings.sh"
 [ -f $SETTINGS_FILE ] && . $SETTINGS_FILE
 
 # list of supported keys
-keys="MONGODB_BIND_UNIX_SOCKET MONGODB_BIND_IP MONGODB_PORT MAIL_URL MAIL_FROM ROOT_URL PORT DISABLE_MONGODB"
+keys="MONGODB_BIND_UNIX_SOCKET MONGODB_BIND_IP MONGODB_PORT MAIL_URL MAIL_FROM ROOT_URL PORT DISABLE_MONGODB CADDY_ENABLED CADDY_BIND_PORT"
 
 # default values
 DESCRIPTION_MONGODB_BIND_UNIX_SOCKET="mongodb binding unix socket:\n"\
@@ -42,3 +44,11 @@ KEY_PORT="port"
 DESCRIPTION_DISABLE_MONGODB="Disable mongodb service: use only if binding to database outside of the snap. Valid values: [true,false]"
 DEFAULT_DISABLE_MONGODB="false"
 KEY_DISABLE_MONGODB="disable-mongodb"
+
+DESCRIPTION_CADDY_ENABLED="Enable caddy service (caddy - Every Site on HTTPS). Set to 'true' to enable caddy\n caddy settings are handled through $SNAP_COMMON/Caddyfile"
+DEFAULT_CADDY_ENABLED="false"
+KEY_CADDY_ENABLED="caddy-enabled"
+
+DESCRIPTION_CADDY_BIND_PORT="Port on which caddy will expect proxy, value set here will be set in $SNAP_COMMON/Caddyfile"
+DEFAULT_CADDY_BIND_PORT="3001"
+KEY_CADDY_BIND_PORT="caddy-bind-port"

+ 9 - 0
snap-src/bin/wekan-read-settings

@@ -18,3 +18,12 @@ do
         export $key=$value
     fi
 done
+
+# if caddy is enabled, do update port settings based on caddy file
+if [ "$CADDY_ENABLED" = "true" ]; then
+    echo "caddy is enabled, adjusting ports"
+    export CADDY_PORT=${PORT}
+    echo -e "CADDY_PORT=$CADDY_PORT"
+    export PORT=${CADDY_BIND_PORT}
+    echo -e "PORT=$PORT"
+fi

+ 12 - 0
snap/hooks/configure

@@ -7,4 +7,16 @@ echo "$(date '+%Y-%m-%d %H:%M:%S') $0: Entering hook"
 . $SNAP/bin/config
 
 # settings were altered by user, safest way to get them applied is to restart service
+# first check if caddy service is enabled
+value=$(snapctl get caddy-enabled)
+if [ "$value" = "true" ]; then
+    # update caddy file
+    bind_port=$(snapctl get caddy-bind-port)
+    port=$(snapctl get port)
+    [ "x" != "x${bind_port}" ] && sed -i 's|proxy / localhost:.* {|proxy / localhost:'"${bind_port}"' {|g' $SNAP_COMMON/Caddyfile
+    [ "x" != "x$port" ] && sed -i 's|http://:.*|http://:'"${port}"'|g' $SNAP_COMMON/Caddyfile
+    snapctl start caddy-service 2>&1 || true
+else
+    snapctl stop caddy-service 2>&1 || true
+fi
 

+ 14 - 0
snapcraft.yaml

@@ -38,6 +38,11 @@ apps:
         daemon: simple
         plugs: [network, network-bind]
 
+    caddy-service:
+        command: caddy-control
+        daemon: simple
+        plugs: [network, network-bind]
+
     help:
         command: wekan-help
 
@@ -124,3 +129,12 @@ parts:
     helpers:
         source: snap-src
         plugin: dump
+
+    caddy:
+        plugin: go
+        go-importpath: github.com/mholt/caddy
+        source: https://github.com/mholt/caddy.git
+        source-type: git
+        source-commit: 53e117802fedd5915eeb32907873d8786a4b2936
+        prime:
+            - bin/caddy