From fe3ccd1074431f83c57fa092787b17bb0d47d62b Mon Sep 17 00:00:00 2001
From: Pieter Vander Vennet <pietervdvn@posteo.net>
Date: Mon, 25 Sep 2023 03:13:17 +0200
Subject: [PATCH] CI: add extra config file for easier per-deployment
 veriations, add hetzner deploy script

---
 config.json                      |  3 +++
 package.json                     |  3 ++-
 scripts/hetzner/config.json      |  4 ++++
 scripts/hetzner/deployHetzner.sh | 20 ++++++++++++++++++++
 src/Models/Constants.ts          | 22 +++++++++++-----------
 5 files changed, 40 insertions(+), 12 deletions(-)
 create mode 100644 config.json
 create mode 100644 scripts/hetzner/config.json
 create mode 100755 scripts/hetzner/deployHetzner.sh

diff --git a/config.json b/config.json
new file mode 100644
index 000000000..16f8d4541
--- /dev/null
+++ b/config.json
@@ -0,0 +1,3 @@
+{
+  "#": "Settings in this file override the `config`-section of `package.json`"
+}
diff --git a/package.json b/package.json
index cbc562381..66bf5bccb 100644
--- a/package.json
+++ b/package.json
@@ -8,7 +8,8 @@
   "main": "index.ts",
   "type": "module",
   "config": {
-    "#": "Various endpoints that are instance-specific",
+    "#": "Various endpoints that are instance-specific. This is the default configuration, which is re-exported in 'Constants.ts'.",
+    "#": "Use MAPCOMPLETE_CONFIGURATION to use an additional configuration, e.g. `MAPCOMPLETE_CONFIGURATION=config_hetzner`",
     "#oauth_credentials:comment": [
       "`oauth_credentials` are the OAuth-2 credentials for the production-OSM server and the test-server.",
       "Are you deploying your own instance? Register your application too.",
diff --git a/scripts/hetzner/config.json b/scripts/hetzner/config.json
new file mode 100644
index 000000000..c3ad60fdd
--- /dev/null
+++ b/scripts/hetzner/config.json
@@ -0,0 +1,4 @@
+{
+  "#":"Some configuration tweaks specifically for hetzner",
+  "country_coder_host": "https://countrycoder.mapcomplete.org/"
+}
diff --git a/scripts/hetzner/deployHetzner.sh b/scripts/hetzner/deployHetzner.sh
new file mode 100755
index 000000000..70aec756f
--- /dev/null
+++ b/scripts/hetzner/deployHetzner.sh
@@ -0,0 +1,20 @@
+#! /bin/bash
+### To be run from the root of the repository 
+
+# Some pointers to get started:
+# apt install npm
+# apt install unzip
+# npm i -g csp-logger
+
+# wget https://github.com/pietervdvn/latlon2country/raw/main/tiles.zip
+# unzip tiles.zip
+
+MAPCOMPLETE_CONFIGURATION="config_hetzner"
+cp config.json config.json.bu &&
+cp ./scripts/hetzner/config.json . &&
+npm run prepare-deploy &&
+mv config.json.bu config.json &&
+zip dist.zip -r dist/* &&
+scp -r dist.zip hetzner:/root/ &&
+scp ./scripts/hetzner/config/* hetzner:/root/
+ssh hetzner -t "unzip dist.zip && rm dist.zip && rm -rf public/ mv dist public && caddy stop && caddy start"
diff --git a/src/Models/Constants.ts b/src/Models/Constants.ts
index 3edeebc87..75ea6f73f 100644
--- a/src/Models/Constants.ts
+++ b/src/Models/Constants.ts
@@ -1,14 +1,11 @@
-import * as meta from "../../package.json"
+import * as packagefile from "../../package.json"
+import * as extraconfig from "../../config.json"
 import { Utils } from "../Utils"
 
 export type PriviligedLayerType = (typeof Constants.priviliged_layers)[number]
 
 export default class Constants {
-    public static vNumber = meta.version
-
-    public static ImgurApiKey = meta.config.api_keys.imgur
-    public static readonly mapillary_client_token_v4 = meta.config.api_keys.mapillary_v4
-
+    public static vNumber = packagefile.version
     /**
      * API key for Maproulette
      *
@@ -17,9 +14,6 @@ export default class Constants {
      * Using an empty string however does work for most actions, but will attribute all actions to the Superuser.
      */
     public static readonly MaprouletteApiKey = ""
-
-    public static defaultOverpassUrls = meta.config.default_overpass_urls
-
     public static readonly added_by_default = [
         "selected_element",
         "gps_location",
@@ -47,7 +41,6 @@ export default class Constants {
         ...Constants.added_by_default,
         ...Constants.no_include,
     ] as const
-
     // The user journey states thresholds when a new feature gets unlocked
     public static userJourney = {
         moreScreenUnlock: 1,
@@ -104,7 +97,14 @@ export default class Constants {
      * In seconds
      */
     static zoomToLocationTimeout = 15
-    static countryCoderEndpoint: string = meta.config.country_coder_host
+    private static readonly config = (() => {
+        const defaultConfig = packagefile.config
+        return { ...defaultConfig, ...extraconfig }
+    })()
+    public static ImgurApiKey = Constants.config.api_keys.imgur
+    public static readonly mapillary_client_token_v4 = Constants.config.api_keys.mapillary_v4
+    public static defaultOverpassUrls = Constants.config.default_overpass_urls
+    static countryCoderEndpoint: string = Constants.config.country_coder_host
 
     /**
      * These are the values that are allowed to use as 'backdrop' icon for a map pin