From 144fb461e4cd0d6c8e326ff987cdb7af77c3a5e0 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Thu, 14 Sep 2023 15:13:23 +0800 Subject: [PATCH 1/6] WIP --- extra/push-examples/php/index.php | 1 - extra/push-examples/python/index.py | 1 - .../socket-handlers/general-socket-handler.js | 27 +++++++ src/assets/app.scss | 14 ++++ src/pages/Details.vue | 77 ++++++++++++++++++- src/pages/StatusPage.vue | 16 +--- 6 files changed, 118 insertions(+), 18 deletions(-) diff --git a/extra/push-examples/php/index.php b/extra/push-examples/php/index.php index 6c981ba11e..d08b451ab9 100644 --- a/extra/push-examples/php/index.php +++ b/extra/push-examples/php/index.php @@ -11,4 +11,3 @@ echo "Pushed!\n"; sleep(interval); } - diff --git a/extra/push-examples/python/index.py b/extra/push-examples/python/index.py index d541d7f434..c735b298cd 100644 --- a/extra/push-examples/python/index.py +++ b/extra/push-examples/python/index.py @@ -8,4 +8,3 @@ urllib.request.urlopen(push_url) print("Pushed!\n") time.sleep(interval) - diff --git a/server/socket-handlers/general-socket-handler.js b/server/socket-handlers/general-socket-handler.js index 64ace40213..82129061e6 100644 --- a/server/socket-handlers/general-socket-handler.js +++ b/server/socket-handlers/general-socket-handler.js @@ -4,6 +4,8 @@ const { sendInfo } = require("../client"); const { checkLogin } = require("../util-server"); const GameResolver = require("gamedig/lib/GameResolver"); const { testChrome } = require("../monitor-types/real-browser-monitor-type"); +const fs = require("fs"); +const path = require("path"); let gameResolver = new GameResolver(); let gameList = null; @@ -62,4 +64,29 @@ module.exports.generalSocketHandler = (socket, server) => { }); }); }); + + socket.on("getPushExample", (language, callback) => { + + try { + let dir = path.join("./extra/push-examples", language); + let files = fs.readdirSync(dir); + + for (let file of files) { + if (file.startsWith("index.")) { + callback({ + ok: true, + code: fs.readFileSync(path.join(dir, file), "utf8"), + }); + return; + } + } + } catch (e) { + + } + + callback({ + ok: false, + msg: "Not found", + }); + }); }; diff --git a/src/assets/app.scss b/src/assets/app.scss index f8d5e1c8d4..eb3c9f8e4f 100644 --- a/src/assets/app.scss +++ b/src/assets/app.scss @@ -584,6 +584,20 @@ h5.settings-subheading::after { border-bottom: 1px solid $dark-border-color; } +/* required class */ +.code-editor, .css-editor { + /* we dont use `language-` classes anymore so thats why we need to add background and text color manually */ + + border-radius: 1rem; + padding: 10px 5px; + border: 1px solid #ced4da; + + .dark & { + background: $dark-bg2; + border: 1px solid $dark-border-color; + } +} + $shadow-box-padding: 20px; diff --git a/src/pages/Details.vue b/src/pages/Details.vue index 847fcb5748..7e4baf3d23 100644 --- a/src/pages/Details.vue +++ b/src/pages/Details.vue @@ -76,6 +76,35 @@ + +
+ View Code + + +
+ + + +
+
+
+
@@ -249,6 +278,12 @@ import CertificateInfo from "../components/CertificateInfo.vue"; import { getMonitorRelativeURL } from "../util.ts"; import { URL } from "whatwg-url"; import { getResBaseURL } from "../util-frontend"; +import { highlight, languages } from "prismjs/components/prism-core"; +import "prismjs/components/prism-clike"; +import "prismjs/components/prism-javascript"; +import "prismjs/components/prism-css"; +import { PrismEditor } from "vue-prism-editor"; +import "vue-prism-editor/dist/prismeditor.min.css"; export default { components: { @@ -262,6 +297,7 @@ export default { PingChart, Tag, CertificateInfo, + PrismEditor, }, data() { return { @@ -275,6 +311,11 @@ export default { chunksNavigation: "scroll", }, cacheTime: Date.now(), + pushMonitor: { + showPushExamples: false, + currentExample: "javascript-fetch", + code: "", + } }; }, computed: { @@ -367,8 +408,23 @@ export default { return getResBaseURL() + this.monitor.screenshot + "?time=" + this.cacheTime; } }, + watch: { + "monitor.type"() { + if (this.monitor && this.monitor.type === "push") { + this.loadPushExample(); + } + }, + "pushMonitor.currentExample"() { + this.loadPushExample(); + }, + }, mounted() { - + if (this.monitor.type === "push") { + if (this.lastHeartBeat.status === -1) { + this.pushMonitor.showPushExamples = true; + } + this.loadPushExample(); + } }, methods: { getResBaseURL, @@ -515,6 +571,25 @@ export default { // Handle SQL Server return urlString.replaceAll(/Password=(.+);/ig, "Password=******;"); } + }, + + /** + * Highlight the example code + * @param {string} code Code + * @returns {string} Highlighted code + */ + pushExampleHighlighter(code) { + return highlight(code, languages.js); + }, + + loadPushExample() { + this.pushMonitor.code = ""; + this.$root.getSocket().emit("getPushExample", this.pushMonitor.currentExample, (res) => { + let code = res.code + .replace("https://example.com/api/push/key?status=up&msg=OK&ping=", this.pushURL) + .replace("60", this.monitor.interval); + this.pushMonitor.code = code; + }); } }, }; diff --git a/src/pages/StatusPage.vue b/src/pages/StatusPage.vue index f9c2be27f4..5e546871d9 100644 --- a/src/pages/StatusPage.vue +++ b/src/pages/StatusPage.vue @@ -742,7 +742,7 @@ export default { /** * Provide syntax highlighting for CSS * @param {string} code Text to highlight - * @returns {string} Highlighted HTML + * @returns {string} Highlighted CSS */ highlighter(code) { return highlight(code, languages.css); @@ -1239,20 +1239,6 @@ footer { } } -/* required class */ -.css-editor { - /* we dont use `language-` classes anymore so thats why we need to add background and text color manually */ - - border-radius: 1rem; - padding: 10px 5px; - border: 1px solid #ced4da; - - .dark & { - background: $dark-bg; - border: 1px solid $dark-border-color; - } -} - .bg-maintenance { .alert-heading { font-weight: bold; From d8a364fbd4d93392b318be4763a3f19012028285 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Fri, 15 Sep 2023 17:29:40 +0800 Subject: [PATCH 2/6] WIP --- extra/push-examples/docker/index.sh | 1 + extra/push-examples/javascript-fetch/index.js | 1 + extra/push-examples/typescript-fetch/index.ts | 1 + extra/uptime-kuma-push/.gitignore | 1 + extra/uptime-kuma-push/Dockerfile | 18 +++++++ extra/uptime-kuma-push/build.js | 49 +++++++++++++++++++ extra/uptime-kuma-push/package.json | 13 +++++ extra/uptime-kuma-push/uptime-kuma-push.go | 44 +++++++++++++++++ src/pages/Details.vue | 1 - 9 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 extra/push-examples/docker/index.sh create mode 100644 extra/uptime-kuma-push/.gitignore create mode 100644 extra/uptime-kuma-push/Dockerfile create mode 100644 extra/uptime-kuma-push/build.js create mode 100644 extra/uptime-kuma-push/package.json create mode 100644 extra/uptime-kuma-push/uptime-kuma-push.go diff --git a/extra/push-examples/docker/index.sh b/extra/push-examples/docker/index.sh new file mode 100644 index 0000000000..1eb43d650d --- /dev/null +++ b/extra/push-examples/docker/index.sh @@ -0,0 +1 @@ +docker run -d --restart=always --name uptime-kuma-push louislam/uptime-kuma:push "https://example.com/api/push/key?status=up&msg=OK&ping=" 60 diff --git a/extra/push-examples/javascript-fetch/index.js b/extra/push-examples/javascript-fetch/index.js index cb664513c3..9d21d0db82 100644 --- a/extra/push-examples/javascript-fetch/index.js +++ b/extra/push-examples/javascript-fetch/index.js @@ -1,3 +1,4 @@ +// Supports: Node.js >= 18, Deno, Bun const pushURL = "https://example.com/api/push/key?status=up&msg=OK&ping="; const interval = 60; diff --git a/extra/push-examples/typescript-fetch/index.ts b/extra/push-examples/typescript-fetch/index.ts index 6a4b0a552d..bb140880cf 100644 --- a/extra/push-examples/typescript-fetch/index.ts +++ b/extra/push-examples/typescript-fetch/index.ts @@ -1,3 +1,4 @@ +// Supports: Deno, Bun, Node.js >= 18 (ts-node) const pushURL : string = "https://example.com/api/push/key?status=up&msg=OK&ping="; const interval : number = 60; diff --git a/extra/uptime-kuma-push/.gitignore b/extra/uptime-kuma-push/.gitignore new file mode 100644 index 0000000000..a007feab07 --- /dev/null +++ b/extra/uptime-kuma-push/.gitignore @@ -0,0 +1 @@ +build/* diff --git a/extra/uptime-kuma-push/Dockerfile b/extra/uptime-kuma-push/Dockerfile new file mode 100644 index 0000000000..9d619d60de --- /dev/null +++ b/extra/uptime-kuma-push/Dockerfile @@ -0,0 +1,18 @@ +FROM node AS build +RUN useradd --create-home kuma +USER kuma +WORKDIR /home/kuma +ARG TARGETPLATFORM +COPY --chown=kuma:kuma ./build/ ./build/ +COPY --chown=kuma:kuma build.js build.js +RUN node build.js $TARGETPLATFORM + +FROM debian:bookworm-slim AS release +RUN useradd --create-home kuma +USER kuma +WORKDIR /home/kuma +COPY --from=build /home/kuma/uptime-kuma-push ./uptime-kuma-push + +ENTRYPOINT ["/home/kuma/uptime-kuma-push"] + + diff --git a/extra/uptime-kuma-push/build.js b/extra/uptime-kuma-push/build.js new file mode 100644 index 0000000000..9b44e1665f --- /dev/null +++ b/extra/uptime-kuma-push/build.js @@ -0,0 +1,49 @@ +const childProcess = require("child_process"); +const fs = require("fs"); +const platform = process.argv[2]; + +if (!platform) { + console.error("No platform??"); + process.exit(1); +} + +const supportedPlatforms = [ + { + name: "linux/amd64", + bin: "./build/uptime-kuma-push-amd64" + }, + { + name: "linux/arm64", + bin: "./build/uptime-kuma-push-arm64" + }, + { + name: "linux/arm/v7", + bin: "./build/uptime-kuma-push-armv7" + } +]; + +let platformObj = null; + +// Check if the platform is supported +for (let i = 0; i < supportedPlatforms.length; i++) { + if (supportedPlatforms[i].name === platform) { + platformObj = supportedPlatforms[i]; + break; + } +} + +if (platformObj) { + let filename = platformObj.bin; + + if (!fs.existsSync(filename)) { + console.error(`prebuilt: ${filename} is not found, please build it first`); + process.exit(1); + } + + fs.renameSync(filename, "./uptime-kuma-push"); + process.exit(0); +} else { + console.error("Unsupported platform: " + platform); + process.exit(1); +} + diff --git a/extra/uptime-kuma-push/package.json b/extra/uptime-kuma-push/package.json new file mode 100644 index 0000000000..f215436c87 --- /dev/null +++ b/extra/uptime-kuma-push/package.json @@ -0,0 +1,13 @@ +{ + "scripts": { + "build-docker": "npm run build-all && docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/uptime-kuma:push . --push --target release", + "build-all": "npm run build-win && npm run build-linux-amd64 && npm run build-linux-arm64 && npm run build-linux-armv7 && npm run build-linux-armv6 && npm run build-linux-armv5 && npm run build-linux-riscv64", + "build-win": "cross-env GOOS=windows GOARCH=amd64 go build -x -o ./build/uptime-kuma-push.exe uptime-kuma-push.go", + "build-linux-amd64": "cross-env GOOS=linux GOARCH=amd64 go build -x -o ./build/uptime-kuma-push-amd64 uptime-kuma-push.go", + "build-linux-arm64": "cross-env GOOS=linux GOARCH=arm64 go build -x -o ./build/uptime-kuma-push-arm64 uptime-kuma-push.go", + "build-linux-armv7": "cross-env GOOS=linux GOARCH=arm GOARM=7 go build -x -o ./build/uptime-kuma-push-armv7 uptime-kuma-push.go", + "build-linux-armv6": "cross-env GOOS=linux GOARCH=arm GOARM=6 go build -x -o ./build/uptime-kuma-push-armv6 uptime-kuma-push.go", + "build-linux-armv5": "cross-env GOOS=linux GOARCH=arm GOARM=5 go build -x -o ./build/uptime-kuma-push-armv5 uptime-kuma-push.go", + "build-linux-riscv64": "cross-env GOOS=linux GOARCH=riscv64 go build -x -o ./build/uptime-kuma-push-riscv64 uptime-kuma-push.go" + } +} diff --git a/extra/uptime-kuma-push/uptime-kuma-push.go b/extra/uptime-kuma-push/uptime-kuma-push.go new file mode 100644 index 0000000000..69cd1f8c27 --- /dev/null +++ b/extra/uptime-kuma-push/uptime-kuma-push.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "net/http" + os "os" + "time" +) + +func main() { + if len(os.Args) < 2 { + fmt.Fprintln(os.Stderr, "Usage: uptime-kuma-push []") + os.Exit(1) + } + + pushURL := os.Args[1] + + var interval time.Duration + + if len(os.Args) >= 3 { + intervalString, err := time.ParseDuration(os.Args[2] + "s") + interval = intervalString + + if err != nil { + fmt.Fprintln(os.Stderr, "Error: Invalid interval", err) + os.Exit(1) + } + + } else { + interval = 60 * time.Second + } + + for { + _, err := http.Get(pushURL) + if err == nil { + fmt.Print("Pushed!") + } else { + fmt.Print("Error: ", err) + } + + fmt.Println(" Sleeping for", interval) + time.Sleep(interval) + } +} diff --git a/src/pages/Details.vue b/src/pages/Details.vue index 7e4baf3d23..8362f0cf31 100644 --- a/src/pages/Details.vue +++ b/src/pages/Details.vue @@ -96,7 +96,6 @@ - From 77327a6636260821c88f22afc1b5b2c12f0e4e51 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Fri, 15 Sep 2023 17:35:40 +0800 Subject: [PATCH 3/6] WIP --- extra/push-examples/bash-curl/index.sh | 1 + extra/push-examples/powershell/index.ps1 | 1 + extra/uptime-kuma-push/build.js | 1 - 3 files changed, 2 insertions(+), 1 deletion(-) diff --git a/extra/push-examples/bash-curl/index.sh b/extra/push-examples/bash-curl/index.sh index 54b7626cc1..3031255f42 100644 --- a/extra/push-examples/bash-curl/index.sh +++ b/extra/push-examples/bash-curl/index.sh @@ -1,4 +1,5 @@ #!/bin/bash +# Filename: index.sh PUSH_URL="https://example.com/api/push/key?status=up&msg=OK&ping=" INTERVAL=60 diff --git a/extra/push-examples/powershell/index.ps1 b/extra/push-examples/powershell/index.ps1 index b3f0159e5b..2894e8dd6b 100644 --- a/extra/push-examples/powershell/index.ps1 +++ b/extra/push-examples/powershell/index.ps1 @@ -1,3 +1,4 @@ +# Filename: index.ps1 $pushURL = "https://example.com/api/push/key?status=up&msg=OK&ping=" $interval = 60 diff --git a/extra/uptime-kuma-push/build.js b/extra/uptime-kuma-push/build.js index 9b44e1665f..3dc8bf43b6 100644 --- a/extra/uptime-kuma-push/build.js +++ b/extra/uptime-kuma-push/build.js @@ -1,4 +1,3 @@ -const childProcess = require("child_process"); const fs = require("fs"); const platform = process.argv[2]; From ea3406ca2f9c3769e86711706bfe2ba012d3cd9b Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Fri, 15 Sep 2023 17:49:09 +0800 Subject: [PATCH 4/6] Translate --- src/lang/en.json | 3 +++ src/pages/Details.vue | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lang/en.json b/src/lang/en.json index 93f505db94..67c7a092e1 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -84,6 +84,9 @@ "Push URL": "Push URL", "needPushEvery": "You should call this URL every {0} seconds.", "pushOptionalParams": "Optional parameters: {0}", + "pushViewCode": "View Code", + "pushOthers": "Others", + "programmingLanguages": "Programming Languages", "Save": "Save", "Notifications": "Notifications", "Not available, please setup.": "Not available, please setup.", diff --git a/src/pages/Details.vue b/src/pages/Details.vue index 8362f0cf31..c4a83196c3 100644 --- a/src/pages/Details.vue +++ b/src/pages/Details.vue @@ -78,12 +78,12 @@
- View Code + {{ $t("pushViewCode") }}