From bf0ce5c9636b0afb1e7ac8206f1cd6fd835cd880 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sat, 26 Nov 2022 16:30:30 +0100 Subject: [PATCH] dark-mode --- server/README.md | 1 - server/api/handler/website.go | 24 +++- server/test/common_test.go | 1 + server/website/api.html | 18 +-- server/website/api_more.html | 36 ++--- server/website/css/style-dark.css | 34 +++++ server/website/css/style.css | 23 ++- server/website/header.dark.html | 14 ++ server/website/header.light.html | 11 ++ server/website/index.html | 19 +-- server/website/message_sent.html | 20 +-- server/website/scn_send.dark.html | 136 ++++++++++++++++++ .../{scn_send.html => scn_send.light.html} | 6 +- server/website/theme_switch.dark.html | 26 ++++ server/website/theme_switch.light.html | 26 ++++ 15 files changed, 323 insertions(+), 72 deletions(-) create mode 100644 server/website/css/style-dark.css create mode 100644 server/website/header.dark.html create mode 100644 server/website/header.light.html create mode 100644 server/website/scn_send.dark.html rename server/website/{scn_send.html => scn_send.light.html} (98%) create mode 100644 server/website/theme_switch.dark.html create mode 100644 server/website/theme_switch.light.html diff --git a/server/README.md b/server/README.md index 28e93b0..efdc297 100644 --- a/server/README.md +++ b/server/README.md @@ -10,7 +10,6 @@ - full-text-search: https://www.sqlite.org/fts5.html#contentless_tables - - dark mode toggle for html - app-store link in HTML - route to re-check all pro-token diff --git a/server/api/handler/website.go b/server/api/handler/website.go index a9923db..4e0b610 100644 --- a/server/api/handler/website.go +++ b/server/api/handler/website.go @@ -6,6 +6,7 @@ import ( "blackforestbytes.com/simplecloudnotifier/website" "errors" "github.com/gin-gonic/gin" + "github.com/rs/zerolog/log" "net/http" "regexp" "strings" @@ -20,7 +21,7 @@ type WebsiteHandler struct { func NewWebsiteHandler(app *logic.Application) WebsiteHandler { return WebsiteHandler{ app: app, - rexTemplate: regexp.MustCompile("{{template\\|[A-Za-z0-9_\\-.]+}}"), + rexTemplate: regexp.MustCompile("{{template\\|[A-Za-z0-9_\\-\\[\\].]+}}"), rexConfig: regexp.MustCompile("{{config\\|[A-Za-z0-9_\\-.]+}}"), } } @@ -86,9 +87,13 @@ func (h WebsiteHandler) serveAsset(g *gin.Context, fn string, repl bool) ginresp data = h.rexTemplate.ReplaceAllFunc(data, func(match []byte) []byte { prefix := len("{{template|") suffix := len("}}") - fnSub := match[prefix : len(match)-suffix] - subdata, err := website.Assets.ReadFile(string(fnSub)) + fnSub := string(match[prefix : len(match)-suffix]) + + fnSub = strings.ReplaceAll(fnSub, "[theme]", h.getTheme(g)) + + subdata, err := website.Assets.ReadFile(fnSub) if err != nil { + log.Error().Str("templ", string(match)).Str("fnSub", fnSub).Str("source", fn).Msg("Failed to replace template") failed = true } return subdata @@ -104,6 +109,7 @@ func (h WebsiteHandler) serveAsset(g *gin.Context, fn string, repl bool) ginresp cval, ok := h.getReplConfig(string(cfgKey)) if !ok { + log.Error().Str("templ", string(match)).Str("source", fn).Msg("Failed to replace config") failed = true } return []byte(cval) @@ -154,3 +160,15 @@ func (h WebsiteHandler) getReplConfig(key string) (string, bool) { return "", false } + +func (h WebsiteHandler) getTheme(g *gin.Context) string { + if c, err := g.Cookie("theme"); err != nil { + return "light" + } else if c == "light" { + return "light" + } else if c == "dark" { + return "dark" + } else { + return "light" + } +} diff --git a/server/test/common_test.go b/server/test/common_test.go index 051f9cf..ef6f75e 100644 --- a/server/test/common_test.go +++ b/server/test/common_test.go @@ -5,6 +5,7 @@ import ( "blackforestbytes.com/simplecloudnotifier/api" "blackforestbytes.com/simplecloudnotifier/common/ginext" "blackforestbytes.com/simplecloudnotifier/db" + "blackforestbytes.com/simplecloudnotifier/google" "blackforestbytes.com/simplecloudnotifier/jobs" "blackforestbytes.com/simplecloudnotifier/logic" "blackforestbytes.com/simplecloudnotifier/push" diff --git a/server/website/api.html b/server/website/api.html index 366afe3..0a25ae0 100644 --- a/server/website/api.html +++ b/server/website/api.html @@ -1,15 +1,7 @@ - - - Simple Cloud Notifications - API - - - - - - + {{template|header.[theme].html}} @@ -18,10 +10,12 @@ made by Mike Schwörer -
- API Documentation + {{template|theme_switch.[theme].html}} - Send +
+ API Documentation + + Send

