Skip to content

Commit

Permalink
feat: add gt-manual
Browse files Browse the repository at this point in the history
Signed-off-by: 117503445 <[email protected]>
  • Loading branch information
117503445 committed Dec 23, 2023
1 parent 110ec29 commit 1bb4423
Show file tree
Hide file tree
Showing 40 changed files with 7,019 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,6 @@ docker compose up -d

[miao-plugin](https://github.com/yoimiya-kokomi/miao-plugin.git)

[GT-Manual](https://gitee.com/QQ1146638442/GT-Manual)

[chatgpt-web](https://github.com/Chanzhaoyu/chatgpt-web)
9 changes: 9 additions & 0 deletions src/GT-Manual/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM node:lts-slim AS runtime

WORKDIR /workspace/GT-Manual

COPY . .

RUN npm install

ENTRYPOINT [ "node", "app" ]
674 changes: 674 additions & 0 deletions src/GT-Manual/LICENSE

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions src/GT-Manual/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 米游社手动验证

原仓库 <https://gitee.com/QQ1146638442/GT-Manual/tree/master>

* 此demo仅提供思路,可以参考demo自行编写内容至您的站点

* ~~使用Yunzai-Bot可以参考此[提交](https://gitee.com/QQ1146638442/Miao-Yunzai/commit/c286fd0627253f8790912ad5802d6da0b9192774)~~
使用Miao-Yunzai可以参考此插件[GT-Manual-plugin](https://static.hlhs-nb.cn/upload/GT-Manual-plugin.zip)
若您不想部署demo或没有公网地址可以使用他人提供的公益接口

* 值得一提的是QQ不允许直接访问IP地址
需要添加域名解析或复制地址到浏览器访问

* 若外部网络无法访问地址请前往防火墙放行对应端口,具体操作需自行百度

# 测试

```
git clone https://gitee.com/QQ1146638442/GT-Manual.git
cd GT-Manual
npm install
```
修改config.yaml配置
```
# 前台启动
node app
# 后台启动
npm start
# 显示log
npm run log
```
4 changes: 4 additions & 0 deletions src/GT-Manual/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import server from './lib/server.js'

/** 全局变量 Server */
global.Server = await server.run()
8 changes: 8 additions & 0 deletions src/GT-Manual/config/GTest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 您对外显示的域名或IP地址
Host: 127.0.0.1

# 为/register添加?key=114514校验,留空则不校验
Key: ''

# HTML页脚标签内容
Copyright: 'Copyright GT-Manual'
Empty file added src/GT-Manual/config/SSL/.keep
Empty file.
16 changes: 16 additions & 0 deletions src/GT-Manual/config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# server地址,一般不用修改
HOST: '0.0.0.0'

# http端口
HTTP_PORT: 8081
# https端口
HTTPS_PORT: 443

# 使用https
HTTPS: false
# http重定向https
HTTP_TO_HTTPS: true
# ssl证书crt文件路径
CA_CERTIFICATE: './config/SSL/certificate.crt'
# ssl证书key文件路径
CA_PRIVATE: './config/SSL/private.key'
14 changes: 14 additions & 0 deletions src/GT-Manual/config/redis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 是否连接redis
open: false

# redis地址
host: localhost

# redis端口
port: 6379

# redis密码,没有密码请留空
password: ''

# redis数据库
db: 0
16 changes: 16 additions & 0 deletions src/GT-Manual/docker-compose-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
services:
gt-manaual:
image: node:lts-slim
# build:
# context: .
# dockerfile: Dockerfile
ports:
- "8081:8081"
volumes:
- ./:/workspace/GT-Manual

working_dir: /workspace/GT-Manual
entrypoint: bash

stdin_open: true
tty: true
30 changes: 30 additions & 0 deletions src/GT-Manual/lib/config/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import fs from 'node:fs'
import { exec } from 'node:child_process'

export async function checkRun () {
if (process.argv[1].includes('pm2')) return
if (process.argv[1].includes('test')) return

let cfg = pm2Cfg()
let status = await execSync(`pm2 show ${cfg.apps[0].name}`)

if (status.stdout.includes('online')) {
logger.mark('检测到后台正在运行')
logger.mark('已停止后台进程,防止重复运行')
execSync(`pm2 stop ${cfg.apps[0].name}`)
}
}

async function execSync (cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
resolve({ error, stdout, stderr })
})
})
}

function pm2Cfg () {
let cfg = fs.readFileSync('pm2.json')
cfg = JSON.parse(cfg)
return cfg
}
73 changes: 73 additions & 0 deletions src/GT-Manual/lib/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import YAML from 'yaml'
import fs from 'node:fs'
import chokidar from 'chokidar'

/** 配置文件 */
class Cfg {
/** 初始化配置 */
constructor () {
let file = 'config/config.yaml'
let fileDef = 'config/default_config.yaml'
if (!fs.existsSync(file)) {
fs.copyFileSync(fileDef, file)
}
if (!fs.existsSync('data')) fs.mkdirSync('data')
this.config = {}
}

get http () {
return this.getConfig('config')
}

get http_listen () {
return [this.http.HTTPS ? this.http.HTTPS_PORT : this.http.HTTP_PORT, this.http.HOST]
}

https_address (originalUrl, hostname) {
let Url = new URL(originalUrl, `https://${hostname}`)
Url.port = this.http.HTTPS_PORT
return Url.href
}

get redis () {
return this.getConfig('redis')
}

get cert() {
return {
cert: fs.readFileSync(this.http.CA_CERTIFICATE, 'utf8'),
key: fs.readFileSync(this.http.CA_PRIVATE, 'utf8')
}
}

/** package.json */
get package () {
if (this._package) return this._package

this._package = JSON.parse(fs.readFileSync('package.json', 'utf8'))
return this._package
}

/** 用户配置 */
getConfig (name) {
return this.getYaml(name)
}

/**
* 获取配置yaml
* @param type 默认跑配置-defSet,用户配置-config
* @param name 名称
*/
getYaml (name) {
let file = `config/${name}.yaml`
if (this.config[name]) return this.config[name]

this.config[name] = YAML.parse(
fs.readFileSync(file, 'utf8')
)

return this.config[name]
}
}

export default new Cfg()
57 changes: 57 additions & 0 deletions src/GT-Manual/lib/config/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import setLog from './log.js'
import cfg from "./config.js"
import redisInit from './redis.js'
import { checkRun } from './check.js'
import fs from 'node:fs'

/** 设置标题 */
process.title = cfg.package.name
/** 设置时区 */
process.env.TZ = 'Asia/Shanghai'

/** 捕获未处理的错误 */
process.on('uncaughtException', (error) => {
let err = error
if (logger) {
logger.error(err)
} else {
console.log(err)
}
})

/** 捕获未处理的Promise错误 */
process.on('unhandledRejection', (error, promise) => {
let err = error
if (logger) {
logger.error(err)
} else {
console.log(err)
}
})

/** 退出事件 */
process.on('exit', async (code) => {
if (typeof redis != 'undefined' && typeof test == 'undefined') {
await redis.save()
}
})

await checkInit()

/** 初始化事件 */
async function checkInit () {
/** 检查node_modules */
if (!fs.existsSync('./node_modules') || !fs.existsSync('./node_modules/express')) {
console.log('请先pnpm install -P安装')
process.exit()
}

/** 日志设置 */
setLog()

await redisInit()

await checkRun()

logger.info(`${cfg.package.name} 启动中...`)
}
98 changes: 98 additions & 0 deletions src/GT-Manual/lib/config/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import log4js from 'log4js'
import chalk from 'chalk'
import fs from 'node:fs'

/**
* 设置日志样式
*/
export default function setLog () {
let file = './logs'
let log_level = 'info'
if (!fs.existsSync(file)) {
fs.mkdirSync(file)
}

/** 调整error日志等级 */
// log4js.levels.levels[5].level = Number.MAX_VALUE
// log4js.levels.levels.sort((a, b) => a.level - b.level)

log4js.configure({
appenders: {
console: {
type: 'console',
layout: {
type: 'pattern',
pattern: '%[[Server][%d{hh:mm:ss}][%4.4p]%] %m'
}
},
command: {
type: 'dateFile', // 可以是console,dateFile,file,Logstash等
filename: 'logs/command', // 将会按照filename和pattern拼接文件名
pattern: 'yyyy-MM-dd.log',
numBackups: 15,
alwaysIncludePattern: true,
layout: {
type: 'pattern',
pattern: '[%d{hh:mm:ss}][%4.4p] %m'
}
},
error: {
type: 'file',
filename: 'logs/error.log',
alwaysIncludePattern: true,
layout: {
type: 'pattern',
pattern: '[%d{hh:mm:ss}][%4.4p] %m'
}
}
},
categories: {
default: { appenders: ['console'], level: log_level },
command: { appenders: ['console', 'command'], level: 'info' },
error: { appenders: ['console', 'command', 'error'], level: 'error' }
}
})

const defaultLogger = log4js.getLogger('message')
const commandLogger = log4js.getLogger('command')
const errorLogger = log4js.getLogger('error')

/* eslint-disable no-useless-call */
/** 全局变量 logger */
global.logger = {
trace () {
defaultLogger.trace.call(defaultLogger, ...arguments)
},
debug () {
defaultLogger.debug.call(defaultLogger, ...arguments)
},
info () {
defaultLogger.info.call(commandLogger, ...arguments)
},
// warn及以上的日志采用error策略
warn () {
commandLogger.warn.call(defaultLogger, ...arguments)
},
error () {
errorLogger.error.call(errorLogger, ...arguments)
},
fatal () {
errorLogger.fatal.call(errorLogger, ...arguments)
},
mark () {
errorLogger.mark.call(commandLogger, ...arguments)
}
}

logColor()
}

function logColor () {
logger.chalk = chalk
logger.red = chalk.red
logger.green = chalk.green
logger.yellow = chalk.yellow
logger.blue = chalk.blue
logger.magenta = chalk.magenta
logger.cyan = chalk.cyan
}
Loading

0 comments on commit 1bb4423

Please sign in to comment.