支持从server加载配置,关闭welcome

This commit is contained in:
Pan Qiancheng 2025-04-15 11:30:37 +08:00
parent f77425bfef
commit cfeb99e38e
8 changed files with 69 additions and 27 deletions

View File

@ -66,7 +66,7 @@ type Service struct {
func NewService() *Service {
tasks := []Task{
NewConfigManagerTask(), // 添加配置管理器任务
NewWelcomeTask(),
// NewWelcomeTask(),
NewUnixSocketTask(),
}

View File

@ -5,6 +5,7 @@ import (
"os"
"bash_go_service/config-loader/internal/models"
"bash_go_service/shared/pkg/client"
"bash_go_service/shared/pkg/constants"
"bash_go_service/shared/pkg/logger"
)
@ -56,6 +57,20 @@ func LoadConfigFromFile(filename string) (*models.JSONConfig, error) {
return &config, nil
}
func LoadConfigFromServer() (*models.JSONConfig, error) {
var config models.JSONConfig
cli := client.NewClient()
err := cli.Get(constants.GetConfigApi, nil, &config)
if err != nil {
logger.Error("Failed to read config file: %v", err)
return nil, err
}
logger.Info("Configuration successfully loaded from file with skip rules added")
return &config, nil
}
func ConvertToCConfig(jsonConfig *models.JSONConfig) *models.ConfigData {
logger.Debug("Converting JSON config to C config")

View File

@ -10,6 +10,8 @@ import (
"bash_go_service/config-loader/internal/shm"
"bash_go_service/config-loader/internal/utils"
"bash_go_service/shared/pkg/logger"
"github.com/spf13/viper"
)
// ConfigManager 负责配置的加载和同步
@ -104,10 +106,19 @@ func (cm *ConfigManager) syncFromFile() error {
cm.mu.Lock()
defer cm.mu.Unlock()
logger.Debug("Loading configuration from file: %s", cm.configFile)
// 从文件加载 JSON 配置
jsonConfig, err := utils.LoadConfigFromFile(cm.configFile)
loader := viper.GetString("bash_config.loader")
var jsonConfig *models.JSONConfig
var err error
if loader == "SERVER" {
logger.Info("Loading configuration from server")
jsonConfig, err = utils.LoadConfigFromServer()
} else if loader == "FILE" {
logger.Info("Loading configuration from file")
jsonConfig, err = utils.LoadConfigFromFile(cm.configFile)
} else {
logger.Info("No loader specified, loading configuration from server by default")
jsonConfig, err = utils.LoadConfigFromServer()
}
if err != nil {
return fmt.Errorf("failed to load config file: %w", err)
}

View File

@ -1,4 +1,6 @@
machine_registry:
endpoint: "http://localhost:9900"
endpoint: "http://localhost:3001/endpoint"
log:
level: DEBUG
level: DEBUG
bash_config:
loader: SERVER

View File

@ -8,6 +8,8 @@ import (
"time"
"bash_go_service/shared/pkg/constants"
"github.com/spf13/viper"
)
type Client struct {
@ -53,7 +55,7 @@ func NewClient(opts ...ClientOption) *Client {
ctx, cancel := context.WithCancel(context.Background())
client := &Client{
baseURL: constants.BaseUrl,
baseURL: viper.GetString("machine_registry.endpoint"),
ctx: ctx,
cancel: cancel,
client: &http.Client{

View File

@ -3,13 +3,13 @@ package constants
import "time"
const (
BaseUrl = "http://localhost:9956"
ConnectionTimeout = 5 * time.Second
KeepaliveTimeout = 30 * time.Second
IdleConnTimeout = 10 * time.Second
ClientTimeout = 30 * time.Second
MachineInfoApi = "/machine/info"
MachineInfoApi = "/machine"
GetVersionApi = "/version"
QuestionStreamApi = "/question"
DownloadNewApi = "/download"
GetConfigApi = "/config"
)

View File

@ -5,20 +5,28 @@ import (
"os"
"os/exec"
"path/filepath"
"strconv"
)
func main() {
if len(os.Args) < 4 {
fmt.Println("Usage: updater <oldPath> <newPath> <targetPath>")
if len(os.Args) < 5 {
fmt.Println("Usage: updater <oldPath> <newPath> <targetPath> <shouldStart>")
os.Exit(1)
}
oldPath := os.Args[1]
newPath := os.Args[2]
targetPath := os.Args[3]
shouldStartStr := os.Args[4]
shouldStart, err := strconv.ParseBool(shouldStartStr)
if err != nil {
fmt.Printf("Invalid value for shouldStart: %v\n", err)
os.Exit(1)
}
// 替换当前程序
err := os.Rename(newPath, targetPath)
err = os.Rename(newPath, targetPath)
if err != nil {
fmt.Printf("Failed to replace executable: %v\n", err)
_ = os.Rename(oldPath, targetPath)
@ -35,19 +43,23 @@ func main() {
os.Exit(1)
}
// 启动新程序
cmd := exec.Command(targetPath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = filepath.Dir(targetPath)
if shouldStart {
// 启动新程序
cmd := exec.Command(targetPath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = filepath.Dir(targetPath)
err = cmd.Start()
if err != nil {
fmt.Printf("Failed to start new process: %v\n", err)
// 回滚
_ = os.Rename(oldPath, targetPath)
os.Exit(1)
err = cmd.Start()
if err != nil {
fmt.Printf("Failed to start new process: %v\n", err)
// 回滚
_ = os.Rename(oldPath, targetPath)
os.Exit(1)
}
fmt.Println("New process started successfully.")
} else {
fmt.Println("Replacement completed. New process not started (shouldStart = false).")
}
fmt.Println("New process started successfully.")
}

View File

@ -95,7 +95,7 @@ func DoUpdate() {
// 启动 updater 进行替换
updaterPath := filepath.Join(exeDir, updaterExeName)
cmd := exec.Command(updaterPath, backupPath, newExePath, exePath)
cmd := exec.Command(updaterPath, backupPath, newExePath, exePath, "true")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr