diff --git a/internal/service/app.go b/internal/service/app.go index 977d1ab2..23fd68ab 100644 --- a/internal/service/app.go +++ b/internal/service/app.go @@ -9,6 +9,7 @@ import ( "github.com/TheTNB/panel/internal/data" "github.com/TheTNB/panel/internal/http/request" "github.com/TheTNB/panel/pkg/str" + "github.com/TheTNB/panel/pkg/types" ) type AppService struct { @@ -22,40 +23,30 @@ func NewAppService() *AppService { } func (s *AppService) List(w http.ResponseWriter, r *http.Request) { - plugins := s.appRepo.All() - installedPlugins, err := s.appRepo.Installed() + all := s.appRepo.All() + installedApps, err := s.appRepo.Installed() if err != nil { Error(w, http.StatusInternalServerError, err.Error()) return } - installedPluginsMap := make(map[string]*biz.App) + installedAppMap := make(map[string]*biz.App) - for _, p := range installedPlugins { - installedPluginsMap[p.Slug] = p + for _, p := range installedApps { + installedAppMap[p.Slug] = p } - type plugin struct { - Name string `json:"name"` - Description string `json:"description"` - Slug string `json:"slug"` - Version string `json:"version"` - Installed bool `json:"installed"` - InstalledVersion string `json:"installed_version"` - Show bool `json:"show"` - } - - var pluginArr []plugin - for _, item := range plugins { + var apps []types.StoreApp + for _, item := range all { installed, installedVersion, currentVersion, show := false, "", "", false if str.FirstElement(item.Versions) != nil { currentVersion = str.FirstElement(item.Versions).Version } - if _, ok := installedPluginsMap[item.Slug]; ok { + if _, ok := installedAppMap[item.Slug]; ok { installed = true - installedVersion = installedPluginsMap[item.Slug].Version - show = installedPluginsMap[item.Slug].Show + installedVersion = installedAppMap[item.Slug].Version + show = installedAppMap[item.Slug].Show } - pluginArr = append(pluginArr, plugin{ + apps = append(apps, types.StoreApp{ Name: item.Name, Description: item.Description, Slug: item.Slug, @@ -66,7 +57,7 @@ func (s *AppService) List(w http.ResponseWriter, r *http.Request) { }) } - paged, total := Paginate(r, pluginArr) + paged, total := Paginate(r, apps) Success(w, chix.M{ "total": total, @@ -141,7 +132,7 @@ func (s *AppService) IsInstalled(w http.ResponseWriter, r *http.Request) { return } - plugin, err := s.appRepo.Get(req.Slug) + app, err := s.appRepo.Get(req.Slug) if err != nil { Error(w, http.StatusInternalServerError, err.Error()) return @@ -154,7 +145,7 @@ func (s *AppService) IsInstalled(w http.ResponseWriter, r *http.Request) { } Success(w, chix.M{ - "name": plugin.Name, + "name": app.Name, "installed": installed, }) } diff --git a/internal/service/info.go b/internal/service/info.go index affca2eb..79c53503 100644 --- a/internal/service/info.go +++ b/internal/service/info.go @@ -232,8 +232,8 @@ func (s *InfoService) InstalledDbAndPhp(w http.ResponseWriter, r *http.Request) continue } - plugin, _ := s.appRepo.Get(p.Slug) - phpData = append(phpData, types.LV{Value: strings.ReplaceAll(p.Slug, "php", ""), Label: plugin.Name}) + app, _ := s.appRepo.Get(p.Slug) + phpData = append(phpData, types.LV{Value: strings.ReplaceAll(p.Slug, "php", ""), Label: app.Name}) } if mysqlInstalled { diff --git a/internal/service/monitor.go b/internal/service/monitor.go index 3030e5b4..38b60b0e 100644 --- a/internal/service/monitor.go +++ b/internal/service/monitor.go @@ -9,6 +9,7 @@ import ( "github.com/TheTNB/panel/internal/biz" "github.com/TheTNB/panel/internal/data" "github.com/TheTNB/panel/internal/http/request" + "github.com/TheTNB/panel/pkg/types" ) type MonitorService struct { @@ -70,40 +71,7 @@ func (s *MonitorService) List(w http.ResponseWriter, r *http.Request) { return } - type load struct { - Load1 []float64 `json:"load1"` - Load5 []float64 `json:"load5"` - Load15 []float64 `json:"load15"` - } - type cpu struct { - Percent []string `json:"percent"` - } - type mem struct { - Total string `json:"total"` - Available []string `json:"available"` - Used []string `json:"used"` - } - type swap struct { - Total string `json:"total"` - Used []string `json:"used"` - Free []string `json:"free"` - } - type network struct { - Sent []string `json:"sent"` - Recv []string `json:"recv"` - Tx []string `json:"tx"` - Rx []string `json:"rx"` - } - type monitorData struct { - Times []string `json:"times"` - Load load `json:"load"` - Cpu cpu `json:"cpu"` - Mem mem `json:"mem"` - Swap swap `json:"swap"` - Net network `json:"net"` - } - - var data monitorData + var list types.MonitorData var bytesSent uint64 var bytesRecv uint64 var bytesSent2 uint64 @@ -119,8 +87,8 @@ func (s *MonitorService) List(w http.ResponseWriter, r *http.Request) { // 跳过第一条数据,因为第一条数据的流量为 0 if i == 0 { // MB - data.Mem.Total = fmt.Sprintf("%.2f", float64(monitor.Info.Mem.Total)/1024/1024) - data.Swap.Total = fmt.Sprintf("%.2f", float64(monitor.Info.Swap.Total)/1024/1024) + list.Mem.Total = fmt.Sprintf("%.2f", float64(monitor.Info.Mem.Total)/1024/1024) + list.SWAP.Total = fmt.Sprintf("%.2f", float64(monitor.Info.Swap.Total)/1024/1024) continue } for _, net := range monitor.Info.Net { @@ -130,21 +98,21 @@ func (s *MonitorService) List(w http.ResponseWriter, r *http.Request) { bytesSent2 += net.BytesSent bytesRecv2 += net.BytesRecv } - data.Times = append(data.Times, monitor.CreatedAt.ToDateTimeString()) - data.Load.Load1 = append(data.Load.Load1, monitor.Info.Load.Load1) - data.Load.Load5 = append(data.Load.Load5, monitor.Info.Load.Load5) - data.Load.Load15 = append(data.Load.Load15, monitor.Info.Load.Load15) - data.Cpu.Percent = append(data.Cpu.Percent, fmt.Sprintf("%.2f", monitor.Info.Percent[0])) - data.Mem.Available = append(data.Mem.Available, fmt.Sprintf("%.2f", float64(monitor.Info.Mem.Available)/1024/1024)) - data.Mem.Used = append(data.Mem.Used, fmt.Sprintf("%.2f", float64(monitor.Info.Mem.Used)/1024/1024)) - data.Swap.Used = append(data.Swap.Used, fmt.Sprintf("%.2f", float64(monitor.Info.Swap.Used)/1024/1024)) - data.Swap.Free = append(data.Swap.Free, fmt.Sprintf("%.2f", float64(monitor.Info.Swap.Free)/1024/1024)) - data.Net.Sent = append(data.Net.Sent, fmt.Sprintf("%.2f", float64(bytesSent2/1024/1024))) - data.Net.Recv = append(data.Net.Recv, fmt.Sprintf("%.2f", float64(bytesRecv2/1024/1024))) + list.Times = append(list.Times, monitor.CreatedAt.ToDateTimeString()) + list.Load.Load1 = append(list.Load.Load1, monitor.Info.Load.Load1) + list.Load.Load5 = append(list.Load.Load5, monitor.Info.Load.Load5) + list.Load.Load15 = append(list.Load.Load15, monitor.Info.Load.Load15) + list.CPU.Percent = append(list.CPU.Percent, fmt.Sprintf("%.2f", monitor.Info.Percent[0])) + list.Mem.Available = append(list.Mem.Available, fmt.Sprintf("%.2f", float64(monitor.Info.Mem.Available)/1024/1024)) + list.Mem.Used = append(list.Mem.Used, fmt.Sprintf("%.2f", float64(monitor.Info.Mem.Used)/1024/1024)) + list.SWAP.Used = append(list.SWAP.Used, fmt.Sprintf("%.2f", float64(monitor.Info.Swap.Used)/1024/1024)) + list.SWAP.Free = append(list.SWAP.Free, fmt.Sprintf("%.2f", float64(monitor.Info.Swap.Free)/1024/1024)) + list.Net.Sent = append(list.Net.Sent, fmt.Sprintf("%.2f", float64(bytesSent2/1024/1024))) + list.Net.Recv = append(list.Net.Recv, fmt.Sprintf("%.2f", float64(bytesRecv2/1024/1024))) // 监控频率为 1 分钟,所以这里除以 60 即可得到每秒的流量 - data.Net.Tx = append(data.Net.Tx, fmt.Sprintf("%.2f", float64(bytesSent2-bytesSent)/60/1024/1024)) - data.Net.Rx = append(data.Net.Rx, fmt.Sprintf("%.2f", float64(bytesRecv2-bytesRecv)/60/1024/1024)) + list.Net.Tx = append(list.Net.Tx, fmt.Sprintf("%.2f", float64(bytesSent2-bytesSent)/60/1024/1024)) + list.Net.Rx = append(list.Net.Rx, fmt.Sprintf("%.2f", float64(bytesRecv2-bytesRecv)/60/1024/1024)) bytesSent = bytesSent2 bytesRecv = bytesRecv2 @@ -152,5 +120,5 @@ func (s *MonitorService) List(w http.ResponseWriter, r *http.Request) { bytesRecv2 = 0 } - Success(w, data) + Success(w, list) } diff --git a/pkg/apploader/apploader.go b/pkg/apploader/apploader.go index 21b6de47..84248a12 100644 --- a/pkg/apploader/apploader.go +++ b/pkg/apploader/apploader.go @@ -10,26 +10,26 @@ import ( "github.com/TheTNB/panel/pkg/types" ) -var plugins sync.Map +var apps sync.Map -func Register(plugin *types.App) { - if _, ok := plugins.Load(plugin.Slug); ok { - panic(fmt.Sprintf("plugin %s already exists", plugin.Slug)) +func Register(app *types.App) { + if _, ok := apps.Load(app.Slug); ok { + panic(fmt.Sprintf("app %s already exists", app.Slug)) } - plugins.Store(plugin.Slug, plugin) + apps.Store(app.Slug, app) } func Get(slug string) (*types.App, error) { - if plugin, ok := plugins.Load(slug); ok { - return plugin.(*types.App), nil + if app, ok := apps.Load(slug); ok { + return app.(*types.App), nil } - return nil, fmt.Errorf("plugin %s not found", slug) + return nil, fmt.Errorf("app %s not found", slug) } func All() []*types.App { var list []*types.App - plugins.Range(func(_, plugin any) bool { - if p, ok := plugin.(*types.App); ok { + apps.Range(func(_, app any) bool { + if p, ok := app.(*types.App); ok { list = append(list, p) } return true @@ -44,9 +44,9 @@ func All() []*types.App { } func Boot(r chi.Router) { - plugins.Range(func(_, plugin any) bool { - if p, ok := plugin.(*types.App); ok { - r.Route(fmt.Sprintf("/api/plugins/%s", p.Slug), p.Route) + apps.Range(func(_, app any) bool { + if p, ok := app.(*types.App); ok { + r.Route(fmt.Sprintf("/api/app/%s", p.Slug), p.Route) } return true }) diff --git a/pkg/types/app.go b/pkg/types/app.go index 28d8608a..7ce22612 100644 --- a/pkg/types/app.go +++ b/pkg/types/app.go @@ -7,3 +7,14 @@ type App struct { Slug string `json:"slug"` // 插件标识 Route func(r chi.Router) `json:"-"` // 路由 } + +// StoreApp 商店应用结构 +type StoreApp struct { + Name string `json:"name"` + Description string `json:"description"` + Slug string `json:"slug"` + Version string `json:"version"` + Installed bool `json:"installed"` + InstalledVersion string `json:"installed_version"` + Show bool `json:"show"` +} diff --git a/pkg/types/monitor.go b/pkg/types/monitor.go new file mode 100644 index 00000000..1e5492de --- /dev/null +++ b/pkg/types/monitor.go @@ -0,0 +1,34 @@ +package types + +type Load struct { + Load1 []float64 `json:"load1"` + Load5 []float64 `json:"load5"` + Load15 []float64 `json:"load15"` +} +type CPU struct { + Percent []string `json:"percent"` +} +type Mem struct { + Total string `json:"total"` + Available []string `json:"available"` + Used []string `json:"used"` +} +type SWAP struct { + Total string `json:"total"` + Used []string `json:"used"` + Free []string `json:"free"` +} +type Network struct { + Sent []string `json:"sent"` + Recv []string `json:"recv"` + Tx []string `json:"tx"` + Rx []string `json:"rx"` +} +type MonitorData struct { + Times []string `json:"times"` + Load Load `json:"load"` + CPU CPU `json:"cpu"` + Mem Mem `json:"mem"` + SWAP SWAP `json:"swap"` + Net Network `json:"net"` +}