Skip to content

Commit

Permalink
feat: systemctl及设置
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Sep 17, 2024
1 parent 224c90f commit 8d42e84
Show file tree
Hide file tree
Showing 14 changed files with 418 additions and 39 deletions.
8 changes: 8 additions & 0 deletions internal/biz/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package biz
import (
"github.com/golang-module/carbon/v2"

"github.com/TheTNB/panel/internal/http/request"
"github.com/TheTNB/panel/pkg/tools"
)

Expand All @@ -12,3 +13,10 @@ type Monitor struct {
CreatedAt carbon.DateTime `json:"created_at"`
UpdatedAt carbon.DateTime `json:"updated_at"`
}

type MonitorRepo interface {
GetSetting() (*request.MonitorSetting, error)
UpdateSetting(setting *request.MonitorSetting) error
Clear() error
List(start, end carbon.Carbon) ([]*Monitor, error)
}
8 changes: 7 additions & 1 deletion internal/biz/setting.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package biz

import "github.com/golang-module/carbon/v2"
import (
"github.com/golang-module/carbon/v2"

"github.com/TheTNB/panel/internal/http/request"
)

type SettingKey string

Expand Down Expand Up @@ -30,4 +34,6 @@ type SettingRepo interface {
Get(key SettingKey, defaultValue ...string) (string, error)
Set(key SettingKey, value string) error
Delete(key SettingKey) error
GetPanelSetting() (*request.PanelSetting, error)
UpdatePanelSetting(setting *request.PanelSetting) error
}
67 changes: 67 additions & 0 deletions internal/data/monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package data

import (
"errors"

"github.com/golang-module/carbon/v2"
"github.com/spf13/cast"

"github.com/TheTNB/panel/internal/app"
"github.com/TheTNB/panel/internal/biz"
"github.com/TheTNB/panel/internal/http/request"
)

type monitorRepo struct {
settingRepo biz.SettingRepo
}

func NewMonitorRepo() biz.MonitorRepo {
return &monitorRepo{
settingRepo: NewSettingRepo(),
}
}

func (r monitorRepo) GetSetting() (*request.MonitorSetting, error) {
monitor, err := r.settingRepo.Get(biz.SettingKeyMonitor)
if err != nil {
return nil, err
}
monitorDays, err := r.settingRepo.Get(biz.SettingKeyMonitorDays)
if err != nil {
return nil, err
}

setting := new(request.MonitorSetting)
setting.Enabled = cast.ToBool(monitor)
setting.Days = cast.ToInt(monitorDays)

return setting, nil
}

func (r monitorRepo) UpdateSetting(setting *request.MonitorSetting) error {
if err := r.settingRepo.Set(biz.SettingKeyMonitor, cast.ToString(setting.Enabled)); err != nil {
return err
}
if err := r.settingRepo.Set(biz.SettingKeyMonitorDays, cast.ToString(setting.Days)); err != nil {
return err
}

return nil
}

func (r monitorRepo) Clear() error {
return app.Orm.Delete(&biz.Monitor{}).Error
}

func (r monitorRepo) List(start, end carbon.Carbon) ([]*biz.Monitor, error) {
var monitors []*biz.Monitor
if err := app.Orm.Where("created_at BETWEEN ? AND ?", start, end).Find(&monitors).Error; err != nil {
return nil, err
}

if len(monitors) == 0 {
return nil, errors.New("没有找到数据")
}

return monitors, nil
}
24 changes: 24 additions & 0 deletions internal/data/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package data
import (
"github.com/TheTNB/panel/internal/app"
"github.com/TheTNB/panel/internal/biz"
"github.com/TheTNB/panel/internal/http/request"
)

type settingRepo struct{}
Expand Down Expand Up @@ -42,3 +43,26 @@ func (r *settingRepo) Delete(key biz.SettingKey) error {

return nil
}

func (r *settingRepo) GetPanelSetting() (*request.PanelSetting, error) {
setting := new(biz.Setting)
if err := app.Orm.Where("key = ?", biz.SettingKeyName).First(setting).Error; err != nil {
return nil, err
}

// TODO fix

return &request.PanelSetting{
Name: setting.Value,
}, nil
}

func (r *settingRepo) UpdatePanelSetting(setting *request.PanelSetting) error {
if err := r.Set(biz.SettingKeyName, setting.Name); err != nil {
return err
}

// TODO fix

return nil
}
11 changes: 11 additions & 0 deletions internal/http/request/monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package request

type MonitorSetting struct {
Enabled bool `json:"enabled"`
Days int `json:"days"`
}

type MonitorList struct {
Start int64 `json:"start"`
End int64 `json:"end"`
}
16 changes: 16 additions & 0 deletions internal/http/request/setting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package request

type PanelSetting struct {
Name string `json:"name"`
Language string `json:"language"`
Entrance string `json:"entrance"`
WebsitePath string `json:"website_path"`
BackupPath string `json:"backup_path"`
Username string `json:"username"`
Password string `json:"password"`
Email string `json:"email"`
Port string `json:"port"`
HTTPS bool `json:"https"`
Cert string `json:"cert"`
Key string `json:"key"`
}
5 changes: 5 additions & 0 deletions internal/http/request/systemctl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package request

type SystemctlService struct {
Service string `json:"service"`
}
2 changes: 0 additions & 2 deletions internal/route/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,6 @@ func Http(r chi.Router) {
setting := service.NewSettingService()
r.Get("/", setting.Get)
r.Post("/", setting.Update)
r.Get("/https", setting.GetHttps)
r.Post("/https", setting.UpdateHttps)
})