Simple Cloud Notifier

diff --git a/server/website/api_more.html b/server/website/api_more.html index 9add34a..88174ba 100644 --- a/server/website/api_more.html +++ b/server/website/api_more.html @@ -1,15 +1,7 @@ - - - Simple Cloud Notifications - API - - - - - - + {{template|header.[theme].html}} @@ -18,10 +10,12 @@ made by Mike Schwörer
-
- API Documentation + {{template|theme_switch.[theme].html}} - Send +
+ API Documentation + + Send

Simple Cloud Notifier

@@ -70,16 +64,10 @@
{
     "success":true,
     "message":"Message sent",
-    "response":
-    {
-        "multicast_id":8000000000000000006,
-        "success":1,
-        "failure":0,
-        "canonical_ids":0,
-        "results": [{"message_id":"0:10000000000000000000000000000000d"}]
-    },
+    "messagecount": 634,
     "quota":17,
-    "quota_max":100
+    "quota_max":100,
+    "scn_msg_id":993625
 }

If the operation is not successful the API will respond with a 4xx or 500 HTTP statuscode. @@ -116,7 +104,7 @@

There is also always a JSON payload with additional information. - The success field is always there and in the error state you the message field to get a descritpion of the problem. + The success field is always there and in the error case you can read the message field to get a more information about the problem.

