2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-05 02:07:18 +08:00
Files
panel/internal/service/app.go
耗子 dded5e26aa feat: 支持远程插件 (#180)
* feat: api基本完成

* feat: 阶段性提交

* feat: 初步支持远程插件

* feat: 支持远程插件

* fix: tests
2024-09-19 01:17:29 +08:00

174 lines
4.1 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"`
Requires []string `json:"requires"`
Excludes []string `json:"excludes"`
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,
Requires: item.Requires,
Excludes: item.Excludes,
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)
}