mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 11:27:17 +08:00
170 lines
3.9 KiB
Go
170 lines
3.9 KiB
Go
package service
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/go-rat/chix"
|
|
|
|
"github.com/TheTNB/panel/internal/biz"
|
|
"github.com/TheTNB/panel/internal/data"
|
|
"github.com/TheTNB/panel/internal/http/request"
|
|
"github.com/TheTNB/panel/pkg/str"
|
|
)
|
|
|
|
type AppService struct {
|
|
appRepo biz.AppRepo
|
|
}
|
|
|
|
func NewAppService() *AppService {
|
|
return &AppService{
|
|
appRepo: data.NewAppRepo(),
|
|
}
|
|
}
|
|
|
|
func (s *AppService) List(w http.ResponseWriter, r *http.Request) {
|
|
plugins := s.appRepo.All()
|
|
installedPlugins, err := s.appRepo.Installed()
|
|
if err != nil {
|
|
Error(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
installedPluginsMap := make(map[string]*biz.App)
|
|
|
|
for _, p := range installedPlugins {
|
|
installedPluginsMap[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 {
|
|
installed, installedVersion, currentVersion, show := false, "", "", false
|
|
if str.FirstElement(item.Versions) != nil {
|
|
currentVersion = str.FirstElement(item.Versions).Version
|
|
}
|
|
if _, ok := installedPluginsMap[item.Slug]; ok {
|
|
installed = true
|
|
installedVersion = installedPluginsMap[item.Slug].Version
|
|
show = installedPluginsMap[item.Slug].Show
|
|
}
|
|
pluginArr = append(pluginArr, plugin{
|
|
Name: item.Name,
|
|
Description: item.Description,
|
|
Slug: item.Slug,
|
|
Version: currentVersion,
|
|
Installed: installed,
|
|
InstalledVersion: installedVersion,
|
|
Show: show,
|
|
})
|
|
}
|
|
|
|
paged, total := Paginate(r, pluginArr)
|
|
|
|
Success(w, chix.M{
|
|
"total": total,
|
|
"items": paged,
|
|
})
|
|
}
|
|
|
|
func (s *AppService) Install(w http.ResponseWriter, r *http.Request) {
|
|
req, err := Bind[request.PluginSlug](r)
|
|
if err != nil {
|
|
Error(w, http.StatusUnprocessableEntity, err.Error())
|
|
return
|
|
}
|
|
|
|
if err = s.appRepo.Install(req.Slug); err != nil {
|
|
Error(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
Success(w, nil)
|
|
}
|
|
|
|
func (s *AppService) Uninstall(w http.ResponseWriter, r *http.Request) {
|
|
req, err := Bind[request.PluginSlug](r)
|
|
if err != nil {
|
|
Error(w, http.StatusUnprocessableEntity, err.Error())
|
|
return
|
|
}
|
|
|
|
if err = s.appRepo.Uninstall(req.Slug); err != nil {
|
|
Error(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
Success(w, nil)
|
|
}
|
|
|
|
func (s *AppService) Update(w http.ResponseWriter, r *http.Request) {
|
|
req, err := Bind[request.PluginSlug](r)
|
|
if err != nil {
|
|
Error(w, http.StatusUnprocessableEntity, err.Error())
|
|
return
|
|
}
|
|
|
|
if err = s.appRepo.Update(req.Slug); err != nil {
|
|
Error(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
Success(w, nil)
|
|
}
|
|
|
|
func (s *AppService) UpdateShow(w http.ResponseWriter, r *http.Request) {
|
|
req, err := Bind[request.PluginUpdateShow](r)
|
|
if err != nil {
|
|
Error(w, http.StatusUnprocessableEntity, err.Error())
|
|
return
|
|
}
|
|
|
|
if err = s.appRepo.UpdateShow(req.Slug, req.Show); err != nil {
|
|
Error(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
Success(w, nil)
|
|
}
|
|
|
|
func (s *AppService) IsInstalled(w http.ResponseWriter, r *http.Request) {
|
|
req, err := Bind[request.PluginSlug](r)
|
|
if err != nil {
|
|
Error(w, http.StatusUnprocessableEntity, err.Error())
|
|
return
|
|
}
|
|
|
|
plugin, err := s.appRepo.Get(req.Slug)
|
|
if err != nil {
|
|
Error(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
installed, err := s.appRepo.IsInstalled(req.Slug)
|
|
if err != nil {
|
|
Error(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
Success(w, chix.M{
|
|
"name": plugin.Name,
|
|
"installed": installed,
|
|
})
|
|
}
|
|
|
|
func (s *AppService) UpdateCache(w http.ResponseWriter, r *http.Request) {
|
|
if err := s.appRepo.UpdateCache(); err != nil {
|
|
Error(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
Success(w, nil)
|
|
}
|