2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 07:57:21 +08:00

feat: 优化配置加载

This commit is contained in:
耗子
2024-10-10 21:14:54 +08:00
parent e02de8ba05
commit d31f40f2dc
5 changed files with 80 additions and 9 deletions

View File

@@ -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
}

View File

@@ -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

View File

@@ -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) {

View File

@@ -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
}