style: 修改app的代码结构

This commit is contained in:
zhengkunwang223 2022-12-14 15:08:21 +08:00 committed by zhengkunwang223
parent a353adc4b9
commit 7171d015a4
9 changed files with 158 additions and 153 deletions

View File

@ -2,24 +2,22 @@ package v1
import (
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/gin-gonic/gin"
)
func (b *BaseApi) SearchApp(c *gin.Context) {
var req dto.AppRequest
var req request.AppSearch
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
list, err := appService.PageApp(req)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, list)
}
@ -32,7 +30,6 @@ func (b *BaseApi) SyncApp(c *gin.Context) {
}
func (b *BaseApi) GetApp(c *gin.Context) {
id, err := helper.GetParamID(c)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
@ -46,13 +43,11 @@ func (b *BaseApi) GetApp(c *gin.Context) {
helper.SuccessWithData(c, appDTO)
}
func (b *BaseApi) GetAppDetail(c *gin.Context) {
appId, err := helper.GetIntParamByKey(c, "appId")
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInternalServer, nil)
return
}
version := c.Param("version")
appDetailDTO, err := appService.GetAppDetail(appId, version)
if err != nil {
@ -63,7 +58,7 @@ func (b *BaseApi) GetAppDetail(c *gin.Context) {
}
func (b *BaseApi) InstallApp(c *gin.Context) {
var req dto.AppInstallRequest
var req request.AppInstallCreate
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
@ -73,6 +68,5 @@ func (b *BaseApi) InstallApp(c *gin.Context) {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, install)
}

View File

@ -2,6 +2,7 @@ package v1
import (
"errors"
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
"reflect"
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
@ -12,7 +13,7 @@ import (
)
func (b *BaseApi) SearchAppInstalled(c *gin.Context) {
var req dto.AppInstalledRequest
var req request.AppInstalledSearch
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
@ -68,7 +69,6 @@ func (b *BaseApi) LoadPort(c *gin.Context) {
}
func (b *BaseApi) DeleteCheck(c *gin.Context) {
appInstallId, err := helper.GetIntParamByKey(c, "appInstallId")
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInternalServer, nil)
@ -92,7 +92,7 @@ func (b *BaseApi) SyncInstalled(c *gin.Context) {
}
func (b *BaseApi) SearchInstalledBackup(c *gin.Context) {
var req dto.AppBackupRequest
var req request.AppBackupSearch
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
@ -110,7 +110,7 @@ func (b *BaseApi) SearchInstalledBackup(c *gin.Context) {
}
func (b *BaseApi) OperateInstalled(c *gin.Context) {
var req dto.AppInstallOperate
var req request.AppInstalledOperate
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
@ -124,7 +124,7 @@ func (b *BaseApi) OperateInstalled(c *gin.Context) {
}
func (b *BaseApi) DeleteAppBackup(c *gin.Context) {
var req dto.AppBackupDeleteRequest
var req request.AppBackupDelete
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
@ -137,7 +137,6 @@ func (b *BaseApi) DeleteAppBackup(c *gin.Context) {
}
func (b *BaseApi) GetServices(c *gin.Context) {
key := c.Param("key")
services, err := appInstallService.GetServices(key)
if err != nil {
@ -149,7 +148,6 @@ func (b *BaseApi) GetServices(c *gin.Context) {
}
func (b *BaseApi) GetUpdateVersions(c *gin.Context) {
appInstallId, err := helper.GetIntParamByKey(c, "appInstallId")
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInternalServer, nil)
@ -166,7 +164,7 @@ func (b *BaseApi) GetUpdateVersions(c *gin.Context) {
}
func (b *BaseApi) ChangeAppPort(c *gin.Context) {
var req dto.PortUpdate
var req request.PortUpdate
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return

View File

@ -2,113 +2,8 @@ package dto
import (
"encoding/json"
"time"
"github.com/1Panel-dev/1Panel/backend/app/model"
)
type AppRes struct {
Version string `json:"version"`
CanUpdate bool `json:"canUpdate"`
Items []*AppDTO `json:"items"`
Tags []model.Tag `json:"tags"`
Total int64 `json:"total"`
}
type AppDTO struct {
model.App
Versions []string `json:"versions"`
Tags []model.Tag `json:"tags"`
}
type AppDetailDTO struct {
model.AppDetail
Enable bool `json:"enable"`
Params interface{} `json:"params"`
}
type AppRequest struct {
PageInfo
Name string `json:"name"`
Tags []string `json:"tags"`
Type string `json:"type"`
}
type AppInstallRequest struct {
AppDetailId uint `json:"appDetailId" validate:"required"`
Params map[string]interface{} `json:"params"`
Name string `json:"name" validate:"required"`
Services map[string]string `json:"services"`
}
type CheckInstalled struct {
IsExist bool `json:"isExist"`
Name string `json:"name"`
App string `json:"app"`
Version string `json:"version"`
Status string `json:"status"`
CreatedAt time.Time `json:"createdAt"`
LastBackupAt string `json:"lastBackupAt"`
AppInstallID uint `json:"appInstallId"`
ContainerName string `json:"containerName"`
}
type AppInstalled struct {
model.AppInstall
Total int `json:"total"`
Ready int `json:"ready"`
AppName string `json:"appName"`
Icon string `json:"icon"`
CanUpdate bool `json:"canUpdate"`
}
type AppInstalledRequest struct {
PageInfo
Type string `json:"type"`
Name string `json:"name"`
}
type AppBackupRequest struct {
PageInfo
AppInstallID uint `json:"appInstallID"`
}
type AppBackupDeleteRequest struct {
Ids []uint `json:"ids"`
}
type AppOperate string
var (
Up AppOperate = "up"
Down AppOperate = "down"
Restart AppOperate = "restart"
Delete AppOperate = "delete"
Sync AppOperate = "sync"
Backup AppOperate = "backup"
Restore AppOperate = "restore"
Update AppOperate = "update"
)
type AppInstallOperate struct {
InstallId uint `json:"installId" validate:"required"`
BackupId uint `json:"backupId"`
DetailId uint `json:"detailId"`
Operate AppOperate `json:"operate" validate:"required"`
}
type PortUpdate struct {
Key string `json:"key"`
Name string `json:"name"`
Port int64 `json:"port"`
}
type AppService struct {
Label string `json:"label"`
Value string `json:"value"`
Config interface{} `json:"config"`
}
type AppDatabase struct {
ServiceName string `json:"PANEL_DB_HOST"`
DbName string `json:"PANEL_DB_NAME"`

View File

@ -0,0 +1,48 @@
package request
import (
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/constant"
)
type AppSearch struct {
dto.PageInfo
Name string `json:"name"`
Tags []string `json:"tags"`
Type string `json:"type"`
}
type AppInstallCreate struct {
AppDetailId uint `json:"appDetailId" validate:"required"`
Params map[string]interface{} `json:"params"`
Name string `json:"name" validate:"required"`
Services map[string]string `json:"services"`
}
type AppInstalledSearch struct {
dto.PageInfo
Type string `json:"type"`
Name string `json:"name"`
}
type AppBackupSearch struct {
dto.PageInfo
AppInstallID uint `json:"appInstallID"`
}
type AppBackupDelete struct {
Ids []uint `json:"ids"`
}
type AppInstalledOperate struct {
InstallId uint `json:"installId" validate:"required"`
BackupId uint `json:"backupId"`
DetailId uint `json:"detailId"`
Operate constant.AppOperate `json:"operate" validate:"required"`
}
type PortUpdate struct {
Key string `json:"key"`
Name string `json:"name"`
Port int64 `json:"port"`
}

View File

@ -0,0 +1,53 @@
package response
import (
"github.com/1Panel-dev/1Panel/backend/app/model"
"time"
)
type AppRes struct {
Version string `json:"version"`
CanUpdate bool `json:"canUpdate"`
Items []*AppDTO `json:"items"`
Tags []model.Tag `json:"tags"`
Total int64 `json:"total"`
}
type AppDTO struct {
model.App
Versions []string `json:"versions"`
Tags []model.Tag `json:"tags"`
}
type AppInstalledCheck struct {
IsExist bool `json:"isExist"`
Name string `json:"name"`
App string `json:"app"`
Version string `json:"version"`
Status string `json:"status"`
CreatedAt time.Time `json:"createdAt"`
LastBackupAt string `json:"lastBackupAt"`
AppInstallID uint `json:"appInstallId"`
ContainerName string `json:"containerName"`
}
type AppDetailDTO struct {
model.AppDetail
Enable bool `json:"enable"`
Params interface{} `json:"params"`
}
type AppInstalledDTO struct {
model.AppInstall
Total int `json:"total"`
Ready int `json:"ready"`
AppName string `json:"appName"`
Icon string `json:"icon"`
CanUpdate bool `json:"canUpdate"`
}
type AppService struct {
Label string `json:"label"`
Value string `json:"value"`
Config interface{} `json:"config"`
}

View File

@ -9,6 +9,8 @@ import (
"strings"
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/app/repo"
"github.com/1Panel-dev/1Panel/backend/constant"
@ -22,7 +24,7 @@ import (
type AppService struct {
}
func (a AppService) PageApp(req dto.AppRequest) (interface{}, error) {
func (a AppService) PageApp(req request.AppSearch) (interface{}, error) {
var opts []repo.DBOption
opts = append(opts, commonRepo.WithOrderBy("name"))
@ -52,14 +54,14 @@ func (a AppService) PageApp(req dto.AppRequest) (interface{}, error) {
opts = append(opts, commonRepo.WithIdsIn(appIds))
}
var res dto.AppRes
var res response.AppRes
total, apps, err := appRepo.Page(req.Page, req.PageSize, opts...)
if err != nil {
return nil, err
}
var appDTOs []*dto.AppDTO
var appDTOs []*response.AppDTO
for _, a := range apps {
appDTO := &dto.AppDTO{
appDTO := &response.AppDTO{
App: a,
}
appDTOs = append(appDTOs, appDTO)
@ -88,8 +90,8 @@ func (a AppService) PageApp(req dto.AppRequest) (interface{}, error) {
return res, nil
}
func (a AppService) GetApp(id uint) (dto.AppDTO, error) {
var appDTO dto.AppDTO
func (a AppService) GetApp(id uint) (response.AppDTO, error) {
var appDTO response.AppDTO
app, err := appRepo.GetFirst(commonRepo.WithByID(id))
if err != nil {
return appDTO, err
@ -109,10 +111,9 @@ func (a AppService) GetApp(id uint) (dto.AppDTO, error) {
return appDTO, nil
}
func (a AppService) GetAppDetail(appId uint, version string) (dto.AppDetailDTO, error) {
func (a AppService) GetAppDetail(appId uint, version string) (response.AppDetailDTO, error) {
var (
appDetailDTO dto.AppDetailDTO
appDetailDTO response.AppDetailDTO
opts []repo.DBOption
)

View File

@ -4,6 +4,8 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
"io/ioutil"
"os"
"path"
@ -27,7 +29,7 @@ import (
type AppInstallService struct {
}
func (a AppInstallService) Page(req dto.AppInstalledRequest) (int64, []dto.AppInstalled, error) {
func (a AppInstallService) Page(req request.AppInstalledSearch) (int64, []response.AppInstalledDTO, error) {
var opts []repo.DBOption
if req.Name != "" {
@ -47,9 +49,9 @@ func (a AppInstallService) Page(req dto.AppInstalledRequest) (int64, []dto.AppIn
return total, installDTOs, nil
}
func (a AppInstallService) CheckExist(key string) (*dto.CheckInstalled, error) {
func (a AppInstallService) CheckExist(key string) (*response.AppInstalledCheck, error) {
res := &dto.CheckInstalled{
res := &response.AppInstalledCheck{
IsExist: false,
}
app, err := appRepo.GetFirst(appRepo.WithKey(key))
@ -87,7 +89,7 @@ func (a AppInstallService) LoadPort(key string) (int64, error) {
return app.Port, nil
}
func (a AppInstallService) Search(req dto.AppInstalledRequest) ([]dto.AppInstalled, error) {
func (a AppInstallService) Search(req request.AppInstalledSearch) ([]response.AppInstalledDTO, error) {
var installs []model.AppInstall
var err error
if req.Type != "" {
@ -113,7 +115,7 @@ func (a AppInstallService) Search(req dto.AppInstalledRequest) ([]dto.AppInstall
return handleInstalled(installs)
}
func (a AppInstallService) Operate(req dto.AppInstallOperate) error {
func (a AppInstallService) Operate(req request.AppInstalledOperate) error {
install, err := appInstallRepo.GetFirst(commonRepo.WithByID(req.InstallId))
if err != nil {
return err
@ -122,25 +124,25 @@ func (a AppInstallService) Operate(req dto.AppInstallOperate) error {
dockerComposePath := install.GetComposePath()
switch req.Operate {
case dto.Up:
case constant.Up:
out, err := compose.Up(dockerComposePath)
if err != nil {
return handleErr(install, err, out)
}
install.Status = constant.Running
case dto.Down:
case constant.Down:
out, err := compose.Stop(dockerComposePath)
if err != nil {
return handleErr(install, err, out)
}
install.Status = constant.Stopped
case dto.Restart:
case constant.Restart:
out, err := compose.Restart(dockerComposePath)
if err != nil {
return handleErr(install, err, out)
}
install.Status = constant.Running
case dto.Delete:
case constant.Delete:
tx, ctx := getTxAndContext()
if err := deleteAppInstall(ctx, install); err != nil {
tx.Rollback()
@ -148,9 +150,9 @@ func (a AppInstallService) Operate(req dto.AppInstallOperate) error {
}
tx.Commit()
return nil
case dto.Sync:
case constant.Sync:
return syncById(install.ID)
case dto.Backup:
case constant.Backup:
tx, ctx := getTxAndContext()
if err := backupInstall(ctx, install); err != nil {
tx.Rollback()
@ -158,9 +160,9 @@ func (a AppInstallService) Operate(req dto.AppInstallOperate) error {
}
tx.Commit()
return nil
case dto.Restore:
case constant.Restore:
return restoreInstall(install, req.BackupId)
case dto.Update:
case constant.Update:
return updateInstall(install.ID, req.DetailId)
default:
return errors.New("operate not support")
@ -184,11 +186,11 @@ func (a AppInstallService) SyncAll() error {
return nil
}
func (a AppInstallService) PageInstallBackups(req dto.AppBackupRequest) (int64, []model.AppInstallBackup, error) {
func (a AppInstallService) PageInstallBackups(req request.AppBackupSearch) (int64, []model.AppInstallBackup, error) {
return appInstallBackupRepo.Page(req.Page, req.PageSize, appInstallBackupRepo.WithAppInstallID(req.AppInstallID))
}
func (a AppInstallService) DeleteBackup(req dto.AppBackupDeleteRequest) error {
func (a AppInstallService) DeleteBackup(req request.AppBackupDelete) error {
backups, err := appInstallBackupRepo.GetBy(commonRepo.WithIdsIn(req.Ids))
if err != nil {
@ -213,7 +215,7 @@ func (a AppInstallService) DeleteBackup(req dto.AppBackupDeleteRequest) error {
return nil
}
func (a AppInstallService) GetServices(key string) ([]dto.AppService, error) {
func (a AppInstallService) GetServices(key string) ([]response.AppService, error) {
app, err := appRepo.GetFirst(appRepo.WithKey(key))
if err != nil {
return nil, err
@ -222,13 +224,13 @@ func (a AppInstallService) GetServices(key string) ([]dto.AppService, error) {
if err != nil {
return nil, err
}
var res []dto.AppService
var res []response.AppService
for _, install := range installs {
paramMap := make(map[string]string)
if install.Param != "" {
_ = json.Unmarshal([]byte(install.Param), &paramMap)
}
res = append(res, dto.AppService{
res = append(res, response.AppService{
Label: install.Name,
Value: install.ServiceName,
Config: paramMap,
@ -262,7 +264,7 @@ func (a AppInstallService) GetUpdateVersions(installId uint) ([]dto.AppVersion,
return versions, nil
}
func (a AppInstallService) ChangeAppPort(req dto.PortUpdate) error {
func (a AppInstallService) ChangeAppPort(req request.PortUpdate) error {
return updateInstallInfoInDB(req.Key, "port", true, strconv.FormatInt(req.Port, 10))
}

View File

@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
"github.com/1Panel-dev/1Panel/backend/buserr"
"io/ioutil"
"math"
@ -589,12 +590,12 @@ func getAppFromOss() error {
return nil
}
func handleInstalled(installed []model.AppInstall) ([]dto.AppInstalled, error) {
var res []dto.AppInstalled
func handleInstalled(installed []model.AppInstall) ([]response.AppInstalledDTO, error) {
var res []response.AppInstalledDTO
for _, installed := range installed {
installDTO := dto.AppInstalled{
installDTO := response.AppInstalledDTO{
AppInstall: installed,
}

View File

@ -16,3 +16,16 @@ const (
AppMysql = "mysql"
AppRedis = "redis"
)
type AppOperate string
var (
Up AppOperate = "up"
Down AppOperate = "down"
Restart AppOperate = "restart"
Delete AppOperate = "delete"
Sync AppOperate = "sync"
Backup AppOperate = "backup"
Restore AppOperate = "restore"
Update AppOperate = "update"
)