diff --git a/app/http/controllers/cert_controller.go b/app/http/controllers/cert_controller.go index 14b5647c..43ea6185 100644 --- a/app/http/controllers/cert_controller.go +++ b/app/http/controllers/cert_controller.go @@ -445,7 +445,7 @@ func (r *CertController) CertList(ctx http.Context) http.Response { var certs []models.Cert var total int64 - err := facades.Orm().Query().Paginate(paginateRequest.Page, paginateRequest.Limit, &certs, &total) + err := facades.Orm().Query().With("Website").With("User").With("DNS").Paginate(paginateRequest.Page, paginateRequest.Limit, &certs, &total) if err != nil { facades.Log().Request(ctx.Request()).Tags("面板", "证书管理").With(map[string]any{ "error": err.Error(), @@ -619,7 +619,7 @@ func (r *CertController) Obtain(ctx http.Context) http.Response { facades.Log().Request(ctx.Request()).Tags("面板", "证书管理").With(map[string]any{ "error": err.Error(), }).Error("签发证书失败") - return ErrorSystem(ctx) + return Error(ctx, http.StatusInternalServerError, err.Error()) } return Success(ctx, nil) @@ -649,7 +649,7 @@ func (r *CertController) Renew(ctx http.Context) http.Response { facades.Log().Request(ctx.Request()).Tags("面板", "证书管理").With(map[string]any{ "error": err.Error(), }).Error("续签证书失败") - return ErrorSystem(ctx) + return Error(ctx, http.StatusInternalServerError, err.Error()) } return Success(ctx, nil) @@ -679,7 +679,7 @@ func (r *CertController) ManualDNS(ctx http.Context) http.Response { facades.Log().Request(ctx.Request()).Tags("面板", "证书管理").With(map[string]any{ "error": err.Error(), }).Error("获取手动DNS记录失败") - return ErrorSystem(ctx) + return Error(ctx, http.StatusInternalServerError, err.Error()) } return Success(ctx, resolves) diff --git a/app/http/requests/cert/cert_store.go b/app/http/requests/cert/cert_store.go index 33e2d7ae..8edc1cd7 100644 --- a/app/http/requests/cert/cert_store.go +++ b/app/http/requests/cert/cert_store.go @@ -3,13 +3,14 @@ package requests import ( "github.com/goravel/framework/contracts/http" "github.com/goravel/framework/contracts/validation" + "github.com/spf13/cast" ) type CertStore struct { Type string `form:"type" json:"type"` Domains []string `form:"domains" json:"domains"` AutoRenew bool `form:"auto_renew" json:"auto_renew"` - UserID uint `form:"user_id" json:"user_id"` + UserID uint `form:"user_id" json:"user_id" filter:"uint"` DNSID *uint `form:"dns_id" json:"dns_id"` WebsiteID *uint `form:"website_id" json:"website_id"` } @@ -23,7 +24,9 @@ func (r *CertStore) Rules(ctx http.Context) map[string]string { "type": "required|in:P256,P384,2048,4096", "domains": "required|array", "auto_renew": "required|bool", - "user_id": "required|exists:cert_users,id", + "user_id": "required|uint|exists:cert_users,id", + "dns_id": "uint", + "website_id": "uint", } } @@ -36,5 +39,22 @@ func (r *CertStore) Attributes(ctx http.Context) map[string]string { } func (r *CertStore) PrepareForValidation(ctx http.Context, data validation.Data) error { + // TODO 由于验证器 filter 标签的问题,暂时这里这样处理 + dnsID, exist := data.Get("dns_id") + if exist { + err := data.Set("dns_id", cast.ToUint(dnsID)) + if err != nil { + return err + } + + } + websiteID, exist := data.Get("website_id") + if exist { + err := data.Set("website_id", cast.ToUint(websiteID)) + if err != nil { + return err + } + } + return nil } diff --git a/app/http/requests/cert/cert_update.go b/app/http/requests/cert/cert_update.go index a5804551..f197396d 100644 --- a/app/http/requests/cert/cert_update.go +++ b/app/http/requests/cert/cert_update.go @@ -3,6 +3,7 @@ package requests import ( "github.com/goravel/framework/contracts/http" "github.com/goravel/framework/contracts/validation" + "github.com/spf13/cast" ) type CertUpdate struct { @@ -10,7 +11,7 @@ type CertUpdate struct { Type string `form:"type" json:"type"` Domains []string `form:"domains" json:"domains"` AutoRenew bool `form:"auto_renew" json:"auto_renew"` - UserID uint `form:"user_id" json:"user_id"` + UserID uint `form:"user_id" json:"user_id" filter:"uint"` DNSID *uint `form:"dns_id" json:"dns_id"` WebsiteID *uint `form:"website_id" json:"website_id"` } @@ -25,7 +26,9 @@ func (r *CertUpdate) Rules(ctx http.Context) map[string]string { "type": "required|in:P256,P384,2048,4096", "domains": "required|array", "auto_renew": "required|bool", - "user_id": "required|exists:cert_users,id", + "user_id": "required|uint|exists:cert_users,id", + "dns_id": "uint", + "website_id": "uint", } } @@ -38,5 +41,22 @@ func (r *CertUpdate) Attributes(ctx http.Context) map[string]string { } func (r *CertUpdate) PrepareForValidation(ctx http.Context, data validation.Data) error { + // TODO 由于验证器 filter 标签的问题,暂时这里这样处理 + dnsID, exist := data.Get("dns_id") + if exist { + err := data.Set("dns_id", cast.ToUint(dnsID)) + if err != nil { + return err + } + + } + websiteID, exist := data.Get("website_id") + if exist { + err := data.Set("website_id", cast.ToUint(websiteID)) + if err != nil { + return err + } + } + return nil } diff --git a/app/http/requests/setting/update.go b/app/http/requests/setting/update.go index 9556afb5..a15c823d 100644 --- a/app/http/requests/setting/update.go +++ b/app/http/requests/setting/update.go @@ -7,7 +7,7 @@ import ( type Update struct { Name string `form:"name" json:"name"` - Port uint `form:"port" json:"port" filter:"int"` + Port uint `form:"port" json:"port" filter:"uint"` BackupPath string `form:"backup_path" json:"backup_path"` WebsitePath string `form:"website_path" json:"website_path"` Entrance string `form:"entrance" json:"entrance"` diff --git a/app/models/cert.go b/app/models/cert.go index d0c19b21..d013fd50 100644 --- a/app/models/cert.go +++ b/app/models/cert.go @@ -18,7 +18,7 @@ type Cert struct { CreatedAt carbon.DateTime `gorm:"autoCreateTime;column:created_at" json:"created_at"` UpdatedAt carbon.DateTime `gorm:"autoUpdateTime;column:updated_at" json:"updated_at"` - Website *Website `gorm:"foreignKey:WebsiteID" json:"-"` - User *CertUser `gorm:"foreignKey:UserID" json:"-"` - DNS *CertDNS `gorm:"foreignKey:DNSID" json:"-"` + Website *Website `gorm:"foreignKey:WebsiteID" json:"website"` + User *CertUser `gorm:"foreignKey:UserID" json:"user"` + DNS *CertDNS `gorm:"foreignKey:DNSID" json:"dns"` } diff --git a/app/services/cert.go b/app/services/cert.go index 27fa190c..b5923024 100644 --- a/app/services/cert.go +++ b/app/services/cert.go @@ -410,7 +410,7 @@ func (s *CertImpl) ManualDNS(ID uint) (map[string]acme.Resolve, error) { // Renew 续签证书 func (s *CertImpl) Renew(ID uint) (certificate.Resource, error) { var cert models.Cert - err := facades.Orm().Query().With("User").With("DNS").Where("id = ?", ID).First(&cert) + err := facades.Orm().Query().With("Website").With("User").With("DNS").Where("id = ?", ID).First(&cert) if err != nil { return certificate.Resource{}, err } diff --git a/docs/docs.go b/docs/docs.go index c2ff860e..ed37f514 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -1348,6 +1348,9 @@ const docTemplate = `{ "created_at": { "type": "string" }, + "dns": { + "$ref": "#/definitions/models.CertDNS" + }, "dns_id": { "description": "关联的 DNS ID", "type": "integer" @@ -1372,10 +1375,16 @@ const docTemplate = `{ "updated_at": { "type": "string" }, + "user": { + "$ref": "#/definitions/models.CertUser" + }, "user_id": { "description": "关联的 ACME 用户 ID", "type": "integer" }, + "website": { + "$ref": "#/definitions/models.Website" + }, "website_id": { "description": "关联的网站 ID", "type": "integer" @@ -1440,6 +1449,41 @@ const docTemplate = `{ } } }, + "models.Website": { + "type": "object", + "properties": { + "cert": { + "$ref": "#/definitions/models.Cert" + }, + "created_at": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "name": { + "type": "string" + }, + "path": { + "type": "string" + }, + "php": { + "type": "integer" + }, + "remark": { + "type": "string" + }, + "ssl": { + "type": "boolean" + }, + "status": { + "type": "boolean" + }, + "updated_at": { + "type": "string" + } + } + }, "requests.CertStore": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index aad3edb2..a6b3302a 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -1341,6 +1341,9 @@ "created_at": { "type": "string" }, + "dns": { + "$ref": "#/definitions/models.CertDNS" + }, "dns_id": { "description": "关联的 DNS ID", "type": "integer" @@ -1365,10 +1368,16 @@ "updated_at": { "type": "string" }, + "user": { + "$ref": "#/definitions/models.CertUser" + }, "user_id": { "description": "关联的 ACME 用户 ID", "type": "integer" }, + "website": { + "$ref": "#/definitions/models.Website" + }, "website_id": { "description": "关联的网站 ID", "type": "integer" @@ -1433,6 +1442,41 @@ } } }, + "models.Website": { + "type": "object", + "properties": { + "cert": { + "$ref": "#/definitions/models.Cert" + }, + "created_at": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "name": { + "type": "string" + }, + "path": { + "type": "string" + }, + "php": { + "type": "integer" + }, + "remark": { + "type": "string" + }, + "ssl": { + "type": "boolean" + }, + "status": { + "type": "boolean" + }, + "updated_at": { + "type": "string" + } + } + }, "requests.CertStore": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 90275d07..75dc8afb 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -52,6 +52,8 @@ definitions: type: string created_at: type: string + dns: + $ref: '#/definitions/models.CertDNS' dns_id: description: 关联的 DNS ID type: integer @@ -69,9 +71,13 @@ definitions: type: string updated_at: type: string + user: + $ref: '#/definitions/models.CertUser' user_id: description: 关联的 ACME 用户 ID type: integer + website: + $ref: '#/definitions/models.Website' website_id: description: 关联的网站 ID type: integer @@ -115,6 +121,29 @@ definitions: updated_at: type: string type: object + models.Website: + properties: + cert: + $ref: '#/definitions/models.Cert' + created_at: + type: string + id: + type: integer + name: + type: string + path: + type: string + php: + type: integer + remark: + type: string + ssl: + type: boolean + status: + type: boolean + updated_at: + type: string + type: object requests.CertStore: properties: auto_renew: diff --git a/pkg/acme/dns_manual.go b/pkg/acme/dns_manual.go index 56c0a2f0..61c0daa4 100644 --- a/pkg/acme/dns_manual.go +++ b/pkg/acme/dns_manual.go @@ -1,9 +1,9 @@ package acme type Resolve struct { - Key string - Value string - Err string + Key string `json:"key"` + Value string `json:"value"` + Err string `json:"err"` } type manualDnsProvider struct {