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

refactor: 应用支持依赖注入

This commit is contained in:
耗子
2024-12-16 02:01:31 +08:00
parent 1fdc86cc0a
commit a64660d0cb
57 changed files with 663 additions and 861 deletions

View File

@@ -6,6 +6,7 @@ import (
"regexp"
"strings"
"github.com/go-chi/chi/v5"
"github.com/go-rat/chix"
"github.com/spf13/cast"
@@ -17,13 +18,20 @@ import (
"github.com/TheTNB/panel/pkg/systemctl"
)
type Service struct{}
type App struct{}
func NewService() *Service {
return &Service{}
func NewApp() *App {
return &App{}
}
func (s *Service) Info(w http.ResponseWriter, r *http.Request) {
func (s *App) Route(r chi.Router) {
r.Get("/info", s.Info)
r.Post("/port", s.UpdatePort)
r.Get("/config", s.GetConfig)
r.Post("/config", s.UpdateConfig)
}
func (s *App) Info(w http.ResponseWriter, r *http.Request) {
files, err := io.ReadDir(fmt.Sprintf("%s/server/phpmyadmin", app.Root))
if err != nil {
service.Error(w, http.StatusInternalServerError, "找不到 phpMyAdmin 目录")
@@ -58,7 +66,7 @@ func (s *Service) Info(w http.ResponseWriter, r *http.Request) {
})
}
func (s *Service) UpdatePort(w http.ResponseWriter, r *http.Request) {
func (s *App) UpdatePort(w http.ResponseWriter, r *http.Request) {
req, err := service.Bind[UpdatePort](r)
if err != nil {
service.Error(w, http.StatusUnprocessableEntity, "%v", err)
@@ -98,7 +106,7 @@ func (s *Service) UpdatePort(w http.ResponseWriter, r *http.Request) {
service.Success(w, nil)
}
func (s *Service) GetConfig(w http.ResponseWriter, r *http.Request) {
func (s *App) GetConfig(w http.ResponseWriter, r *http.Request) {
config, err := io.Read(fmt.Sprintf("%s/server/vhost/phpmyadmin.conf", app.Root))
if err != nil {
service.Error(w, http.StatusInternalServerError, "%v", err)
@@ -108,7 +116,7 @@ func (s *Service) GetConfig(w http.ResponseWriter, r *http.Request) {
service.Success(w, config)
}
func (s *Service) UpdateConfig(w http.ResponseWriter, r *http.Request) {
func (s *App) UpdateConfig(w http.ResponseWriter, r *http.Request) {
req, err := service.Bind[UpdateConfig](r)
if err != nil {
service.Error(w, http.StatusUnprocessableEntity, "%v", err)

View File

@@ -1,21 +0,0 @@
package phpmyadmin
import (
"github.com/go-chi/chi/v5"
"github.com/TheTNB/panel/pkg/apploader"
"github.com/TheTNB/panel/pkg/types"
)
func init() {
apploader.Register(&types.App{
Slug: "phpmyadmin",
Route: func(r chi.Router) {
service := NewService()
r.Get("/info", service.Info)
r.Post("/port", service.UpdatePort)
r.Get("/config", service.GetConfig)
r.Post("/config", service.UpdateConfig)
},
})
}