r.Route("/systemctl", func(r chi.Router) {
Expand Down
8 changes: 4 additions & 4 deletions internal/service/container_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s *ContainerImageService) List(w http.ResponseWriter, r *http.Request) {
func (s *ContainerImageService) Exist(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.ContainerImageID](r)
if err != nil {
Error(w, http.StatusBadRequest, err.Error())
Error(w, http.StatusUnprocessableEntity, err.Error())
return
}

Expand All @@ -69,7 +69,7 @@ func (s *ContainerImageService) Exist(w http.ResponseWriter, r *http.Request) {
func (s *ContainerImageService) Pull(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.ContainerImagePull](r)
if err != nil {
Error(w, http.StatusBadRequest, err.Error())
Error(w, http.StatusUnprocessableEntity, err.Error())
return
}

Expand All @@ -84,7 +84,7 @@ func (s *ContainerImageService) Pull(w http.ResponseWriter, r *http.Request) {
func (s *ContainerImageService) Remove(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.ContainerImageID](r)
if err != nil {
Error(w, http.StatusBadRequest, err.Error())
Error(w, http.StatusUnprocessableEntity, err.Error())
return
}

Expand All @@ -99,7 +99,7 @@ func (s *ContainerImageService) Remove(w http.ResponseWriter, r *http.Request) {
func (s *ContainerImageService) Inspect(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.ContainerImageID](r)
if err != nil {
Error(w, http.StatusBadRequest, err.Error())
Error(w, http.StatusUnprocessableEntity, err.Error())
return
}

Expand Down
12 changes: 6 additions & 6 deletions internal/service/container_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (s *ContainerNetworkService) List(w http.ResponseWriter, r *http.Request) {
func (s *ContainerNetworkService) Create(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.ContainerNetworkCreate](r)
if err != nil {
Error(w, http.StatusBadRequest, err.Error())
Error(w, http.StatusUnprocessableEntity, err.Error())
return
}

Expand All @@ -86,7 +86,7 @@ func (s *ContainerNetworkService) Create(w http.ResponseWriter, r *http.Request)
func (s *ContainerNetworkService) Remove(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.ContainerNetworkID](r)
if err != nil {
Error(w, http.StatusBadRequest, err.Error())
Error(w, http.StatusUnprocessableEntity, err.Error())
return
}

Expand All @@ -101,7 +101,7 @@ func (s *ContainerNetworkService) Remove(w http.ResponseWriter, r *http.Request)
func (s *ContainerNetworkService) Exist(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.ContainerNetworkID](r)
if err != nil {
Error(w, http.StatusBadRequest, err.Error())
Error(w, http.StatusUnprocessableEntity, err.Error())
return
}

Expand All @@ -117,7 +117,7 @@ func (s *ContainerNetworkService) Exist(w http.ResponseWriter, r *http.Request)
func (s *ContainerNetworkService) Inspect(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.ContainerNetworkID](r)
if err != nil {
Error(w, http.StatusBadRequest, err.Error())
Error(w, http.StatusUnprocessableEntity, err.Error())
return
}

Expand All @@ -133,7 +133,7 @@ func (s *ContainerNetworkService) Inspect(w http.ResponseWriter, r *http.Request
func (s *ContainerNetworkService) Connect(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.ContainerNetworkConnect](r)
if err != nil {
Error(w, http.StatusBadRequest, err.Error())
Error(w, http.StatusUnprocessableEntity, err.Error())
return
}

Expand All @@ -148,7 +148,7 @@ func (s *ContainerNetworkService) Connect(w http.ResponseWriter, r *http.Request
func (s *ContainerNetworkService) Disconnect(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.ContainerNetworkConnect](r)
if err != nil {
Error(w, http.StatusBadRequest, err.Error())
Error(w, http.StatusUnprocessableEntity, err.Error())
return
}

Expand Down
8 changes: 4 additions & 4 deletions internal/service/container_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *ContainerVolumeService) List(w http.ResponseWriter, r *http.Request) {
func (s *ContainerVolumeService) Create(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.ContainerVolumeCreate](r)
if err != nil {
Error(w, http.StatusBadRequest, err.Error())
Error(w, http.StatusUnprocessableEntity, err.Error())
return
}

Expand All @@ -79,7 +79,7 @@ func (s *ContainerVolumeService) Create(w http.ResponseWriter, r *http.Request)
func (s *ContainerVolumeService) Exist(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.ContainerVolumeID](r)
if err != nil {
Error(w, http.StatusBadRequest, err.Error())
Error(w, http.StatusUnprocessableEntity, err.Error())
return
}

Expand All @@ -95,7 +95,7 @@ func (s *ContainerVolumeService) Exist(w http.ResponseWriter, r *http.Request) {
func (s *ContainerVolumeService) Remove(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.ContainerVolumeID](r)
if err != nil {
Error(w, http.StatusBadRequest, err.Error())
Error(w, http.StatusUnprocessableEntity, err.Error())
return
}

Expand All @@ -110,7 +110,7 @@ func (s *ContainerVolumeService) Remove(w http.ResponseWriter, r *http.Request)
func (s *ContainerVolumeService) Inspect(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.ContainerVolumeID](r)
if err != nil {
Error(w, http.StatusBadRequest, err.Error())
Error(w, http.StatusUnprocessableEntity, err.Error())
return
}

Expand Down
Loading

0 comments on commit 8d42e84

Please sign in to comment.