2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 11:27:17 +08:00

fix: 修正环境检查

This commit is contained in:
2026-01-09 17:12:57 +08:00
parent 1d41b2c61c
commit f48c975de3
14 changed files with 74 additions and 53 deletions

View File

@@ -27,7 +27,7 @@ type AppRepo interface {
GetInstalled(slug string) (*App, error)
GetInstalledAll(query string, cond ...string) ([]*App, error)
GetHomeShow() ([]map[string]string, error)
IsInstalled(query string, cond ...string) (bool, error)
IsInstalled(query string, cond ...any) (bool, error)
Install(channel, slug string) error
UnInstall(slug string) error
Update(slug string) error

View File

@@ -9,6 +9,7 @@ type EnvironmentRepo interface {
Types() []types.LV
All(typ ...string) api.Environments
IsInstalled(typ, slug string) bool
InstalledSlugs(typ string) []string
InstalledVersion(typ, slug string) string
HasUpdate(typ, slug string) bool
Install(typ, slug string) error

View File

@@ -162,14 +162,14 @@ func (r *appRepo) GetHomeShow() ([]map[string]string, error) {
return filtered, nil
}
func (r *appRepo) IsInstalled(query string, cond ...string) (bool, error) {
func (r *appRepo) IsInstalled(query string, cond ...any) (bool, error) {
var count int64
if len(cond) == 0 {
if err := r.db.Model(&biz.App{}).Where("slug = ?", query).Count(&count).Error; err != nil {
return false, err
}
} else {
if err := r.db.Model(&biz.App{}).Where(query, cond).Count(&count).Error; err != nil {
if err := r.db.Model(&biz.App{}).Where(query, cond...).Count(&count).Error; err != nil {
return false, err
}
}

View File

@@ -82,6 +82,20 @@ func (r *environmentRepo) IsInstalled(typ, slug string) bool {
return err == nil
}
func (r *environmentRepo) InstalledSlugs(typ string) []string {
var slugs []string
all := r.All()
for _, env := range all {
if env.Type != typ {
continue
}
if r.IsInstalled(typ, env.Slug) {
slugs = append(slugs, env.Slug)
}
}
return slugs
}
func (r *environmentRepo) InstalledVersion(typ, slug string) string {
if !r.IsInstalled(typ, slug) {
return ""

View File

@@ -167,7 +167,7 @@ func (route *Http) Register(r *chi.Mux) {
r.Post("/current", route.home.Current)
r.Get("/system_info", route.home.SystemInfo)
r.Get("/count_info", route.home.CountInfo)
r.Get("/installed_db_and_php", route.home.InstalledDbAndPhp)
r.Get("/installed_environment", route.home.InstalledEnvironment)
r.Get("/check_update", route.home.CheckUpdate)
r.Get("/update_info", route.home.UpdateInfo)
r.Post("/update", route.home.Update)

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"net"
"net/http"
"regexp"
"strings"
"github.com/hashicorp/go-version"
@@ -27,28 +26,30 @@ import (
)
type HomeService struct {
t *gotext.Locale
api *api.API
conf *config.Config
taskRepo biz.TaskRepo
websiteRepo biz.WebsiteRepo
appRepo biz.AppRepo
settingRepo biz.SettingRepo
cronRepo biz.CronRepo
backupRepo biz.BackupRepo
t *gotext.Locale
api *api.API
conf *config.Config
taskRepo biz.TaskRepo
websiteRepo biz.WebsiteRepo
appRepo biz.AppRepo
environmentRepo biz.EnvironmentRepo
settingRepo biz.SettingRepo
cronRepo biz.CronRepo
backupRepo biz.BackupRepo
}
func NewHomeService(t *gotext.Locale, conf *config.Config, task biz.TaskRepo, website biz.WebsiteRepo, appRepo biz.AppRepo, setting biz.SettingRepo, cron biz.CronRepo, backupRepo biz.BackupRepo) *HomeService {
func NewHomeService(t *gotext.Locale, conf *config.Config, task biz.TaskRepo, website biz.WebsiteRepo, appRepo biz.AppRepo, environment biz.EnvironmentRepo, setting biz.SettingRepo, cron biz.CronRepo, backupRepo biz.BackupRepo) *HomeService {
return &HomeService{
t: t,
api: api.NewAPI(app.Version, app.Locale),
conf: conf,
taskRepo: task,
websiteRepo: website,
appRepo: appRepo,
settingRepo: setting,
cronRepo: cron,
backupRepo: backupRepo,
t: t,
api: api.NewAPI(app.Version, app.Locale),
conf: conf,
taskRepo: task,
websiteRepo: website,
appRepo: appRepo,
environmentRepo: environment,
settingRepo: setting,
cronRepo: cron,
backupRepo: backupRepo,
}
}
@@ -190,24 +191,17 @@ func (s *HomeService) CountInfo(w http.ResponseWriter, r *http.Request) {
})
}
func (s *HomeService) InstalledDbAndPhp(w http.ResponseWriter, r *http.Request) {
mysqlInstalled, _ := s.appRepo.IsInstalled("slug = ?", "mysql")
func (s *HomeService) InstalledEnvironment(w http.ResponseWriter, r *http.Request) {
mysqlInstalled, _ := s.appRepo.IsInstalled("slug IN ?", []string{"mysql", "mariadb", "percona"})
postgresqlInstalled, _ := s.appRepo.IsInstalled("slug = ?", "postgresql")
php, _ := s.appRepo.GetInstalledAll("slug like ?", "php%")
var phpData []types.LVInt
var dbData []types.LV
phpData = append(phpData, types.LVInt{Value: 0, Label: s.t.Get("Not used")})
dbData = append(dbData, types.LV{Value: "0", Label: s.t.Get("Not used")})
for _, p := range php {
// 过滤 phpmyadmin
match := regexp.MustCompile(`php(\d+)`).FindStringSubmatch(p.Slug)
if len(match) == 0 {
continue
}
item, _ := s.appRepo.Get(p.Slug)
phpData = append(phpData, types.LVInt{Value: cast.ToInt(strings.ReplaceAll(p.Slug, "php", "")), Label: item.Name})
for _, slug := range s.environmentRepo.InstalledSlugs("php") {
ver := s.environmentRepo.InstalledVersion("php", slug)
phpData = append(phpData, types.LVInt{Value: cast.ToInt(slug), Label: fmt.Sprintf("PHP %s", ver)})
}
if mysqlInstalled {
@@ -217,9 +211,11 @@ func (s *HomeService) InstalledDbAndPhp(w http.ResponseWriter, r *http.Request)
dbData = append(dbData, types.LV{Value: "postgresql", Label: "PostgreSQL"})
}
webserver, _ := s.settingRepo.Get(biz.SettingKeyWebserver)
Success(w, chix.M{
"php": phpData,
"db": dbData,
"webserver": webserver,
"php": phpData,
"db": dbData,
})
}