diff --git a/db/kuma.db b/db/kuma.db new file mode 100644 index 0000000..9eeb517 Binary files /dev/null and b/db/kuma.db differ diff --git a/package.json b/package.json index c45883d..5a8fdc0 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 a958a24..7b34037 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 112512e..a44bef8 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 9c86477..38d5813 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 9814e25..d96e14b 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 0000000..78ff9e6 --- /dev/null +++ b/src/pages/Setup.vue @@ -0,0 +1,95 @@ + + + + +