Skip to content

Commit

Permalink
feat: webui
Browse files Browse the repository at this point in the history
  • Loading branch information
BIYUEHU committed Feb 15, 2024
1 parent 6ba5738 commit 3743694
Show file tree
Hide file tree
Showing 18 changed files with 329 additions and 94 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
},
"extends": [
"airbnb-base",
"prettier"
"prettier",
"typescript"
],
"parser": "@typescript-eslint/parser",
"plugins": [
Expand Down
26 changes: 13 additions & 13 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

## Packages

- [x] @kotori-bot/webui
- [x] @kotori-bot/kotori-plugin-webui
- [x] @kotori-bot/kotori-plugin-adapter-sandbox
- [x] @kotori-bot/kotori-plugin-adapter-mail
- [x] @kotori-bot/kotori-plugin-adapter-telegram
- [x] kotori-plugin-adapter-minecraft
- [x] kotori-plugin-requester
- [x] kotori-plugin-grouper
- [x] kotori-plugin-manger
- [x] @kotori-bot/kotori-plugin-alias
- [x] create-kotori
- [] @kotori-bot/webui
- [] @kotori-bot/kotori-plugin-webui
- [] @kotori-bot/kotori-plugin-adapter-sandbox
- [] @kotori-bot/kotori-plugin-adapter-mail
- [] @kotori-bot/kotori-plugin-adapter-telegram
- [] kotori-plugin-adapter-minecraft
- [] kotori-plugin-requester
- [] kotori-plugin-grouper
- [] kotori-plugin-manger
- [] @kotori-bot/kotori-plugin-alias
- [] create-kotori

## Features

- [x] ctx.filter()
- [x] message.schedule()
- [] ctx.filter()
- [] message.schedule()
Binary file modified kotori.db
Binary file not shown.
4 changes: 2 additions & 2 deletions modules/status/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Blog: https://hotaru.icu
* @Date: 2023-07-11 14:18:27
* @LastEditors: Hotaru [email protected]
* @LastEditTime: 2024-02-11 20:20:33
* @LastEditTime: 2024-02-15 17:21:28
*/
import { Context, Tsu } from 'kotori-bot';
import os from 'os';
Expand Down Expand Up @@ -49,7 +49,7 @@ function dealRam() {

function dealCpu() {
const cpuData = os.cpus();
let rate: number = 0;
let rate = 0;
const ratearr: number[] = [];
cpuData.forEach((key) => {
const { times } = key;
Expand Down
19 changes: 19 additions & 0 deletions modules/webui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# kotori-bot-admin-server

This is a website management system specifically designed for the kotori-bot project
Web repository: [https://github.com/BIYUEHU/kotori-bot-admin-web](https://github.com/BIYUEHU/kotori-bot-admin-web)

**Version:** 1.1.0
**Author:** hotaru
**License:** GPL-3.0

## List of command

- /login - Get Kotori-Amdin login url^^

## Lang Support

- ja_JP
- en_US
- zh_TW
- zh_CN
4 changes: 4 additions & 0 deletions modules/webui/locales/en_US.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"kotori_bot_admin_server.help.login": "Get Kotori-Amdin login url",
"kotori_bot_admin_server.msg.login.info": "Here are your login url:\nhttp://127.0.0.1:%port%%path%\n%face_address%:%port%%path%\nLogin url will expire after[%expire_time%Second], dont give others"
}
4 changes: 4 additions & 0 deletions modules/webui/locales/ja_JP.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"kotori_bot_admin_server.help.login": "Kotori-Amdinログインアドレスの取得",
"kotori_bot_admin_server.msg.login.info": "これがあなたのログイン先です:\nhttp://127.0.0.1:%port%%path%\n%face_address%:%port%%path%\nログインアドレスは[%expire_time%秒]後に期限切れになりますので、他人に漏らさないでください"
}
4 changes: 4 additions & 0 deletions modules/webui/locales/zh_CN.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"kotori_bot_admin_server.help.login": "获取Kotori-Amdin后台一键登录地址",
"kotori_bot_admin_server.msg.login.info": "以下是您的BOT后台管理的一键登录地址:\nhttp://127.0.0.1:%port%%path%\n%face_address%:%port%%path%\n登录地址将在[%expire_time%秒]后过期,请勿泄露给他人"
}
4 changes: 4 additions & 0 deletions modules/webui/locales/zh_TW.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"kotori_bot_admin_server.help.login": "獲取Kotori-Amdin後臺一鍵登錄地址",
"kotori_bot_admin_server.msg.login.info": "以下是您的BOT後臺管理的一鍵登錄地址:\nhttp://127.0.0.1:%port%%path%\n%face_address%:%port%%path%\n登錄地址將在[%expire_time%秒]後過期,請勿泄露給他人"
}
35 changes: 35 additions & 0 deletions modules/webui/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@kotori-bot/kotori-plugin-webui",
"version": "1.0.0",
"description": "webui plugin",
"main": "lib/index.js",
"scripts": {
"build": "tsc --build"
},
"keywords": [
"kotori",
"chatbot",
"kotori-plugin"
],
"license": "GPL-3.0",
"files": [
"lib",
"locales",
"LICENSE",
"README.md"
],
"author": "Hotaru <[email protected]>",
"peerDependencies": {
"kotori-bot": "workspace:^"
},
"kotori": {
"meta": {
"language": [
"en_US",
"ja_JP",
"zh_TW",
"zh_CN"
]
}
}
}
26 changes: 26 additions & 0 deletions modules/webui/src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { getUuid } from 'kotori-bot';
import os from 'os';

type Code = 500 | 501 | 502;

export function response<T>(code: Code, data?: T) {
return { code, data };
}
export function getToken() {
return getUuid().replaceAll('-', '');
}

export function getRamRate() {
const total = os.totalmem() / (1024 * 3);
return ((total - os.freemem() / (1024 * 3)) / total) * 100;
}

export function getCpuRate() {
let rate = 0;
os.cpus().forEach((key) => {
const { times } = key;
const usage = (1 - times.idle / (times.idle + times.user + times.nice + times.sys + times.irq)) * 100;
rate += usage;
});
return rate;
}
2 changes: 2 additions & 0 deletions modules/webui/src/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const DEFAULT_USERNAME = 'kotori';
export const DEFAULT_PASSWORD = '123456';
99 changes: 99 additions & 0 deletions modules/webui/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { type Adapter, type Context, Symbols, ModuleConfig, Tsu, stringRightSplit, PLUGIN_PREFIX } from 'kotori-bot';
import path from 'path';
import { getCpuRate, getRamRate, getToken } from './common';
import { DEFAULT_PASSWORD, DEFAULT_USERNAME } from './const';

export const inject = ['server', 'file'];

export const config = Tsu.Object({
username: Tsu.String().default(DEFAULT_USERNAME),
password: Tsu.String().default(DEFAULT_PASSWORD)
});

export function main(ctx: Context, con: Tsu.infer<typeof config>) {
const app = ctx.server;
app.use(app.static(path.join('/web')));
app.use(app.json());

/* eslint @typescript-eslint/no-explicit-any: 0 */
app.all('*', (req: any, res: any, next: any) => {
if (req.path !== '/api/info') ctx.logger.label(req.method).trace(req.path);
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Methods', '*');
res.header('Content-Type', 'application/json;charset=utf-8');
next();
});

const router = app.router();

router.get('/bots', (_, res) => {
const bots: (Adapter['status'] & { platform: string; identity: string; id: string | number; lang: string })[] = [];
ctx[Symbols.bot].forEach((bot) =>
bot.forEach((api) =>
bots.push({
...api.adapter.status,
platform: api.adapter.platform,
identity: api.adapter.identity,
id: api.adapter.selfId,
lang: api.adapter.config.lang
})
)
);
res.status(200).json(bots);
});

router.get('/modules', (_, res) => {
const modules: [object, ModuleConfig][] = [];
ctx[Symbols.modules].forEach((module) => modules.push([module[0].pkg, module[1]]));
res.status(200).json(modules);
});

router.get('/data', (_, res) => {
res.status(200).json({
midwares: ctx[Symbols.midware].size,
commands: ctx[Symbols.command].size,
regexps: ctx[Symbols.regexp].size,
bots: ctx[Symbols.bot].size,
adapters: ctx[Symbols.adapter].size,
modules: ctx[Symbols.modules].size,
dir: ctx.baseDir.root,
pkg: ctx.pkg,
node: process.version
});
});

router.get('/info', (_, res) => {
res.status(200).json({ cpu: getCpuRate(), ram: getRamRate() });
});

router.get('/login', (req, res) => {
const { user, pwd } = req.query;
if (user === con.username && pwd === con.password) {
const token = getToken();
ctx.file.save('token', token);
res.status(200).json({ token, default: con.username === DEFAULT_USERNAME && con.password === DEFAULT_PASSWORD });
} else {
res.status(200).json({});
}
});

router.get('/config', (req, res) => {
const { type, id } = req.query;
if (type === 'module') {
if (typeof id === 'string') {
const split = id.split('/');
const handle = stringRightSplit(split.length > 1 ? split[1] : split[0], PLUGIN_PREFIX);
res.status(200).json(handle in ctx.config.plugin ? ctx.config.plugin[handle] : {});
} else {
res.status(200).json({});
}
} else if (type === 'adapter') {
res.status(200).json(typeof id === 'string' && id in ctx.config.adapter ? ctx.config.adapter[id] : {});
} else {
res.status(200).json(ctx.config);
}
});

app.use('/api', router);
}
12 changes: 12 additions & 0 deletions modules/webui/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.node.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./lib"
},
"references": [
{
"path": "../../packages/kotori"
}
]
}
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@
"@types/inquirer": "^9.0.7",
"@types/js-beautify": "^1.14.3",
"@types/node": "^20.8.7",
"@typescript-eslint/eslint-plugin": "^6.4.0",
"@typescript-eslint/parser": "^6.4.0",
"conventional-changelog-cli": "^4.1.0",
"eslint": "^8.47.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"eslint-config-typescript": "^3.0.0",
"execa": "^8.0.1",
"inquirer": "^9.2.12",
"prettier": "^3.0.2",
Expand All @@ -53,4 +51,4 @@
"version": "pnpm exec conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
"release": "pnpm exec release"
}
}
}
23 changes: 13 additions & 10 deletions packages/loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@
"license": "GPL-3.0",
"main": "lib/index.js",
"author": "Hotaru <[email protected]>",
"dependencies": {
"@kotori-bot/core": "workspace:^",
"@kotori-bot/logger": "workspace:^",
"express": "^4.18.2",
"knex": "^3.1.0",
"mysql": "^2.18.1",
"sqlite3": "^5.1.7"
},
"keywords": [
"kotori",
"chatbot",
Expand All @@ -31,7 +23,18 @@
"url": "git+https://github.com/kotorijs/kotori.git"
},
"homepage": "https://kotori.js.org",
"dependencies": {
"@kotori-bot/core": "workspace:^",
"@kotori-bot/logger": "workspace:^",
"express": "^4.18.2",
"knex": "^3.1.0",
"sqlite3": "^5.1.7"
},
"devDependencies": {
"@types/express": "^4.17.21"
"@types/body-parser": "^1.19.5",
"@types/connect": "^3.4.38",
"@types/express": "^4.17.21",
"@types/express-serve-static-core": "^4.17.43",
"@types/serve-static": "^1.15.5"
}
}
}
Loading

0 comments on commit 3743694

Please sign in to comment.