diff --git a/config/config.example.yml b/config/config.example.yml index acd76807..5a27eccb 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -8,7 +8,7 @@ app: version: 2.3.0 root: /www http: - debug: true + debug: false port: 8888 entrance: / tls: false diff --git a/internal/bootstrap/conf.go b/internal/bootstrap/conf.go index 0b4a4972..0b5713d0 100644 --- a/internal/bootstrap/conf.go +++ b/internal/bootstrap/conf.go @@ -2,18 +2,36 @@ package bootstrap import ( "fmt" + "os" + "path/filepath" + "strings" "time" - "github.com/knadh/koanf/parsers/yaml" - "github.com/knadh/koanf/providers/file" "github.com/knadh/koanf/v2" "github.com/TheTNB/panel/internal/app" + "github.com/knadh/koanf/parsers/yaml" + "github.com/knadh/koanf/providers/file" ) func initConf() { + executable, err := os.Executable() + if err != nil { + panic(fmt.Sprintf("failed to get executable: %v", err)) + } + res, err := filepath.EvalSymlinks(filepath.Dir(executable)) + if err != nil { + panic(fmt.Sprintf("failed to get executable path: %v", err)) + } + if isTesting() || isAir() || isDirectlyRun() { + res, err = os.Getwd() + if err != nil { + panic(fmt.Sprintf("failed to get working directory: %v", err)) + } + } + app.Conf = koanf.New(".") - if err := app.Conf.Load(file.Provider("config/config.yml"), yaml.Parser()); err != nil { + if err = app.Conf.Load(file.Provider(filepath.Join(res, "config/config.yml")), yaml.Parser()); err != nil { panic(fmt.Sprintf("failed to load config: %v", err)) } } @@ -30,3 +48,32 @@ func initGlobal() { } time.Local = loc } + +// isTesting checks if the application is running in testing mode. +func isTesting() bool { + for _, arg := range os.Args { + if strings.Contains(arg, "-test.") { + return true + } + } + + return false +} + +// isAir checks if the application is running using Air. +func isAir() bool { + for _, arg := range os.Args { + if strings.Contains(filepath.ToSlash(arg), "/storage/temp") { + return true + } + } + + return false +} + +// isDirectlyRun checks if the application is running using go run. +func isDirectlyRun() bool { + executable, _ := os.Executable() + return strings.Contains(filepath.Base(executable), os.TempDir()) || + (strings.Contains(filepath.ToSlash(executable), "/var/folders") && strings.Contains(filepath.ToSlash(executable), "/T/go-build")) // macOS +} diff --git a/internal/data/app.go b/internal/data/app.go index bbdcc783..94be7e6e 100644 --- a/internal/data/app.go +++ b/internal/data/app.go @@ -182,7 +182,7 @@ func (r *appRepo) Install(channel, slug string) error { task := new(biz.Task) task.Name = "安装应用 " + item.Name task.Status = biz.TaskStatusWaiting - task.Shell = fmt.Sprintf(`curl -s "%s" | bash -s -- "%s" "%s" >> /tmp/%s.log 2>&1`, shellUrl, shellChannel, shellVersion, item.Slug) + task.Shell = fmt.Sprintf(`curl -f -s --connect-timeout 10 --retry 3 "%s" | bash -s -- "%s" "%s" >> /tmp/%s.log 2>&1`, shellUrl, shellChannel, shellVersion, item.Slug) task.Log = "/tmp/" + item.Slug + ".log" if err = r.taskRepo.Push(task); err != nil { return err @@ -236,7 +236,7 @@ func (r *appRepo) Uninstall(slug string) error { task := new(biz.Task) task.Name = "卸载应用 " + item.Name task.Status = biz.TaskStatusWaiting - task.Shell = fmt.Sprintf(`curl -s "%s" | bash -s -- "%s" "%s" >> /tmp/%s.log 2>&1`, shellUrl, shellChannel, shellVersion, item.Slug) + task.Shell = fmt.Sprintf(`curl -f -s --connect-timeout 10 --retry 3 "%s" | bash -s -- "%s" "%s" >> /tmp/%s.log 2>&1`, shellUrl, shellChannel, shellVersion, item.Slug) task.Log = "/tmp/" + item.Slug + ".log" if err = r.taskRepo.Push(task); err != nil { return err @@ -290,7 +290,7 @@ func (r *appRepo) Update(slug string) error { task := new(biz.Task) task.Name = "更新应用 " + item.Name task.Status = biz.TaskStatusWaiting - task.Shell = fmt.Sprintf(`curl -s "%s" | bash -s -- "%s" "%s" >> /tmp/%s.log 2>&1`, shellUrl, shellChannel, shellVersion, item.Slug) + task.Shell = fmt.Sprintf(`curl -f -s --connect-timeout 10 --retry 3 "%s" | bash -s -- "%s" "%s" >> /tmp/%s.log 2>&1`, shellUrl, shellChannel, shellVersion, item.Slug) task.Log = "/tmp/" + item.Slug + ".log" if err = r.taskRepo.Push(task); err != nil { return err diff --git a/internal/data/setting.go b/internal/data/setting.go index e1c2b72a..bf61f5d1 100644 --- a/internal/data/setting.go +++ b/internal/data/setting.go @@ -122,7 +122,7 @@ func (r *settingRepo) UpdatePanelSetting(ctx context.Context, setting *request.P restartFlag := false config := new(types.PanelConfig) cm := yaml.CommentMap{} - raw, err := io.Read("config/config.yml") + raw, err := io.Read(filepath.Join(app.Root, "panel/config/config.yml")) if err != nil { return false, err } @@ -139,7 +139,7 @@ func (r *settingRepo) UpdatePanelSetting(ctx context.Context, setting *request.P if err != nil { return false, err } - if err = io.Write("config/config.yml", string(encoded), 0644); err != nil { + if err = io.Write(filepath.Join(app.Root, "panel/config/config.yml"), string(encoded), 0644); err != nil { return false, err } if raw != string(encoded) { diff --git a/internal/service/cli.go b/internal/service/cli.go index da236753..b251f296 100644 --- a/internal/service/cli.go +++ b/internal/service/cli.go @@ -8,15 +8,18 @@ import ( "path/filepath" "github.com/go-rat/utils/hash" + "github.com/goccy/go-yaml" "github.com/gookit/color" "github.com/urfave/cli/v3" "github.com/TheTNB/panel/internal/app" "github.com/TheTNB/panel/internal/biz" "github.com/TheTNB/panel/internal/data" + "github.com/TheTNB/panel/pkg/io" "github.com/TheTNB/panel/pkg/str" "github.com/TheTNB/panel/pkg/systemctl" "github.com/TheTNB/panel/pkg/tools" + "github.com/TheTNB/panel/pkg/types" ) type CliService struct { @@ -289,5 +292,26 @@ func (s *CliService) Init(ctx context.Context, cmd *cli.Command) error { return fmt.Errorf("初始化失败: %v", err) } + config := new(types.PanelConfig) + cm := yaml.CommentMap{} + raw, err := io.Read(filepath.Join(app.Root, "panel/config/config.yml")) + if err != nil { + return err + } + if err = yaml.UnmarshalWithOptions([]byte(raw), &config, yaml.CommentToMap(cm)); err != nil { + return err + } + + config.App.Key = str.RandomString(32) + config.HTTP.Entrance = "/" + str.RandomString(6) + + encoded, err := yaml.MarshalWithOptions(config, yaml.WithComment(cm)) + if err != nil { + return err + } + if err = io.Write(filepath.Join(app.Root, "panel/config/config.yml"), string(encoded), 0644); err != nil { + return err + } + return nil }