This commit is contained in:
Hua
2024-08-09 21:46:59 +08:00
commit e40ef6b559
9 changed files with 543 additions and 0 deletions

175
config/web/app.js Normal file
View File

@ -0,0 +1,175 @@
const MessageType = Object.freeze({
SUCCESS: 'success',
ERROR: 'error',
WARNING: 'warning',
INFO: 'info'
});
new Vue({
el: '#app',
data: {
config: {},
message: '',
textContent: '启动',
bgColor: '#3B82F6',
serverRunning: false,
showMessage: false,
messageType: MessageType.SUCCESS,
messageTimer: null,
selectedProtocol: 'http'
},
mounted() {
this.fetchConfig();
},
methods: {
msg(message, type = MessageType.SUCCESS) {
this.message = message;
this.messageType = type;
// 如果已经有一个定时器在运行,先清除它
if (this.messageTimer) {
clearTimeout(this.messageTimer);
}
// 显示消息
this.showMessage = true;
// 设置淡出定时器
this.messageTimer = setTimeout(() => {
this.showMessage = false;
}, 2500); // 2.5秒后开始淡出
},
fetchConfig() {
axios.get('/api/config')
.then(response => {
this.config = this.processConfig(response.data);
})
.catch(error => {
this.msg('获取配置失败: ' + error, MessageType.ERROR);
});
},
processConfig(config) {
const processed = {};
for (const [key, value] of Object.entries(config)) {
if (typeof value === 'object' && value !== null) {
processed[key] = JSON.stringify(value, null, 2);
} else {
processed[key] = value;
}
}
return processed;
},
updateConfig() {
const updatedConfig = this.prepareConfigForUpdate(this.config);
axios.post('/api/config', updatedConfig)
.then(() => {
this.msg('配置更新成功,服务器正在重启 ');
this.restartApp();
})
.catch(error => {
this.msg('获取配置失败: ' + error, MessageType.ERROR);
});
},
prepareConfigForUpdate(config) {
const prepared = {};
for (const [key, value] of Object.entries(config)) {
if (typeof value === 'string' && value.trim().startsWith('{')) {
try {
prepared[key] = JSON.parse(value);
} catch (e) {
console.error(`解析 JSON 失败,键: ${key}:`, e);
prepared[key] = value; // 保持原始字符串
}
} else {
prepared[key] = value;
}
}
return prepared;
},
restartApp() {
axios.post('/api/restart')
.then(() => {
this.msg('应用正在重启');
})
.catch(error => {
this.msg('获取配置失败: ' + error, MessageType.ERROR);
});
},
toggleServer() {
this.serverRunning = !this.serverRunning;
let message = '';
let textContent = '';
let bgColor = '';
let url = '';
let errorMsg = '';
if (this.serverRunning) {
message = '应用正在停止';
textContent = '停止';
bgColor = '#EF4444';
url = '/api/stop';
errorMsg = '停止应用失败: ';
} else {
message = '应用正在启动';
textContent = '启动';
bgColor = '#3B82F6';
url = '/api/start';
errorMsg = '启动应用失败: ';
}
axios.post(url)
.then(() => {
this.textContent = textContent;
this.bgColor = bgColor;
this.msg(message);
})
.catch(error => {
this.msg(errorMsg + error, MessageType.ERROR);
});
},
toggleServerStyle() {
return {
backgroundColor: this.bgColor
}
},
copyVscodeConfig() {
let config = {
"github.copilot.advanced": {
"debug.overrideCAPIUrl": `http://${this.config.bind}/v1`,
"debug.overrideProxyUrl": `http://${this.config.bind}`,
"debug.chatOverrideProxyUrl": `http://${this.config.bind}/v1/chat/completions`,
"authProvider": "github-enterprise"
},
"github-enterprise.uri": "https://cocopilot.org",
};
let configStr = JSON.stringify(config, null, 2);
configStr = configStr.slice(1, -1);
navigator.clipboard.writeText(`${configStr.trimEnd()},\n`)
.then(r => {
this.msg('vscode 配置已复制到剪贴板');
});
},
testConnection() {
const params = new URLSearchParams({
bind: this.config.bind,
protocol: this.selectedProtocol
});
axios.get(`/_ping?${params.toString()}`, { timeout: 5000 })
.then(response => {
this.textContent = '停止';
this.bgColor = '#EF4444';
this.msg(response.data.msg);
})
.catch(error => {
this.textContent = '启动';
this.bgColor = '#3B82F6';
const errorMsg = error.response ? error.response.data.msg : error.message;
this.msg('连接测试失败: ' + errorMsg, MessageType.ERROR);
});
}
}
});

85
config/web/index.html Normal file
View File

@ -0,0 +1,85 @@
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Override-Config-Gui</title>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body class="bg-gray-100">
<div id="app" class="container mx-auto p-4">
<h1 class="text-2xl font-bold mb-4">Override-Config-Gui</h1>
<div class="bg-white rounded-lg shadow-sm p-5 mb-5 px-8 pt-6 pb-8">
<h2 class="text-2xl bg-white font-bold mb-4">服务管理</h2>
<button id="toggleServer"
class="px-5 py-2.5 bg-blue-500 text-white border-none rounded cursor-pointer mr-2.5 hover:bg-blue-600"
:style="toggleServerStyle()" @click="toggleServer">{{ textContent }}</button>
<button id="copyVscodeConfig"
class="px-5 py-2.5 bg-blue-500 text-white border-none rounded cursor-pointer mr-2.5 hover:bg-blue-600"
@click="copyVscodeConfig">复制 vscode 配置</button>
<button id="testConnection"
class="px-5 py-2.5 bg-blue-500 text-white border-none rounded cursor-pointer mr-2.5 hover:bg-blue-600"
@click="testConnection">连通性测试</button>
<div class="inline-block mr-2.5">
<select id="protocolSelect"
class="px-3 py-2.5 bg-white text-gray-700 border border-gray-300 rounded cursor-pointer"
v-model="selectedProtocol">
<option value="http">HTTP</option>
<option value="https">HTTPS</option>
</select>
</div>
</div>
<div class="bg-white rounded-lg shadow-sm p-5 mb-5 px-8 pt-6 pb-8">
<h2 class="text-2xl font-bold mb-4">配置管理</h2>
<template>
<div v-if="config && Object.keys(config).length > 0">
<div v-for="(value, key) in config" :key="key" class="mb-4 flex items-start">
<label class="w-1/3 text-right pr-4 text-gray-700 text-sm font-bold mt-2" :for="key">
{{ key }}
</label>
<div class="w-2/3">
<template v-if="typeof value === 'object' && value !== null">
<textarea
class="w-full shadow appearance-none border rounded py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
:id="key"
v-model="config[key]"
rows="5"
></textarea>
</template>
<template v-else>
<input
class="w-full shadow appearance-none border rounded py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
:id="key"
type="text"
v-model="config[key]"
>
</template>
</div>
</div>
</div>
<div v-else class="text-center text-red-600 font-bold">
配置获取失败
</div>
</template>
<div class="flex items-center justify-end mt-6">
<button @click="updateConfig"
class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
更新配置并重启
</button>
</div>
</div>
<div v-show="showMessage"
:class="['fixed', 'top-5', 'left-1/2', 'transform', '-translate-x-1/2', 'px-4', 'py-2', 'rounded', 'text-white', 'transition-opacity', 'duration-500',
{'bg-green-500': messageType === 'success',
'bg-red-500': messageType === 'error',
'bg-yellow-500': messageType === 'warning',
'opacity-0': !showMessage,
'opacity-100': showMessage}]">
{{ message }}
</div>
</div>
<script src="app.js"></script>
</body>
</html>