From 5f89940ab621ea29d44bb568de18b5c1e3cb3ad2 Mon Sep 17 00:00:00 2001 From: LouisLam Date: Sun, 11 Jul 2021 13:47:57 +0800 Subject: [PATCH] add setup page --- db/kuma.db | Bin 0 -> 57344 bytes package.json | 2 +- server/server.js | 68 +++++++++++++++++++++++++--- src/main.js | 9 +++- src/mixins/socket.js | 4 ++ src/pages/Dashboard.vue | 4 +- src/pages/Setup.vue | 95 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 173 insertions(+), 9 deletions(-) create mode 100644 db/kuma.db create mode 100644 src/pages/Setup.vue diff --git a/db/kuma.db b/db/kuma.db new file mode 100644 index 0000000000000000000000000000000000000000..9eeb517ea0d2eec3cf8bc4fcc1184ac10549d1cd GIT binary patch literal 57344 zcmeI&e{0)D7{GBWPOQYKoxli&We{yFG@{*bGRzodFf~zEGuui1V>ATMb7LhDi|y2w z(`Af-n~lASzLLF$y@9=f{X6LX-%j>PmXdgDHw>nH4RPeV)1$l3b0=9?rAIm2v9zOh zr&)Kjk0Q$vMTvZ-X_3ee_ac!r+?NBvWMp=M)$i7H+QAbKLZhlb zycJ8Wt}6DRJH4*eso4#A$V9j!va+fL4qDwf@QU@~Vl1_`rt}7p?B-d!OI zKYr@epPX7=RS@T!Y@uv^ZWgt@Vm5CS_q8v~eXX!t)(Vwe zPG-Y9NU*urL@SyP%%WM?HcS4YTpW=zcWgUm&J>em+bC@tJEmJw+1qiajVu|HZ@*VJ z9wt1JS3Z{B@64;I^>yX)_P{e3Cb+#2_6t`1is0y=hgoFOA~% zL!-E{mC5LG@Q3Zzk$rq)RsGPh&&6K%cXxB9QSd_w*9S%=SJpOV3~zd0gVXyT$5eOe zJU0h}?plsxw~pmMH9S;V`Z}YO$N$E<2n-;d)<<2f6ta&hrfhd!KkWq-L`zxYT%3g} zbo~Q0mNHhA$Y51DFpnvsk{I6q4?R?Leu%I z-tAuRsT{>CPXxzqTHm!>f$8r~C;j$pEOl>X-28X@S=)5q=fqc$dH3tc-b^fY=Z>;J zTj^i(%kCR51Q0*~0R#|0009ILKmY**5O`As-24BzrC%aTe=PmJ z^xM*}Vv`pF2q1s}0tg_000IagfB*srOpn0wTy$LtK1H^$I2&D6 zfB*srAbeQ;`NbfdB#sAb3j7V9Q4KNx literal 0 HcmV?d00001 diff --git a/package.json b/package.json index c45883d92..5a8fdc03c 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "dev": "vite --host", "start-server": "node server/server.js", "update": "", - "build": "npm install && vite build", + "build": "vite build", "vite-preview-dist": "vite preview --host" }, "dependencies": { diff --git a/server/server.js b/server/server.js index a958a247a..7b340372e 100644 --- a/server/server.js +++ b/server/server.js @@ -9,6 +9,7 @@ const {R} = require("redbean-node"); const passwordHash = require('password-hash'); const jwt = require('jsonwebtoken'); const Monitor = require("./model/monitor"); +const fs = require("fs"); const {getSettings} = require("./util-server"); const {Notification} = require("./notification") @@ -17,14 +18,9 @@ app.use(express.json()) let totalClient = 0; let jwtSecret = null; let monitorList = {}; +let needSetup = false; (async () => { - R.setup('sqlite', { - filename: './data/kuma.db' - }); - R.freeze(true) - await R.autoloadModels("./server/model"); - await initDatabase(); app.use('/', express.static("dist")); @@ -44,6 +40,11 @@ let monitorList = {}; console.log('a user connected'); totalClient++; + if (needSetup) { + console.log("Redirect to setup page") + socket.emit("setup") + } + socket.on('disconnect', () => { console.log('user disconnected'); totalClient--; @@ -113,6 +114,40 @@ let monitorList = {}; socket.leave(socket.userID) socket.userID = null; callback(); + }); + + socket.on("needSetup", async (callback) => { + callback(needSetup); + }); + + socket.on("setup", async (username, password, callback) => { + try { + if ((await R.count("user")) !== 0) { + throw new Error("Uptime Kuma has been setup. If you want to setup again, please delete the database.") + } + + let user = R.dispense("user") + user.username = username; + user.password = passwordHash.generate(password) + await R.store(user) + + needSetup = false; + + callback({ + ok: true, + msg: "Added Successfully." + }); + + } catch (e) { + callback({ + ok: false, + msg: e.message + }); + } + + + + }); // Auth Only API @@ -402,6 +437,7 @@ let monitorList = {}; server.listen(3001, () => { console.log('Listening on 3001'); + startMonitors(); }); @@ -489,6 +525,21 @@ function checkLogin(socket) { } async function initDatabase() { + const path = './data/kuma.db'; + + if (! fs.existsSync(path)) { + console.log("Copy Database") + fs.copyFileSync("./db/kuma.db", path); + } + + console.log("Connect to Database") + + R.setup('sqlite', { + filename: path + }); + R.freeze(true) + await R.autoloadModels("./server/model"); + let jwtSecretBean = await R.findOne("setting", " `key` = ? ", [ "jwtSecret" ]); @@ -504,6 +555,11 @@ async function initDatabase() { console.log("Load JWT secret from database.") } + if ((await R.count("user")) === 0) { + console.log("No user, need setup") + needSetup = true; + } + jwtSecret = jwtSecretBean.value; } diff --git a/src/main.js b/src/main.js index 112512e5e..a44bef87c 100644 --- a/src/main.js +++ b/src/main.js @@ -14,6 +14,7 @@ import EditMonitor from "./pages/EditMonitor.vue"; import Toast from "vue-toastification"; import "vue-toastification/dist/index.css"; import "bootstrap" +import Setup from "./pages/Setup.vue"; const routes = [ { @@ -56,8 +57,14 @@ const routes = [ }, ], }, + ], - } + + }, + { + path: '/setup', + component: Setup, + }, ] const router = createRouter({ diff --git a/src/mixins/socket.js b/src/mixins/socket.js index 9c864776c..38d58136c 100644 --- a/src/mixins/socket.js +++ b/src/mixins/socket.js @@ -33,6 +33,10 @@ export default { transports: ['websocket'] }); + socket.on('setup', (monitorID, data) => { + this.$router.push("/setup") + }); + socket.on('monitorList', (data) => { this.monitorList = data; }); diff --git a/src/pages/Dashboard.vue b/src/pages/Dashboard.vue index 9814e2561..d96e14b5e 100644 --- a/src/pages/Dashboard.vue +++ b/src/pages/Dashboard.vue @@ -9,7 +9,9 @@
- No Monitors, please add one. +
+ No Monitors, please add one. +
diff --git a/src/pages/Setup.vue b/src/pages/Setup.vue new file mode 100644 index 000000000..78ff9e633 --- /dev/null +++ b/src/pages/Setup.vue @@ -0,0 +1,95 @@ + + + + +