style: 修改websiteSSL代码结构

This commit is contained in:
zhengkunwang223 2022-12-14 15:26:51 +08:00 committed by zhengkunwang223
parent 7171d015a4
commit e99badb738
14 changed files with 123 additions and 137 deletions

View File

@ -3,6 +3,7 @@ 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"
)
@ -25,7 +26,7 @@ func (b *BaseApi) PageWebsiteAcmeAccount(c *gin.Context) {
}
func (b *BaseApi) CreateWebsiteAcmeAccount(c *gin.Context) {
var req dto.WebsiteAcmeAccountCreate
var req request.WebsiteAcmeAccountCreate
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
@ -39,7 +40,6 @@ func (b *BaseApi) CreateWebsiteAcmeAccount(c *gin.Context) {
}
func (b *BaseApi) DeleteWebsiteAcmeAccount(c *gin.Context) {
id, err := helper.GetParamID(c)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)

View File

@ -3,6 +3,7 @@ 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"
)
@ -25,7 +26,7 @@ func (b *BaseApi) PageWebsiteDnsAccount(c *gin.Context) {
}
func (b *BaseApi) CreateWebsiteDnsAccount(c *gin.Context) {
var req dto.WebsiteDnsAccountCreate
var req request.WebsiteDnsAccountCreate
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
@ -38,7 +39,7 @@ func (b *BaseApi) CreateWebsiteDnsAccount(c *gin.Context) {
}
func (b *BaseApi) UpdateWebsiteDnsAccount(c *gin.Context) {
var req dto.WebsiteDnsAccountUpdate
var req request.WebsiteDnsAccountUpdate
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
@ -51,7 +52,6 @@ func (b *BaseApi) UpdateWebsiteDnsAccount(c *gin.Context) {
}
func (b *BaseApi) DeleteWebsiteDnsAccount(c *gin.Context) {
id, err := helper.GetParamID(c)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)

View File

@ -3,13 +3,14 @@ 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"
"reflect"
)
func (b *BaseApi) PageWebsiteSSL(c *gin.Context) {
var req dto.WebsiteSSLSearch
var req request.WebsiteSSLSearch
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
@ -35,7 +36,7 @@ func (b *BaseApi) PageWebsiteSSL(c *gin.Context) {
}
func (b *BaseApi) CreateWebsiteSSL(c *gin.Context) {
var req dto.WebsiteSSLCreate
var req request.WebsiteSSLCreate
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
@ -49,7 +50,7 @@ func (b *BaseApi) CreateWebsiteSSL(c *gin.Context) {
}
func (b *BaseApi) RenewWebsiteSSL(c *gin.Context) {
var req dto.WebsiteSSLRenew
var req request.WebsiteSSLRenew
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
@ -62,7 +63,7 @@ func (b *BaseApi) RenewWebsiteSSL(c *gin.Context) {
}
func (b *BaseApi) GetDNSResolve(c *gin.Context) {
var req dto.WebsiteDNSReq
var req request.WebsiteDNSReq
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
@ -76,7 +77,6 @@ func (b *BaseApi) GetDNSResolve(c *gin.Context) {
}
func (b *BaseApi) DeleteWebsiteSSL(c *gin.Context) {
id, err := helper.GetParamID(c)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
@ -90,7 +90,6 @@ func (b *BaseApi) DeleteWebsiteSSL(c *gin.Context) {
}
func (b *BaseApi) GetWebsiteSSL(c *gin.Context) {
websiteId, err := helper.GetIntParamByKey(c, "websiteId")
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)

View File

@ -0,0 +1,41 @@
package request
import "github.com/1Panel-dev/1Panel/backend/app/dto"
type WebsiteSSLSearch struct {
dto.PageInfo
}
type WebsiteSSLCreate struct {
PrimaryDomain string `json:"primaryDomain" validate:"required"`
OtherDomains string `json:"otherDomains"`
Provider string `json:"provider" validate:"required"`
AcmeAccountID uint `json:"acmeAccountId" validate:"required"`
DnsAccountID uint `json:"dnsAccountId"`
}
type WebsiteDNSReq struct {
Domains []string `json:"domains" validate:"required"`
AcmeAccountID uint `json:"acmeAccountId" validate:"required"`
}
type WebsiteSSLRenew struct {
SSLID uint `json:"SSLId" validate:"required"`
}
type WebsiteAcmeAccountCreate struct {
Email string `json:"email" validate:"required"`
}
type WebsiteDnsAccountCreate struct {
Name string `json:"name" validate:"required"`
Type string `json:"type" validate:"required"`
Authorization map[string]string `json:"authorization" validate:"required"`
}
type WebsiteDnsAccountUpdate struct {
ID uint `json:"id" validate:"required"`
Name string `json:"name" validate:"required"`
Type string `json:"type" validate:"required"`
Authorization map[string]string `json:"authorization" validate:"required"`
}

View File

@ -0,0 +1,23 @@
package response
import "github.com/1Panel-dev/1Panel/backend/app/model"
type WebsiteSSLDTO struct {
model.WebsiteSSL
}
type WebsiteDNSRes struct {
Key string `json:"resolve"`
Value string `json:"value"`
Domain string `json:"domain"`
Err string `json:"err"`
}
type WebsiteAcmeAccountDTO struct {
model.WebsiteAcmeAccount
}
type WebsiteDnsAccountDTO struct {
model.WebsiteDnsAccount
Authorization map[string]string `json:"authorization"`
}

View File

@ -1 +0,0 @@
package dto

View File

@ -1,11 +0,0 @@
package dto
import "github.com/1Panel-dev/1Panel/backend/app/model"
type WebsiteAcmeAccountDTO struct {
model.WebsiteAcmeAccount
}
type WebsiteAcmeAccountCreate struct {
Email string `json:"email" validate:"required"`
}

View File

@ -1,21 +0,0 @@
package dto
import "github.com/1Panel-dev/1Panel/backend/app/model"
type WebsiteDnsAccountDTO struct {
model.WebsiteDnsAccount
Authorization map[string]string `json:"authorization"`
}
type WebsiteDnsAccountCreate struct {
Name string `json:"name" validate:"required"`
Type string `json:"type" validate:"required"`
Authorization map[string]string `json:"authorization" validate:"required"`
}
type WebsiteDnsAccountUpdate struct {
ID uint `json:"id" validate:"required"`
Name string `json:"name" validate:"required"`
Type string `json:"type" validate:"required"`
Authorization map[string]string `json:"authorization" validate:"required"`
}

View File

@ -1,49 +0,0 @@
package dto
import "github.com/1Panel-dev/1Panel/backend/app/model"
type WebsiteSSLDTO struct {
model.WebsiteSSL
}
type SSLProvider string
const (
DNSAccount = "dnsAccount"
DnsManual = "dnsManual"
Http = "http"
Manual = "manual"
)
type WebsiteSSLSearch struct {
PageInfo
}
type WebsiteSSLCreate struct {
PrimaryDomain string `json:"primaryDomain" validate:"required"`
OtherDomains string `json:"otherDomains"`
Provider SSLProvider `json:"provider" validate:"required"`
AcmeAccountID uint `json:"acmeAccountId" validate:"required"`
DnsAccountID uint `json:"dnsAccountId"`
}
type WebsiteSSLApply struct {
WebsiteID uint `json:"websiteId" validate:"required"`
Enable bool `json:"enable" validate:"required"`
}
type WebsiteDNSReq struct {
Domains []string `json:"domains" validate:"required"`
AcmeAccountID uint `json:"acmeAccountId" validate:"required"`
}
type WebsiteDNSRes struct {
Key string `json:"resolve"`
Value string `json:"value"`
Domain string `json:"domain"`
Err string `json:"err"`
}
type WebsiteSSLRenew struct {
SSLID uint `json:"SSLId" validate:"required"`
}

View File

@ -477,7 +477,7 @@ func (w WebsiteService) OpWebsiteHTTPS(req request.WebsiteHTTPSOp) (response.Web
}
}
websiteSSL.Provider = dto.Manual
websiteSSL.Provider = constant.Manual
websiteSSL.PrivateKey = req.PrivateKey
websiteSSL.Pem = req.Certificate

View File

@ -2,6 +2,8 @@ package service
import (
"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/buserr"
"github.com/1Panel-dev/1Panel/backend/constant"
@ -11,21 +13,21 @@ import (
type WebsiteAcmeAccountService struct {
}
func (w WebsiteAcmeAccountService) Page(search dto.PageInfo) (int64, []dto.WebsiteAcmeAccountDTO, error) {
func (w WebsiteAcmeAccountService) Page(search dto.PageInfo) (int64, []response.WebsiteAcmeAccountDTO, error) {
total, accounts, err := websiteAcmeRepo.Page(search.Page, search.PageSize, commonRepo.WithOrderBy("created_at desc"))
var accountDTOs []dto.WebsiteAcmeAccountDTO
var accountDTOs []response.WebsiteAcmeAccountDTO
for _, account := range accounts {
accountDTOs = append(accountDTOs, dto.WebsiteAcmeAccountDTO{
accountDTOs = append(accountDTOs, response.WebsiteAcmeAccountDTO{
WebsiteAcmeAccount: account,
})
}
return total, accountDTOs, err
}
func (w WebsiteAcmeAccountService) Create(create dto.WebsiteAcmeAccountCreate) (dto.WebsiteAcmeAccountDTO, error) {
func (w WebsiteAcmeAccountService) Create(create request.WebsiteAcmeAccountCreate) (response.WebsiteAcmeAccountDTO, error) {
client, err := ssl.NewAcmeClient(create.Email, "")
if err != nil {
return dto.WebsiteAcmeAccountDTO{}, err
return response.WebsiteAcmeAccountDTO{}, err
}
acmeAccount := model.WebsiteAcmeAccount{
Email: create.Email,
@ -33,9 +35,9 @@ func (w WebsiteAcmeAccountService) Create(create dto.WebsiteAcmeAccountCreate) (
PrivateKey: string(ssl.GetPrivateKey(client.User.GetPrivateKey())),
}
if err := websiteAcmeRepo.Create(acmeAccount); err != nil {
return dto.WebsiteAcmeAccountDTO{}, err
return response.WebsiteAcmeAccountDTO{}, err
}
return dto.WebsiteAcmeAccountDTO{WebsiteAcmeAccount: acmeAccount}, nil
return response.WebsiteAcmeAccountDTO{WebsiteAcmeAccount: acmeAccount}, nil
}
func (w WebsiteAcmeAccountService) Delete(id uint) error {

View File

@ -2,6 +2,8 @@ package service
import (
"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/buserr"
"github.com/1Panel-dev/1Panel/backend/constant"
@ -11,13 +13,13 @@ import (
type WebsiteDnsAccountService struct {
}
func (w WebsiteDnsAccountService) Page(search dto.PageInfo) (int64, []dto.WebsiteDnsAccountDTO, error) {
func (w WebsiteDnsAccountService) Page(search dto.PageInfo) (int64, []response.WebsiteDnsAccountDTO, error) {
total, accounts, err := websiteDnsRepo.Page(search.Page, search.PageSize, commonRepo.WithOrderBy("created_at desc"))
var accountDTOs []dto.WebsiteDnsAccountDTO
var accountDTOs []response.WebsiteDnsAccountDTO
for _, account := range accounts {
auth := make(map[string]string)
_ = json.Unmarshal([]byte(account.Authorization), &auth)
accountDTOs = append(accountDTOs, dto.WebsiteDnsAccountDTO{
accountDTOs = append(accountDTOs, response.WebsiteDnsAccountDTO{
WebsiteDnsAccount: account,
Authorization: auth,
})
@ -25,10 +27,10 @@ func (w WebsiteDnsAccountService) Page(search dto.PageInfo) (int64, []dto.Websit
return total, accountDTOs, err
}
func (w WebsiteDnsAccountService) Create(create dto.WebsiteDnsAccountCreate) (dto.WebsiteDnsAccountCreate, error) {
func (w WebsiteDnsAccountService) Create(create request.WebsiteDnsAccountCreate) (request.WebsiteDnsAccountCreate, error) {
authorization, err := json.Marshal(create.Authorization)
if err != nil {
return dto.WebsiteDnsAccountCreate{}, err
return request.WebsiteDnsAccountCreate{}, err
}
if err := websiteDnsRepo.Create(model.WebsiteDnsAccount{
@ -36,16 +38,16 @@ func (w WebsiteDnsAccountService) Create(create dto.WebsiteDnsAccountCreate) (dt
Type: create.Type,
Authorization: string(authorization),
}); err != nil {
return dto.WebsiteDnsAccountCreate{}, err
return request.WebsiteDnsAccountCreate{}, err
}
return create, nil
}
func (w WebsiteDnsAccountService) Update(update dto.WebsiteDnsAccountUpdate) (dto.WebsiteDnsAccountUpdate, error) {
func (w WebsiteDnsAccountService) Update(update request.WebsiteDnsAccountUpdate) (request.WebsiteDnsAccountUpdate, error) {
authorization, err := json.Marshal(update.Authorization)
if err != nil {
return dto.WebsiteDnsAccountUpdate{}, err
return request.WebsiteDnsAccountUpdate{}, err
}
if err := websiteDnsRepo.Save(model.WebsiteDnsAccount{
@ -56,7 +58,7 @@ func (w WebsiteDnsAccountService) Update(update dto.WebsiteDnsAccountUpdate) (dt
Type: update.Type,
Authorization: string(authorization),
}); err != nil {
return dto.WebsiteDnsAccountUpdate{}, err
return request.WebsiteDnsAccountUpdate{}, err
}
return update, nil

View File

@ -4,7 +4,8 @@ import (
"context"
"crypto/x509"
"encoding/pem"
"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/buserr"
"github.com/1Panel-dev/1Panel/backend/constant"
@ -16,49 +17,47 @@ import (
type WebsiteSSLService struct {
}
func (w WebsiteSSLService) Page(search dto.WebsiteSSLSearch) (int64, []dto.WebsiteSSLDTO, error) {
func (w WebsiteSSLService) Page(search request.WebsiteSSLSearch) (int64, []response.WebsiteSSLDTO, error) {
total, sslList, err := websiteSSLRepo.Page(search.Page, search.PageSize, commonRepo.WithOrderBy("created_at desc"))
if err != nil {
return 0, nil, err
}
var sslDTOs []dto.WebsiteSSLDTO
var sslDTOs []response.WebsiteSSLDTO
for _, ssl := range sslList {
sslDTOs = append(sslDTOs, dto.WebsiteSSLDTO{
sslDTOs = append(sslDTOs, response.WebsiteSSLDTO{
WebsiteSSL: ssl,
})
}
return total, sslDTOs, err
}
func (w WebsiteSSLService) Search() ([]dto.WebsiteSSLDTO, error) {
func (w WebsiteSSLService) Search() ([]response.WebsiteSSLDTO, error) {
sslList, err := websiteSSLRepo.List()
if err != nil {
return nil, err
}
var sslDTOs []dto.WebsiteSSLDTO
var sslDTOs []response.WebsiteSSLDTO
for _, ssl := range sslList {
sslDTOs = append(sslDTOs, dto.WebsiteSSLDTO{
sslDTOs = append(sslDTOs, response.WebsiteSSLDTO{
WebsiteSSL: ssl,
})
}
return sslDTOs, err
}
func (w WebsiteSSLService) Create(create dto.WebsiteSSLCreate) (dto.WebsiteSSLCreate, error) {
var res dto.WebsiteSSLCreate
func (w WebsiteSSLService) Create(create request.WebsiteSSLCreate) (request.WebsiteSSLCreate, error) {
var res request.WebsiteSSLCreate
acmeAccount, err := websiteAcmeRepo.GetFirst(commonRepo.WithByID(create.AcmeAccountID))
if err != nil {
return res, err
}
client, err := ssl.NewPrivateKeyClient(acmeAccount.Email, acmeAccount.PrivateKey)
if err != nil {
return res, err
}
switch create.Provider {
case dto.DNSAccount:
case constant.DNSAccount:
dnsAccount, err := websiteDnsRepo.GetFirst(commonRepo.WithByID(create.DnsAccountID))
if err != nil {
return res, err
@ -66,16 +65,14 @@ func (w WebsiteSSLService) Create(create dto.WebsiteSSLCreate) (dto.WebsiteSSLCr
if err := client.UseDns(ssl.DnsType(dnsAccount.Type), dnsAccount.Authorization); err != nil {
return res, err
}
case dto.Http:
case constant.Http:
appInstall, err := getAppInstallByKey(constant.AppNginx)
if err != nil {
return dto.WebsiteSSLCreate{}, err
return request.WebsiteSSLCreate{}, err
}
if err := client.UseHTTP(path.Join(constant.AppInstallDir, constant.AppNginx, appInstall.Name, "root")); err != nil {
return res, err
}
case dto.DnsManual:
}
domains := []string{create.PrimaryDomain}
@ -90,7 +87,7 @@ func (w WebsiteSSLService) Create(create dto.WebsiteSSLCreate) (dto.WebsiteSSLCr
var websiteSSL model.WebsiteSSL
websiteSSL.DnsAccountID = create.DnsAccountID
websiteSSL.AcmeAccountID = acmeAccount.ID
websiteSSL.Provider = string(create.Provider)
websiteSSL.Provider = create.Provider
websiteSSL.Domains = strings.Join(otherDomainArray, ",")
websiteSSL.PrimaryDomain = create.PrimaryDomain
websiteSSL.PrivateKey = string(resource.PrivateKey)
@ -99,7 +96,7 @@ func (w WebsiteSSLService) Create(create dto.WebsiteSSLCreate) (dto.WebsiteSSLCr
certBlock, _ := pem.Decode(resource.Certificate)
cert, err := x509.ParseCertificate(certBlock.Bytes)
if err != nil {
return dto.WebsiteSSLCreate{}, err
return request.WebsiteSSLCreate{}, err
}
websiteSSL.ExpireDate = cert.NotAfter
websiteSSL.StartDate = cert.NotBefore
@ -114,7 +111,6 @@ func (w WebsiteSSLService) Create(create dto.WebsiteSSLCreate) (dto.WebsiteSSLCr
}
func (w WebsiteSSLService) Renew(sslId uint) error {
websiteSSL, err := websiteSSLRepo.GetFirst(commonRepo.WithByID(sslId))
if err != nil {
return err
@ -129,7 +125,7 @@ func (w WebsiteSSLService) Renew(sslId uint) error {
return err
}
switch websiteSSL.Provider {
case dto.DNSAccount:
case constant.DNSAccount:
dnsAccount, err := websiteDnsRepo.GetFirst(commonRepo.WithByID(websiteSSL.DnsAccountID))
if err != nil {
return err
@ -137,7 +133,7 @@ func (w WebsiteSSLService) Renew(sslId uint) error {
if err := client.UseDns(ssl.DnsType(dnsAccount.Type), dnsAccount.Authorization); err != nil {
return err
}
case dto.Http:
case constant.Http:
appInstall, err := getAppInstallByKey(constant.AppNginx)
if err != nil {
return err
@ -145,7 +141,7 @@ func (w WebsiteSSLService) Renew(sslId uint) error {
if err := client.UseHTTP(path.Join(constant.AppInstallDir, constant.AppNginx, appInstall.Name, "root")); err != nil {
return err
}
case dto.DnsManual:
case constant.DnsManual:
}
@ -169,7 +165,7 @@ func (w WebsiteSSLService) Renew(sslId uint) error {
return websiteSSLRepo.Save(websiteSSL)
}
func (w WebsiteSSLService) GetDNSResolve(req dto.WebsiteDNSReq) ([]dto.WebsiteDNSRes, error) {
func (w WebsiteSSLService) GetDNSResolve(req request.WebsiteDNSReq) ([]response.WebsiteDNSRes, error) {
acmeAccount, err := websiteAcmeRepo.GetFirst(commonRepo.WithByID(req.AcmeAccountID))
if err != nil {
return nil, err
@ -183,9 +179,9 @@ func (w WebsiteSSLService) GetDNSResolve(req dto.WebsiteDNSReq) ([]dto.WebsiteDN
if err != nil {
return nil, err
}
var res []dto.WebsiteDNSRes
var res []response.WebsiteDNSRes
for k, v := range resolves {
res = append(res, dto.WebsiteDNSRes{
res = append(res, response.WebsiteDNSRes{
Domain: k,
Key: v.Key,
Value: v.Value,
@ -195,8 +191,8 @@ func (w WebsiteSSLService) GetDNSResolve(req dto.WebsiteDNSReq) ([]dto.WebsiteDN
return res, nil
}
func (w WebsiteSSLService) GetWebsiteSSL(websiteId uint) (dto.WebsiteSSLDTO, error) {
var res dto.WebsiteSSLDTO
func (w WebsiteSSLService) GetWebsiteSSL(websiteId uint) (response.WebsiteSSLDTO, error) {
var res response.WebsiteSSLDTO
website, err := websiteRepo.GetFirst(commonRepo.WithByID(websiteId))
if err != nil {
return res, err

View File

@ -20,4 +20,9 @@ const (
SSLExisted = "existed"
SSLAuto = "auto"
SSLManual = "manual"
DNSAccount = "dnsAccount"
DnsManual = "dnsManual"
Http = "http"
Manual = "manual"
)