mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 10:17:17 +08:00
feat: 优化配置加载
This commit is contained in:
@@ -8,7 +8,7 @@ app:
|
|||||||
version: 2.3.0
|
version: 2.3.0
|
||||||
root: /www
|
root: /www
|
||||||
http:
|
http:
|
||||||
debug: true
|
debug: false
|
||||||
port: 8888
|
port: 8888
|
||||||
entrance: /
|
entrance: /
|
||||||
tls: false
|
tls: false
|
||||||
|
|||||||
@@ -2,18 +2,36 @@ package bootstrap
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/knadh/koanf/parsers/yaml"
|
|
||||||
"github.com/knadh/koanf/providers/file"
|
|
||||||
"github.com/knadh/koanf/v2"
|
"github.com/knadh/koanf/v2"
|
||||||
|
|
||||||
"github.com/TheTNB/panel/internal/app"
|
"github.com/TheTNB/panel/internal/app"
|
||||||
|
"github.com/knadh/koanf/parsers/yaml"
|
||||||
|
"github.com/knadh/koanf/providers/file"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initConf() {
|
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(".")
|
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))
|
panic(fmt.Sprintf("failed to load config: %v", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -30,3 +48,32 @@ func initGlobal() {
|
|||||||
}
|
}
|
||||||
time.Local = loc
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -182,7 +182,7 @@ func (r *appRepo) Install(channel, slug string) error {
|
|||||||
task := new(biz.Task)
|
task := new(biz.Task)
|
||||||
task.Name = "安装应用 " + item.Name
|
task.Name = "安装应用 " + item.Name
|
||||||
task.Status = biz.TaskStatusWaiting
|
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"
|
task.Log = "/tmp/" + item.Slug + ".log"
|
||||||
if err = r.taskRepo.Push(task); err != nil {
|
if err = r.taskRepo.Push(task); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -236,7 +236,7 @@ func (r *appRepo) Uninstall(slug string) error {
|
|||||||
task := new(biz.Task)
|
task := new(biz.Task)
|
||||||
task.Name = "卸载应用 " + item.Name
|
task.Name = "卸载应用 " + item.Name
|
||||||
task.Status = biz.TaskStatusWaiting
|
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"
|
task.Log = "/tmp/" + item.Slug + ".log"
|
||||||
if err = r.taskRepo.Push(task); err != nil {
|
if err = r.taskRepo.Push(task); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -290,7 +290,7 @@ func (r *appRepo) Update(slug string) error {
|
|||||||
task := new(biz.Task)
|
task := new(biz.Task)
|
||||||
task.Name = "更新应用 " + item.Name
|
task.Name = "更新应用 " + item.Name
|
||||||
task.Status = biz.TaskStatusWaiting
|
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"
|
task.Log = "/tmp/" + item.Slug + ".log"
|
||||||
if err = r.taskRepo.Push(task); err != nil {
|
if err = r.taskRepo.Push(task); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ func (r *settingRepo) UpdatePanelSetting(ctx context.Context, setting *request.P
|
|||||||
restartFlag := false
|
restartFlag := false
|
||||||
config := new(types.PanelConfig)
|
config := new(types.PanelConfig)
|
||||||
cm := yaml.CommentMap{}
|
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 {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
@@ -139,7 +139,7 @@ func (r *settingRepo) UpdatePanelSetting(ctx context.Context, setting *request.P
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
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
|
return false, err
|
||||||
}
|
}
|
||||||
if raw != string(encoded) {
|
if raw != string(encoded) {
|
||||||
|
|||||||
@@ -8,15 +8,18 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/go-rat/utils/hash"
|
"github.com/go-rat/utils/hash"
|
||||||
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/gookit/color"
|
"github.com/gookit/color"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
|
|
||||||
"github.com/TheTNB/panel/internal/app"
|
"github.com/TheTNB/panel/internal/app"
|
||||||
"github.com/TheTNB/panel/internal/biz"
|
"github.com/TheTNB/panel/internal/biz"
|
||||||
"github.com/TheTNB/panel/internal/data"
|
"github.com/TheTNB/panel/internal/data"
|
||||||
|
"github.com/TheTNB/panel/pkg/io"
|
||||||
"github.com/TheTNB/panel/pkg/str"
|
"github.com/TheTNB/panel/pkg/str"
|
||||||
"github.com/TheTNB/panel/pkg/systemctl"
|
"github.com/TheTNB/panel/pkg/systemctl"
|
||||||
"github.com/TheTNB/panel/pkg/tools"
|
"github.com/TheTNB/panel/pkg/tools"
|
||||||
|
"github.com/TheTNB/panel/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CliService struct {
|
type CliService struct {
|
||||||
@@ -289,5 +292,26 @@ func (s *CliService) Init(ctx context.Context, cmd *cli.Command) error {
|
|||||||
return fmt.Errorf("初始化失败: %v", err)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user