支持从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 { func NewService() *Service {
tasks := []Task{ tasks := []Task{
NewConfigManagerTask(), // 添加配置管理器任务 NewConfigManagerTask(), // 添加配置管理器任务
NewWelcomeTask(), // NewWelcomeTask(),
NewUnixSocketTask(), NewUnixSocketTask(),
} }

View File

@ -5,6 +5,7 @@ import (
"os" "os"
"bash_go_service/config-loader/internal/models" "bash_go_service/config-loader/internal/models"
"bash_go_service/shared/pkg/client"
"bash_go_service/shared/pkg/constants" "bash_go_service/shared/pkg/constants"
"bash_go_service/shared/pkg/logger" "bash_go_service/shared/pkg/logger"
) )
@ -56,6 +57,20 @@ func LoadConfigFromFile(filename string) (*models.JSONConfig, error) {
return &config, nil 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 { func ConvertToCConfig(jsonConfig *models.JSONConfig) *models.ConfigData {
logger.Debug("Converting JSON config to C config") 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/shm"
"bash_go_service/config-loader/internal/utils" "bash_go_service/config-loader/internal/utils"
"bash_go_service/shared/pkg/logger" "bash_go_service/shared/pkg/logger"
"github.com/spf13/viper"
) )
// ConfigManager 负责配置的加载和同步 // ConfigManager 负责配置的加载和同步
@ -104,10 +106,19 @@ func (cm *ConfigManager) syncFromFile() error {
cm.mu.Lock() cm.mu.Lock()
defer cm.mu.Unlock() defer cm.mu.Unlock()
logger.Debug("Loading configuration from file: %s", cm.configFile) loader := viper.GetString("bash_config.loader")
var jsonConfig *models.JSONConfig
// 从文件加载 JSON 配置 var err error
jsonConfig, err := utils.LoadConfigFromFile(cm.configFile) 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 { if err != nil {
return fmt.Errorf("failed to load config file: %w", err) return fmt.Errorf("failed to load config file: %w", err)
} }

View File

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

View File

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

View File

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

View File

@ -5,20 +5,28 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strconv"
) )
func main() { func main() {
if len(os.Args) < 4 { if len(os.Args) < 5 {
fmt.Println("Usage: updater <oldPath> <newPath> <targetPath>") fmt.Println("Usage: updater <oldPath> <newPath> <targetPath> <shouldStart>")
os.Exit(1) os.Exit(1)
} }
oldPath := os.Args[1] oldPath := os.Args[1]
newPath := os.Args[2] newPath := os.Args[2]
targetPath := os.Args[3] 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 { if err != nil {
fmt.Printf("Failed to replace executable: %v\n", err) fmt.Printf("Failed to replace executable: %v\n", err)
_ = os.Rename(oldPath, targetPath) _ = os.Rename(oldPath, targetPath)
@ -35,6 +43,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
if shouldStart {
// 启动新程序 // 启动新程序
cmd := exec.Command(targetPath) cmd := exec.Command(targetPath)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
@ -50,4 +59,7 @@ func main() {
} }
fmt.Println("New process started successfully.") fmt.Println("New process started successfully.")
} else {
fmt.Println("Replacement completed. New process not started (shouldStart = false).")
}
} }

View File

@ -95,7 +95,7 @@ func DoUpdate() {
// 启动 updater 进行替换 // 启动 updater 进行替换
updaterPath := filepath.Join(exeDir, updaterExeName) 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.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr