init
This commit is contained in:
37
.gitignore
vendored
Normal file
37
.gitignore
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
# Reference https://github.com/github/gitignore/blob/master/Go.gitignore
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
vendor/
|
||||
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# OS General
|
||||
Thumbs.db
|
||||
.DS_Store
|
||||
|
||||
# project
|
||||
*.cert
|
||||
*.key
|
||||
*.log
|
||||
bin/
|
||||
config.json
|
||||
|
||||
# Develop tools
|
||||
.vscode/
|
||||
.idea/
|
||||
*.swp
|
42
Dockerfile
Normal file
42
Dockerfile
Normal file
@ -0,0 +1,42 @@
|
||||
# 构建阶段
|
||||
FROM golang:alpine AS builder
|
||||
|
||||
WORKDIR /build
|
||||
|
||||
# 复制整个项目
|
||||
COPY . .
|
||||
|
||||
# 设置 Go 环境
|
||||
ENV GO111MODULE=on GOPROXY=https://goproxy.cn,direct
|
||||
|
||||
# 构建配置管理服务器
|
||||
WORKDIR /build/override-web/config
|
||||
RUN go mod download
|
||||
RUN CGO_ENABLED=0 go build -ldflags="-w -s" -o config-server .
|
||||
|
||||
# 构建原始 override 应用
|
||||
WORKDIR /build/override-web/override
|
||||
RUN go mod download
|
||||
RUN CGO_ENABLED=0 go build -ldflags="-w -s" -o override .
|
||||
|
||||
# 最终镜像
|
||||
FROM alpine:latest
|
||||
|
||||
RUN apk --no-cache add ca-certificates
|
||||
|
||||
# 从构建阶段复制编译好的二进制文件
|
||||
COPY --from=builder /build/override-web/config/config-server /usr/local/bin/
|
||||
COPY --from=builder /build/override-web/override/override /usr/local/bin/
|
||||
|
||||
# 复制配置文件和静态文件
|
||||
COPY override-web/config/web /app/web
|
||||
COPY config.json.example /app/config.json
|
||||
|
||||
WORKDIR /app
|
||||
VOLUME /app
|
||||
|
||||
# 暴露原始应用和配置管理服务器的端口
|
||||
EXPOSE 8181 9090
|
||||
|
||||
# 使用 shell 形式的 CMD,这样可以使用环境变量并同时运行两个应用
|
||||
CMD ["/bin/sh", "-c", "/usr/local/bin/config-server -app /usr/local/bin/override -config /app/config.json & /usr/local/bin/override"]
|
BIN
config/config-manager
Executable file
BIN
config/config-manager
Executable file
Binary file not shown.
18
config/config.go
Normal file
18
config/config.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
func LoadConfig(path string) ([]byte, error) {
|
||||
return ioutil.ReadFile(path)
|
||||
}
|
||||
|
||||
func SaveConfig(path string, config map[string]interface{}) error {
|
||||
data, err := json.MarshalIndent(config, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile(path, data, 0644)
|
||||
}
|
3
config/go.mod
Normal file
3
config/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module config
|
||||
|
||||
go 1.22
|
170
config/main.go
Normal file
170
config/main.go
Normal file
@ -0,0 +1,170 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
configPath string
|
||||
serverAddr string
|
||||
originalApp string
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.StringVar(&configPath, "config", "config.json", "path to config file")
|
||||
flag.StringVar(&serverAddr, "addr", ":9090", "address for config server")
|
||||
flag.StringVar(&originalApp, "app", "./override", "path to the original app")
|
||||
flag.Parse()
|
||||
|
||||
http.HandleFunc("/api/config", handleConfig)
|
||||
http.HandleFunc("/api/restart", handleRestart)
|
||||
http.HandleFunc("/api/start", start)
|
||||
http.HandleFunc("/api/stop", stop)
|
||||
http.HandleFunc("/_ping", handlePing)
|
||||
http.Handle("/", http.FileServer(http.Dir("web")))
|
||||
|
||||
log.Printf("Config server starting on %s", serverAddr)
|
||||
log.Fatal(http.ListenAndServe(serverAddr, nil))
|
||||
}
|
||||
|
||||
func handleConfig(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodGet {
|
||||
config, err := LoadConfig(configPath)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, err = w.Write(config)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else if r.Method == http.MethodPost {
|
||||
var newConfig map[string]interface{}
|
||||
err := json.NewDecoder(r.Body).Decode(&newConfig)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
err = SaveConfig(configPath, newConfig)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
} else {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func handleRestart(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
cmd := exec.Command(originalApp)
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to restart the application", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, err = w.Write([]byte("Application restarted"))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func start(w http.ResponseWriter, r *http.Request) {
|
||||
cmd := exec.Command("override")
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to start override: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func stop(w http.ResponseWriter, r *http.Request) {
|
||||
cmd := exec.Command("pkill", "override")
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to stop override: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func handlePing(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
bindAddress := r.URL.Query().Get("bind")
|
||||
protocol := r.URL.Query().Get("protocol")
|
||||
|
||||
if bindAddress == "" {
|
||||
http.Error(w, "bind address is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if protocol == "" {
|
||||
protocol = "http" // 默认使用http
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s://%s/_ping", protocol, bindAddress)
|
||||
|
||||
client := &http.Client{
|
||||
Timeout: 5 * time.Second,
|
||||
}
|
||||
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
err := json.NewEncoder(w).Encode(map[string]string{
|
||||
"msg": fmt.Sprintf("无法连接到服务器: %v", err),
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
err := Body.Close()
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
}(resp.Body)
|
||||
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
err := json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"msg": "服务器已启动并正常运行",
|
||||
"data": map[string]interface{}{
|
||||
"now": time.Now().Unix(),
|
||||
"ns1": "200 OK",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
w.WriteHeader(http.StatusBadGateway)
|
||||
err := json.NewEncoder(w).Encode(map[string]string{
|
||||
"msg": fmt.Sprintf("服务器响应异常: %s", resp.Status),
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
175
config/web/app.js
Normal file
175
config/web/app.js
Normal 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
85
config/web/index.html
Normal 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>
|
13
docker-compose.yml
Normal file
13
docker-compose.yml
Normal file
@ -0,0 +1,13 @@
|
||||
services:
|
||||
override-app:
|
||||
image: huagcs/override-config-ui:latest
|
||||
container_name: override-config-ui
|
||||
restart: always
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
volumes:
|
||||
- ./config.json:/app/config.json
|
||||
ports:
|
||||
- "8181:8181"
|
||||
- "9090:9090"
|
Reference in New Issue
Block a user