Compare commits

...

5 commits

Author SHA1 Message Date
Nelson Chan
cdc65f3bbc
Merge 9e2c66657d into 46d8744fa4 2024-10-27 05:22:35 +00:00
Louis Lam
46d8744fa4
Fix: Docker Healthcheck is not happy during migration (#5258)
Some checks are pending
Auto Test / auto-test (18, ARM64) (push) Blocked by required conditions
Auto Test / auto-test (18, macos-latest) (push) Blocked by required conditions
Auto Test / auto-test (18, ubuntu-latest) (push) Blocked by required conditions
Auto Test / auto-test (18, windows-latest) (push) Blocked by required conditions
Auto Test / auto-test (20, ARM64) (push) Blocked by required conditions
Auto Test / auto-test (20, macos-latest) (push) Blocked by required conditions
Auto Test / auto-test (20, ubuntu-latest) (push) Blocked by required conditions
Auto Test / auto-test (20, windows-latest) (push) Blocked by required conditions
Auto Test / armv7-simple-test (18, ARMv7) (push) Waiting to run
Auto Test / armv7-simple-test (20, ARMv7) (push) Waiting to run
Auto Test / check-linters (push) Waiting to run
Auto Test / e2e-test (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Merge Conflict Labeler / Labeling (push) Waiting to run
json-yaml-validate / json-yaml-validate (push) Waiting to run
json-yaml-validate / check-lang-json (push) Waiting to run
2024-10-27 13:22:23 +08:00
Louis Lam
7d8dc55dbe
Fix: the rootless user put in the wrong place (#5257) 2024-10-27 11:47:30 +08:00
Nelson Chan
9e2c66657d
Chore: Fix lint 2024-02-12 06:33:22 +08:00
Nelson Chan
63add0376e
Feat: Impl. server default appearance by injected JSON 2024-02-12 06:22:41 +08:00
10 changed files with 583 additions and 177 deletions

View file

@ -27,7 +27,6 @@ RUN mkdir ./data
# ⭐ Main Image
############################################
FROM $BASE_IMAGE AS release
USER node
WORKDIR /app
LABEL org.opencontainers.image.source="https://github.com/louislam/uptime-kuma"
@ -46,6 +45,7 @@ CMD ["node", "server/server.js"]
# Rootless Image
############################################
FROM release AS rootless
USER node
############################################
# Mark as Nightly

View file

@ -9,6 +9,7 @@ const mysql = require("mysql2/promise");
const { Settings } = require("./settings");
const { UptimeCalculator } = require("./uptime-calculator");
const dayjs = require("dayjs");
const { SimpleMigrationServer } = require("./utils/simple-migration-server");
/**
* Database & App Data Folder
@ -382,9 +383,11 @@ class Database {
/**
* Patch the database
* @param {number} port Start the migration server for aggregate tables on this port if provided
* @param {string} hostname Start the migration server for aggregate tables on this hostname if provided
* @returns {Promise<void>}
*/
static async patch() {
static async patch(port = undefined, hostname = undefined) {
// Still need to keep this for old versions of Uptime Kuma
if (Database.dbConfig.type === "sqlite") {
await this.patchSqlite();
@ -409,7 +412,7 @@ class Database {
await R.exec("PRAGMA foreign_keys = ON");
}
await this.migrateAggregateTable();
await this.migrateAggregateTable(port, hostname);
} catch (e) {
// Allow missing patch files for downgrade or testing pr.
@ -735,9 +738,11 @@ class Database {
* Normally, it should be in transaction, but UptimeCalculator wasn't designed to be in transaction before that.
* I don't want to heavily modify the UptimeCalculator, so it is not in transaction.
* Run `npm run reset-migrate-aggregate-table-state` to reset, in case the migration is interrupted.
* @param {number} port Start the migration server on this port if provided
* @param {string} hostname Start the migration server on this hostname if provided
* @returns {Promise<void>}
*/
static async migrateAggregateTable() {
static async migrateAggregateTable(port, hostname = undefined) {
log.debug("db", "Enter Migrate Aggregate Table function");
// Add a setting for 2.0.0-dev users to skip this migration
@ -758,6 +763,18 @@ class Database {
throw new Error("Aggregate table migration is already in progress");
}
/**
* Start migration server for displaying the migration status
* @type {SimpleMigrationServer}
*/
let migrationServer;
let msg;
if (port) {
migrationServer = new SimpleMigrationServer();
await migrationServer.start(port, hostname);
}
await Settings.set("migrateAggregateTableState", "migrating");
log.info("db", "Migrating Aggregate Table");
@ -777,6 +794,7 @@ class Database {
let count = countResult.count;
if (count > 0) {
log.warn("db", `Aggregate table ${table} is not empty, migration will not be started (Maybe you were using 2.0.0-dev?)`);
await migrationServer?.stop();
return;
}
}
@ -811,7 +829,9 @@ class Database {
`, [ monitor.monitor_id, date.date ]);
if (heartbeats.length > 0) {
log.info("db", `[DON'T STOP] Migrating monitor data ${monitor.monitor_id} - ${date.date} [${progressPercent.toFixed(2)}%][${i}/${monitors.length}]`);
msg = `[DON'T STOP] Migrating monitor data ${monitor.monitor_id} - ${date.date} [${progressPercent.toFixed(2)}%][${i}/${monitors.length}]`;
log.info("db", msg);
migrationServer?.update(msg);
}
for (let heartbeat of heartbeats) {
@ -829,9 +849,13 @@ class Database {
i++;
}
await Database.clearHeartbeatData(true);
msg = "Clearing non-important heartbeats";
log.info("db", msg);
migrationServer?.update(msg);
await Database.clearHeartbeatData(true);
await Settings.set("migrateAggregateTableState", "migrated");
await migrationServer?.stop();
if (monitors.length > 0) {
log.info("db", "Aggregate Table Migration Completed");

View file

@ -313,6 +313,9 @@ let needSetup = false;
const statusPageRouter = require("./routers/status-page-router");
app.use(statusPageRouter);
const { injectDefaultAppearance } = require("./utils/inject-default-appearance");
app.use(injectDefaultAppearance);
// Universal Route Handler, must be at the end of all express routes.
app.get("*", async (_request, response) => {
if (_request.originalUrl.startsWith("/upload/")) {
@ -1716,7 +1719,7 @@ async function initDatabase(testMode = false) {
log.info("server", "Connected to the database");
// Patch the database
await Database.patch();
await Database.patch(port, hostname);
let jwtSecretBean = await R.findOne("setting", " `key` = ? ", [
"jwtSecret",

View file

@ -0,0 +1,50 @@
const { Settings } = require("../settings");
const cheerio = require("cheerio");
const jsesc = require("jsesc");
const { log } = require("../../src/util");
const injectDefaultAppearance = (req, res, next) => {
try {
// Intercept send() calls and inject Default Appearance
// https://stackoverflow.com/a/60817116
const oldSend = res.send;
res.send = async (data) => {
if (typeof data === "string") {
log.debug("inject-default-appearance", req.method + " " + req.url);
const $ = cheerio.load(data);
const defaultAppearance = await Settings.get("defaultAppearance");
if (defaultAppearance) {
const head = $("head");
const escapedJSONObject = jsesc(defaultAppearance, { isScriptContext: true });
const script = $(`
<script id="default-appearance" data-json="{}">
window.defaultAppearance = ${escapedJSONObject};
</script>
`);
head.append(script);
data = $.root().html();
}
}
res.send = oldSend; // set function back to avoid 'double-send'
return res.send(data);
};
next();
} catch (e) {
next(e);
}
};
module.exports = {
injectDefaultAppearance
};

View file

@ -0,0 +1,84 @@
const express = require("express");
const http = require("node:http");
const { log } = require("../../src/util");
/**
* SimpleMigrationServer
* For displaying the migration status of the server
* Also, it is used to let Docker healthcheck know the status of the server, as the main server is not started yet, healthcheck will think the server is down incorrectly.
*/
class SimpleMigrationServer {
/**
* Express app instance
* @type {?Express}
*/
app;
/**
* Server instance
* @type {?Server}
*/
server;
/**
* Response object
* @type {?Response}
*/
response;
/**
* Start the server
* @param {number} port Port
* @param {string} hostname Hostname
* @returns {Promise<void>}
*/
start(port, hostname) {
this.app = express();
this.server = http.createServer(this.app);
this.app.get("/", (req, res) => {
res.set("Content-Type", "text/plain");
res.write("Migration is in progress, listening message...\n");
if (this.response) {
this.response.write("Disconnected\n");
this.response.end();
}
this.response = res;
// never ending response
});
return new Promise((resolve) => {
this.server.listen(port, hostname, () => {
if (hostname) {
log.info("migration", `Migration server is running on http://${hostname}:${port}`);
} else {
log.info("migration", `Migration server is running on http://localhost:${port}`);
}
resolve();
});
});
}
/**
* Update the message
* @param {string} msg Message to update
* @returns {void}
*/
update(msg) {
this.response?.write(msg + "\n");
}
/**
* Stop the server
* @returns {Promise<void>}
*/
async stop() {
this.response?.write("Finished, please refresh this page.\n");
this.response?.end();
await this.server?.close();
}
}
module.exports = {
SimpleMigrationServer,
};

View file

@ -0,0 +1,73 @@
<template>
<div>
<ul class="nav nav-tabs">
<li v-for="(tab, index) in tabs" :key="index" class="nav-item">
<a
class="nav-link"
:class="{ active: index == selected }"
href="#"
@click="$emit('update:selected', index)"
>
{{ tab }}
</a>
</li>
</ul>
</div>
</template>
<script lang="js">
export default {
props: {
tabs: {
type: Array[String],
required: true,
},
selected: {
type: Number,
required: true,
},
},
emits: [ "update:selected" ],
data() {
return {};
},
};
</script>
<style lang="scss" scoped>
@import "../assets/vars.scss";
.nav-tabs {
border-bottom: 1px solid $primary;
.nav-item {
flex-grow: 1;
.nav-link {
text-align: center;
border-top-left-radius: 10px;
border-top-right-radius: 10px;
&.active {
background-color: $highlight-white;
.dark & {
color: $dark-font-color;
background-color: $dark-header-bg;
}
border-color: transparent transparent $primary transparent;
border-width: 1px 1px 6px 1px;
}
&:hover {
.dark & {
background-color: $dark-header-bg;
}
border-color: $primary;
border-width: 1px 1px 6px 1px;
}
}
}
}
</style>

View file

@ -1,186 +1,97 @@
<template>
<div>
<div class="my-4">
<label for="language" class="form-label">
{{ $t("Language") }}
</label>
<select id="language" v-model="$root.language" class="form-select">
<option
v-for="(lang, i) in $i18n.availableLocales"
:key="`Lang${i}`"
:value="lang"
>
{{ $i18n.messages[lang].languageName }}
</option>
</select>
</div>
<div class="my-4">
<label for="timezone" class="form-label">{{ $t("Theme") }}</label>
<div>
<div
class="btn-group"
role="group"
aria-label="Basic checkbox toggle button group"
>
<input
id="btncheck1"
v-model="$root.userTheme"
type="radio"
class="btn-check"
name="theme"
autocomplete="off"
value="light"
/>
<label class="btn btn-outline-primary" for="btncheck1">
{{ $t("Light") }}
</label>
<input
id="btncheck2"
v-model="$root.userTheme"
type="radio"
class="btn-check"
name="theme"
autocomplete="off"
value="dark"
/>
<label class="btn btn-outline-primary" for="btncheck2">
{{ $t("Dark") }}
</label>
<input
id="btncheck3"
v-model="$root.userTheme"
type="radio"
class="btn-check"
name="theme"
autocomplete="off"
value="auto"
/>
<label class="btn btn-outline-primary" for="btncheck3">
{{ $t("Auto") }}
</label>
</div>
</div>
</div>
<div class="my-4">
<label class="form-label">{{ $t("Theme - Heartbeat Bar") }}</label>
<div>
<div
class="btn-group"
role="group"
aria-label="Basic checkbox toggle button group"
>
<input
id="btncheck4"
v-model="$root.userHeartbeatBar"
type="radio"
class="btn-check"
name="heartbeatBarTheme"
autocomplete="off"
value="normal"
/>
<label class="btn btn-outline-primary" for="btncheck4">
{{ $t("Normal") }}
</label>
<input
id="btncheck5"
v-model="$root.userHeartbeatBar"
type="radio"
class="btn-check"
name="heartbeatBarTheme"
autocomplete="off"
value="bottom"
/>
<label class="btn btn-outline-primary" for="btncheck5">
{{ $t("Bottom") }}
</label>
<input
id="btncheck6"
v-model="$root.userHeartbeatBar"
type="radio"
class="btn-check"
name="heartbeatBarTheme"
autocomplete="off"
value="none"
/>
<label class="btn btn-outline-primary" for="btncheck6">
{{ $t("None") }}
</label>
</div>
</div>
</div>
<!-- Timeline -->
<div class="my-4">
<label class="form-label">{{ $t("styleElapsedTime") }}</label>
<div>
<div class="btn-group" role="group">
<input
id="styleElapsedTimeShowNoLine"
v-model="$root.styleElapsedTime"
type="radio"
class="btn-check"
name="styleElapsedTime"
autocomplete="off"
value="no-line"
/>
<label class="btn btn-outline-primary" for="styleElapsedTimeShowNoLine">
{{ $t("styleElapsedTimeShowNoLine") }}
</label>
<input
id="styleElapsedTimeShowWithLine"
v-model="$root.styleElapsedTime"
type="radio"
class="btn-check"
name="styleElapsedTime"
autocomplete="off"
value="with-line"
/>
<label class="btn btn-outline-primary" for="styleElapsedTimeShowWithLine">
{{ $t("styleElapsedTimeShowWithLine") }}
</label>
<input
id="styleElapsedTimeNone"
v-model="$root.styleElapsedTime"
type="radio"
class="btn-check"
name="styleElapsedTime"
autocomplete="off"
value="none"
/>
<label class="btn btn-outline-primary" for="styleElapsedTimeNone">
{{ $t("None") }}
</label>
</div>
</div>
</div>
<HorizontalTabHeader
v-model:selected="storageLoc"
class="mt-4"
:tabs="['Browser', 'Server']"
/>
<AppearanceSettings
v-if="storageLoc == 0"
v-model:language="$root.language"
v-model:userTheme="$root.userTheme"
v-model:userHeartbeatBar="$root.userHeartbeatBar"
v-model:styleElapsedTime="$root.styleElapsedTime"
:languages="languagesList"
/>
<AppearanceSettings
v-if="storageLoc == 1"
:language="settings.defaultAppearance.language || ''"
:userTheme="settings.defaultAppearance.theme || ''"
:userHeartbeatBar="settings.defaultAppearance.heartbeatBarTheme || ''"
:styleElapsedTime="settings.defaultAppearance.styleElapsedTime || ''"
:languages="languagesList"
@update:language="updateDefaultLanguage"
@update:user-theme="updateDefaultTheme"
@update:user-heartbeat-bar="updateDefaultHeartbeatBarTheme"
@update:style-elapsed-time="updateDefaultStyleElapsedTime"
/>
</div>
</template>
<script>
export default {
import HorizontalTabHeader from "../HorizontalTabHeader.vue";
import AppearanceSettings from "./AppearanceSettings.vue";
export default {
components: {
HorizontalTabHeader,
AppearanceSettings
},
data() {
return {
storageLoc: 0,
languagesList: this.$i18n.availableLocales.map(lang => {
return {
value: lang,
label: this.$i18n.messages[lang].languageName,
};
})
};
},
computed: {
settings() {
return this.$parent.$parent.$parent.settings;
},
saveSettings() {
return this.$parent.$parent.$parent.saveSettings;
}
},
methods: {
updateDefaultLanguage(language) {
this.updateDefaultAppearance({
...this.settings.defaultAppearance,
language
});
},
updateDefaultTheme(theme) {
this.updateDefaultAppearance({
...this.settings.defaultAppearance,
theme: theme
});
},
updateDefaultHeartbeatBarTheme(theme) {
this.updateDefaultAppearance({
...this.settings.defaultAppearance,
heartbeatBarTheme: theme
});
},
updateDefaultStyleElapsedTime(styleElapsedTime) {
this.updateDefaultAppearance({
...this.settings.defaultAppearance,
styleElapsedTime
});
},
updateDefaultAppearance(appearance) {
this.$parent.$parent.$parent.settings.defaultAppearance = appearance;
this.saveSettings();
}
}
};
</script>
<style lang="scss" scoped>
@import "../../assets/vars.scss";
.btn-check:active + .btn-outline-primary,
.btn-check:checked + .btn-outline-primary,
.btn-check:hover + .btn-outline-primary {
color: #fff;
.dark & {
color: #000;
}
}
.dark {
.list-group-item {
background-color: $dark-bg2;

View file

@ -0,0 +1,234 @@
<template>
<div>
<div class="my-4">
<label for="language" class="form-label">
{{ $t("Language") }}
</label>
<select
id="language"
:value="language"
class="form-select"
@input="$emit('update:language', $event.target.value)"
>
<option
v-for="(lang, i) in languages"
:key="`Lang${i}`"
:value="lang.value"
>
{{ lang.label }}
</option>
</select>
</div>
<div class="my-4">
<label for="timezone" class="form-label">{{ $t("Theme") }}</label>
<div>
<div
class="btn-group"
role="group"
aria-label="Basic checkbox toggle button group"
>
<input
id="btncheck1"
:checked="userTheme == 'light'"
type="radio"
class="btn-check"
name="theme"
autocomplete="off"
value="light"
@input="$emit('update:userTheme', 'light')"
/>
<label class="btn btn-outline-primary" for="btncheck1">
{{ $t("Light") }}
</label>
<input
id="btncheck2"
:checked="userTheme == 'dark'"
type="radio"
class="btn-check"
name="theme"
autocomplete="off"
value="dark"
@input="$emit('update:userTheme', 'dark')"
/>
<label class="btn btn-outline-primary" for="btncheck2">
{{ $t("Dark") }}
</label>
<input
id="btncheck3"
:checked="userTheme == 'auto'"
type="radio"
class="btn-check"
name="theme"
autocomplete="off"
value="auto"
@input="$emit('update:userTheme', 'auto')"
/>
<label class="btn btn-outline-primary" for="btncheck3">
{{ $t("Auto") }}
</label>
</div>
</div>
</div>
<div class="my-4">
<label class="form-label">{{ $t("Theme - Heartbeat Bar") }}</label>
<div>
<div
class="btn-group"
role="group"
aria-label="Basic checkbox toggle button group"
>
<input
id="btncheck4"
:checked="userHeartbeatBar == 'normal'"
type="radio"
class="btn-check"
name="heartbeatBarTheme"
autocomplete="off"
value="normal"
@input="$emit('update:userHeartbeatBar', 'normal')"
/>
<label class="btn btn-outline-primary" for="btncheck4">
{{ $t("Normal") }}
</label>
<input
id="btncheck5"
:checked="userHeartbeatBar == 'bottom'"
type="radio"
class="btn-check"
name="heartbeatBarTheme"
autocomplete="off"
value="bottom"
@input="$emit('update:userHeartbeatBar', 'bottom')"
/>
<label class="btn btn-outline-primary" for="btncheck5">
{{ $t("Bottom") }}
</label>
<input
id="btncheck6"
:checked="userHeartbeatBar == 'none'"
type="radio"
class="btn-check"
name="heartbeatBarTheme"
autocomplete="off"
value="none"
@input="$emit('update:userHeartbeatBar', 'none')"
/>
<label class="btn btn-outline-primary" for="btncheck6">
{{ $t("None") }}
</label>
</div>
</div>
</div>
<!-- Timeline -->
<div class="my-4">
<label class="form-label">{{ $t("styleElapsedTime") }}</label>
<div>
<div class="btn-group" role="group">
<input
id="styleElapsedTimeShowNoLine"
:checked="styleElapsedTime == 'no-line'"
type="radio"
class="btn-check"
name="styleElapsedTime"
autocomplete="off"
value="no-line"
@input="$emit('update:styleElapsedTime', 'no-line')"
/>
<label
class="btn btn-outline-primary"
for="styleElapsedTimeShowNoLine"
>
{{ $t("styleElapsedTimeShowNoLine") }}
</label>
<input
id="styleElapsedTimeShowWithLine"
:checked="styleElapsedTime == 'with-line'"
type="radio"
class="btn-check"
name="styleElapsedTime"
autocomplete="off"
value="with-line"
@input="$emit('update:styleElapsedTime', 'with-line')"
/>
<label
class="btn btn-outline-primary"
for="styleElapsedTimeShowWithLine"
>
{{ $t("styleElapsedTimeShowWithLine") }}
</label>
<input
id="styleElapsedTimeNone"
:checked="styleElapsedTime == 'none'"
type="radio"
class="btn-check"
name="styleElapsedTime"
autocomplete="off"
value="none"
@input="$emit('update:styleElapsedTime', 'none')"
/>
<label
class="btn btn-outline-primary"
for="styleElapsedTimeNone"
>
{{ $t("None") }}
</label>
</div>
</div>
</div>
</div>
</template>
<script lang="js">
export default {
props: {
languages: {
type: Array,
required: true,
},
language: {
type: String,
required: true,
},
userTheme: {
type: String,
required: true,
},
userHeartbeatBar: {
type: String,
required: true,
},
styleElapsedTime: {
type: String,
required: true,
},
},
emits: [
"update:language",
"update:userTheme",
"update:userHeartbeatBar",
"update:styleElapsedTime",
],
data() {
return {};
},
};
</script>
<style lang="scss" scoped>
.btn-check:active + .btn-outline-primary,
.btn-check:checked + .btn-outline-primary,
.btn-check:hover + .btn-outline-primary {
color: #fff;
.dark & {
color: #000;
}
}
</style>

View file

@ -13,6 +13,24 @@ export default {
},
mounted() {
if (window.defaultAppearance) {
if (window.defaultAppearance.language) {
localStorage.locale = window.defaultAppearance.language;
}
if (window.defaultAppearance.theme) {
this.userTheme = window.defaultAppearance.theme;
}
if (window.defaultAppearance.heartbeatBarTheme) {
this.userHeartbeatBar = window.defaultAppearance.heartbeatBarTheme;
}
if (window.defaultAppearance.styleElapsedTime) {
this.styleElapsedTime = window.defaultAppearance.styleElapsedTime;
}
}
// Default Light
if (! this.userTheme) {
this.userTheme = "auto";

View file

@ -183,6 +183,15 @@ export default {
this.settings.trustProxy = false;
}
if (this.settings.defaultAppearance === undefined) {
this.settings.defaultAppearance = {
theme: null,
language: null,
heartbeatBarTheme: null,
styleElapsedTime: null,
};
}
this.settingsLoaded = true;
});
},