add 配置读取写入

This commit is contained in:
SummerTail 2023-08-01 13:02:12 +08:00
parent c4f88a5368
commit a0d7b9b17f
6 changed files with 72 additions and 4 deletions

5
config.json Normal file
View File

@ -0,0 +1,5 @@
{
"downloadPath": "",
"vCookies": "",
"torrentSavePath": ""
}

View File

@ -2,7 +2,7 @@ const Koa = require('koa');
const app = new Koa();
const static = require('koa-static')
app.use(static("./view/dist"))
const { koaBody } = require('koa-body');
const {koaBody} = require('koa-body');
const KoaSSEStream = require('koa-sse-stream');
app.use(koaBody());
@ -20,7 +20,9 @@ app.use(async (ctx, next) => {
const index = require("./router/index.js")
app.use(index.routes()).use(index.allowedMethods())
const videoInfo = require("./router/videoInfo.js");
const config = require("./router/config.js")
app.use(config.routes()).use(config.allowedMethods())
app.use(videoInfo.routes()).use(videoInfo.allowedMethods())
app.listen(3050, "0.0.0.0");

32
router/config.js Normal file
View File

@ -0,0 +1,32 @@
const Router = require("koa-router")
const config = require("../util/config/config")
const router = new Router({
prefix: "/config"
})
/**
* 获取配置文件接口
*/
router.get('/getConfig', async (ctx) => {
let json = config.getConfig();
const b = json === '{}'
return {
code: b ? -1 : 0,
msg: b ? 'ok' : "error",
data: json
}
})
/**
* 设置配置文件接口
*/
router.get('/setConfig', async (ctx) => {
let b = config.setConfig(ctx.data);
return {
code: b ? -1 : 0,
msg: b ? 'ok' : "error",
data: null
}
})
module.exports = router

30
util/config/config.js Normal file
View File

@ -0,0 +1,30 @@
"use strict";
const fs = require("fs");
const {cwd} = require("process");
const path = require('path');
let configPath = path.join(cwd(), "/config.json");
/**
* 获取配置文件
* @returns {JSON}
*/
function getConfig() {
let file = fs.readFileSync(configPath);
return JSON.parse(file);
}
/**
* 保存配置文件
* @param string {JSON}
* @return {boolean}
*/
function setConfig(string) {
fs.writeFileSync("config.json", JSON.stringify(string, null, 2));
return getConfig() === string;
}
module.exports = {
getConfig,
setConfig
}

File diff suppressed because one or more lines are too long

View File

@ -6,8 +6,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Vue</title>
<meta name="referrer" content="never">
<script type="module" crossorigin src="./assets/index-b619957d.js"></script>
<link rel="stylesheet" href="./assets/index-6ef103ac.css">
<script type="module" crossorigin src="./assets/index-ccfbebb1.js"></script>
<link rel="stylesheet" href="./assets/index-e0450ab1.css">
</head>
<body>
<div id="app"></div>