{
     "success": false,
@@ -162,7 +150,7 @@
     {{config|baseURL}}/
-

Channels

+

Channel

By default all messages are sent to the user default channel (typically main) @@ -222,7 +210,7 @@ Depending on your use case it can be useful to create a bash script that handles things like resending messages if you have connection problems or waiting if there is no quota left.
Here is an example how such a scrippt could look like, you can put it into /usr/local/sbin and call it with scn_send "title" "content" (or with more parameters, see the script itself)

-
{{template|scn_send.html}}
+ {{template|scn_send.[theme].html}}
diff --git a/server/website/css/style-dark.css b/server/website/css/style-dark.css new file mode 100644 index 0000000..823ff5b --- /dev/null +++ b/server/website/css/style-dark.css @@ -0,0 +1,34 @@ + + + +#mainpnl { + box-shadow: initial; +} + +#mainpnl h1 +{ + text-align: center; + margin-top: 0; + margin-bottom: 24px; + font-weight: bold; + color: #CFCFCF; + text-shadow: #000 0 0 2px, #666 0 0 8px; +} + +.edge-btn { + position: absolute; + border-color: #888 !important; +} + +pre.yellow-code, +pre.yellow-code span { + background: none; +} + +#theme-switch { + fill: #EA0; +} + +#theme-switch:hover { + fill: #88F; +} \ No newline at end of file diff --git a/server/website/css/style.css b/server/website/css/style.css index 0e04e7e..13c80c8 100644 --- a/server/website/css/style.css +++ b/server/website/css/style.css @@ -113,9 +113,12 @@ body color: #0288D1; } +.edge-btn { + position: absolute; +} + #tr_link { - position: absolute; top: 0; right: 0; margin: -1px -1px 0 0; @@ -127,7 +130,6 @@ body #tl_link1 { - position: absolute; top: 0; left: 0; margin: -1px 0 0 -1px; @@ -138,7 +140,6 @@ body #tl_link2 { - position: absolute; top: 0; left: 48px; margin: -1px 0 0 -1px; @@ -149,7 +150,6 @@ body #tl_linkDocs { - position: absolute; top: 0; left: 0; margin: -1px 0 0 -1px; @@ -297,4 +297,17 @@ pre, pre span .display_none { display: none; -} \ No newline at end of file +} + +#theme-switch { + position: absolute; + top: 0.75rem; + right: 0.75rem; + cursor: pointer; + fill: #000; +} + +#theme-switch:hover { + filter: blur(1px); + fill: #008; +} diff --git a/server/website/header.dark.html b/server/website/header.dark.html new file mode 100644 index 0000000..eb52789 --- /dev/null +++ b/server/website/header.dark.html @@ -0,0 +1,14 @@ + +Simple Cloud Notifications + + + + + + + + + + + + \ No newline at end of file diff --git a/server/website/header.light.html b/server/website/header.light.html new file mode 100644 index 0000000..aa4b231 --- /dev/null +++ b/server/website/header.light.html @@ -0,0 +1,11 @@ + +Simple Cloud Notifications + + + + + + + + + \ No newline at end of file diff --git a/server/website/index.html b/server/website/index.html index 0a29f1a..c22d682 100644 --- a/server/website/index.html +++ b/server/website/index.html @@ -1,16 +1,7 @@ - - Simple Cloud Notifications - - - - - - - - + {{template|header.[theme].html}} @@ -19,12 +10,14 @@ made by Mike Schwörer
+ {{template|theme_switch.[theme].html}} +
- - + + - API + API

Simple Cloud Notifier

diff --git a/server/website/message_sent.html b/server/website/message_sent.html index ade52e6..178017e 100644 --- a/server/website/message_sent.html +++ b/server/website/message_sent.html @@ -1,15 +1,7 @@ - - Simple Cloud Notifications - - - - - - - + {{template|header.[theme].html}} @@ -18,12 +10,14 @@ made by Mike Schwörer -
+ {{template|theme_switch.[theme].html}} - - +
- Send + + + + Send

Simple Cloud Notifier

diff --git a/server/website/scn_send.dark.html b/server/website/scn_send.dark.html new file mode 100644 index 0000000..228b8fe --- /dev/null +++ b/server/website/scn_send.dark.html @@ -0,0 +1,136 @@ + + + + + +
+#!/usr/bin/env bash
+
+#
+# Wrapper around SCN ( https://scn.blackforestbytes.com/ )
+# ========================================================
+#
+# ./scn_send [@channel] title [content] [priority]
+#
+#
+# Call with   scn_send              "${title}"
+#        or   scn_send              "${title}" ${content}"
+#        or   scn_send              "${title}" ${content}" "${priority:0|1|2}"
+#        or   scn_send "@${channel} "${title}"
+#        or   scn_send "@${channel} "${title}" ${content}"
+#        or   scn_send "@${channel} "${title}" ${content}" "${priority:0|1|2}"
+#
+#
+
+################################################################################
+# INSERT YOUR DATA HERE                                                        #
+################################################################################
+user_id="999"
+user_key="??"
+################################################################################
+
+usage() {
+    echo "Usage: "
+    echo "  scn_send [@channel] title [content] [priority]"
+    echo ""
+}
+
+args=( "$@" )
+
+title=$1
+content=""
+channel=""
+priority=1
+usr_msg_id=$(uuidgen)
+sendtime=$(date +%s)
+
+if [ ${#args[@]} -lt 1 ]; then
+    echo "[ERROR]: no title supplied via parameter" 1>&2
+    usage
+    exit 1
+fi
+
+if [[ "${args[0]}" =~ ^@.* ]]; then
+	channel="${args[0]}"
+    unset "args[0]"
+	channel="${channel:1}"
+fi
+
+if [ ${#args[@]} -lt 1 ]; then
+    echo "[ERROR]: no title supplied via parameter" 1>&2
+    usage
+    exit 1
+fi
+
+title="${args[0]}"
+content=""
+sendtime=$(date +%s)
+
+if [ ${#args[@]} -gt 1 ]; then
+    content="${args[0]}"
+    unset "args[0]"
+fi
+
+if [ ${#args[@]} -gt 1 ]; then
+    priority="${args[0]}"
+    unset "args[0]"
+fi
+
+if [ ${#args[@]} -gt 1 ]; then
+        echo "Too many arguments to scn_send" 1>&2
+        usage
+        exit 1
+fi
+
+
+while true ; do
+
+    curlresp=$(curl --silent                             \
+                    --output /dev/null                   \
+                    --write-out "%{http_code}"           \
+                    --data "user_id=$user_id"            \
+                    --data "user_key=$user_key"          \
+                    --data "title=$title"                \
+                    --data "timestamp=$sendtime"         \
+                    --data "content=$content"            \
+                    --data "priority=$priority"          \
+                    --data "msg_id=$usr_msg_id"          \
+                    --data "channel=$channel"            \
+                    "https://scn.blackforestbytes.com/"  )
+
+    if [ "$curlresp" == 200 ] ; then
+        echo "Successfully send"
+        exit 0
+    fi
+
+    if [ "$curlresp" == 400 ] ; then
+        echo "Bad request - something went wrong" 1>&2
+        exit 1
+    fi
+
+    if [ "$curlresp" == 401 ] ; then
+        echo "Unauthorized - wrong userid/userkey" 1>&2
+        exit 1
+    fi
+
+    if [ "$curlresp" == 403 ] ; then
+        echo "Quota exceeded - wait one hour before re-try" 1>&2
+        sleep 3600
+    fi
+
+    if [ "$curlresp" == 412 ] ; then
+        echo "Precondition Failed - No device linked" 1>&2
+        exit 1
+    fi
+
+    if [ "$curlresp" == 500 ] ; then
+        echo "Internal server error - waiting for better times" 1>&2
+        sleep 60
+    fi
+
+    # if none of the above matched we probably hav no network ...
+    echo "Send failed (response code $curlresp) ... try again in 5s" 1>&2
+    sleep 5
+done
+
+ diff --git a/server/website/scn_send.html b/server/website/scn_send.light.html similarity index 98% rename from server/website/scn_send.html rename to server/website/scn_send.light.html index 008c37b..041ec63 100644 --- a/server/website/scn_send.html +++ b/server/website/scn_send.light.html @@ -1,7 +1,10 @@ -#!/usr/bin/env bash + + +
+#!/usr/bin/env bash
 
 #
 # Wrapper around SCN ( https://scn.blackforestbytes.com/ )
@@ -129,3 +132,4 @@ sendtime=$(date +%s"Send failed (response code $curlresp) ... try again in 5s" 1>&2
     sleep 5
 done
+
\ No newline at end of file diff --git a/server/website/theme_switch.dark.html b/server/website/theme_switch.dark.html new file mode 100644 index 0000000..8f54d78 --- /dev/null +++ b/server/website/theme_switch.dark.html @@ -0,0 +1,26 @@ +
+ + + + + + + + + + + + + + +
+ + \ No newline at end of file diff --git a/server/website/theme_switch.light.html b/server/website/theme_switch.light.html new file mode 100644 index 0000000..56a09c2 --- /dev/null +++ b/server/website/theme_switch.light.html @@ -0,0 +1,26 @@ +
+ + + + + + + + + + + + + + +
+ + \ No newline at end of file