Skip to content

Commit

Permalink
enhance: show the form error details of CertificateEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJacky committed Jan 28, 2024
1 parent 8581bdd commit b0a3989
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 147 deletions.
282 changes: 141 additions & 141 deletions api/certificate/certificate.go
Original file line number Diff line number Diff line change
@@ -1,174 +1,174 @@
package certificate

import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/api/cosy"
"github.com/0xJacky/Nginx-UI/internal/cert"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"net/http"
"os"
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/api/cosy"
"github.com/0xJacky/Nginx-UI/internal/cert"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"net/http"
"os"
)

type APICertificate struct {
*model.Cert
SSLCertificate string `json:"ssl_certificate,omitempty"`
SSLCertificateKey string `json:"ssl_certificate_key,omitempty"`
CertificateInfo *cert.Info `json:"certificate_info,omitempty"`
*model.Cert
SSLCertificate string `json:"ssl_certificate,omitempty"`
SSLCertificateKey string `json:"ssl_certificate_key,omitempty"`
CertificateInfo *cert.Info `json:"certificate_info,omitempty"`
}

func Transformer(certModel *model.Cert) (certificate *APICertificate) {
var sslCertificationBytes, sslCertificationKeyBytes []byte
var certificateInfo *cert.Info
if certModel.SSLCertificatePath != "" {
if _, err := os.Stat(certModel.SSLCertificatePath); err == nil {
sslCertificationBytes, _ = os.ReadFile(certModel.SSLCertificatePath)
if !cert.IsPublicKey(string(sslCertificationBytes)) {
sslCertificationBytes = []byte{}
}
}

certificateInfo, _ = cert.GetCertInfo(certModel.SSLCertificatePath)
}

if certModel.SSLCertificateKeyPath != "" {
if _, err := os.Stat(certModel.SSLCertificateKeyPath); err == nil {
sslCertificationKeyBytes, _ = os.ReadFile(certModel.SSLCertificateKeyPath)
if !cert.IsPrivateKey(string(sslCertificationKeyBytes)) {
sslCertificationKeyBytes = []byte{}
}
}
}

return &APICertificate{
Cert: certModel,
SSLCertificate: string(sslCertificationBytes),
SSLCertificateKey: string(sslCertificationKeyBytes),
CertificateInfo: certificateInfo,
}
var sslCertificationBytes, sslCertificationKeyBytes []byte
var certificateInfo *cert.Info
if certModel.SSLCertificatePath != "" {
if _, err := os.Stat(certModel.SSLCertificatePath); err == nil {
sslCertificationBytes, _ = os.ReadFile(certModel.SSLCertificatePath)
if !cert.IsPublicKey(string(sslCertificationBytes)) {
sslCertificationBytes = []byte{}
}
}

certificateInfo, _ = cert.GetCertInfo(certModel.SSLCertificatePath)
}

if certModel.SSLCertificateKeyPath != "" {
if _, err := os.Stat(certModel.SSLCertificateKeyPath); err == nil {
sslCertificationKeyBytes, _ = os.ReadFile(certModel.SSLCertificateKeyPath)
if !cert.IsPrivateKey(string(sslCertificationKeyBytes)) {
sslCertificationKeyBytes = []byte{}
}
}
}

return &APICertificate{
Cert: certModel,
SSLCertificate: string(sslCertificationBytes),
SSLCertificateKey: string(sslCertificationKeyBytes),
CertificateInfo: certificateInfo,
}
}

func GetCertList(c *gin.Context) {
cosy.Core[model.Cert](c).SetFussy("name", "domain").SetTransformer(func(m *model.Cert) any {
cosy.Core[model.Cert](c).SetFussy("name", "domain").SetTransformer(func(m *model.Cert) any {

info, _ := cert.GetCertInfo(m.SSLCertificatePath)
info, _ := cert.GetCertInfo(m.SSLCertificatePath)

return APICertificate{
Cert: m,
CertificateInfo: info,
}
}).PagingList()
return APICertificate{
Cert: m,
CertificateInfo: info,
}
}).PagingList()
}

func GetCert(c *gin.Context) {
q := query.Cert
q := query.Cert

certModel, err := q.FirstByID(cast.ToInt(c.Param("id")))
certModel, err := q.FirstByID(cast.ToInt(c.Param("id")))

if err != nil {
api.ErrHandler(c, err)
return
}
if err != nil {
api.ErrHandler(c, err)
return
}

c.JSON(http.StatusOK, Transformer(certModel))
c.JSON(http.StatusOK, Transformer(certModel))
}

type certJson struct {
Name string `json:"name"`
SSLCertificatePath string `json:"ssl_certificate_path" binding:"publickey_path"`
SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"privatekey_path"`
SSLCertificate string `json:"ssl_certificate" binding:"omitempty,publickey"`
SSLCertificateKey string `json:"ssl_certificate_key" binding:"omitempty,privatekey"`
ChallengeMethod string `json:"challenge_method"`
DnsCredentialID int `json:"dns_credential_id"`
Name string `json:"name" binding:"required"`
SSLCertificatePath string `json:"ssl_certificate_path" binding:"required,publickey_path"`
SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required,privatekey_path"`
SSLCertificate string `json:"ssl_certificate" binding:"omitempty,publickey"`
SSLCertificateKey string `json:"ssl_certificate_key" binding:"omitempty,privatekey"`
ChallengeMethod string `json:"challenge_method"`
DnsCredentialID int `json:"dns_credential_id"`
}

func AddCert(c *gin.Context) {
var json certJson
if !api.BindAndValid(c, &json) {
return
}
certModel := &model.Cert{
Name: json.Name,
SSLCertificatePath: json.SSLCertificatePath,
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
ChallengeMethod: json.ChallengeMethod,
DnsCredentialID: json.DnsCredentialID,
}

err := certModel.Insert()

if err != nil {
api.ErrHandler(c, err)
return
}

content := &cert.Content{
SSLCertificatePath: json.SSLCertificatePath,
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
SSLCertificate: json.SSLCertificate,
SSLCertificateKey: json.SSLCertificateKey,
}

err = content.WriteFile()

if err != nil {
api.ErrHandler(c, err)
return
}

c.JSON(http.StatusOK, Transformer(certModel))
var json certJson
if !api.BindAndValid(c, &json) {
return
}
certModel := &model.Cert{
Name: json.Name,
SSLCertificatePath: json.SSLCertificatePath,
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
ChallengeMethod: json.ChallengeMethod,
DnsCredentialID: json.DnsCredentialID,
}

err := certModel.Insert()

if err != nil {
api.ErrHandler(c, err)
return
}

content := &cert.Content{
SSLCertificatePath: json.SSLCertificatePath,
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
SSLCertificate: json.SSLCertificate,
SSLCertificateKey: json.SSLCertificateKey,
}

err = content.WriteFile()

if err != nil {
api.ErrHandler(c, err)
return
}

c.JSON(http.StatusOK, Transformer(certModel))
}

func ModifyCert(c *gin.Context) {
id := cast.ToInt(c.Param("id"))

var json certJson

if !api.BindAndValid(c, &json) {
return
}

q := query.Cert

certModel, err := q.FirstByID(id)
if err != nil {
api.ErrHandler(c, err)
return
}

err = certModel.Updates(&model.Cert{
Name: json.Name,
SSLCertificatePath: json.SSLCertificatePath,
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
ChallengeMethod: json.ChallengeMethod,
DnsCredentialID: json.DnsCredentialID,
})

if err != nil {
api.ErrHandler(c, err)
return
}

content := &cert.Content{
SSLCertificatePath: json.SSLCertificatePath,
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
SSLCertificate: json.SSLCertificate,
SSLCertificateKey: json.SSLCertificateKey,
}

err = content.WriteFile()

if err != nil {
api.ErrHandler(c, err)
return
}

GetCert(c)
id := cast.ToInt(c.Param("id"))

var json certJson

if !api.BindAndValid(c, &json) {
return
}

q := query.Cert

certModel, err := q.FirstByID(id)
if err != nil {
api.ErrHandler(c, err)
return
}

err = certModel.Updates(&model.Cert{
Name: json.Name,
SSLCertificatePath: json.SSLCertificatePath,
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
ChallengeMethod: json.ChallengeMethod,
DnsCredentialID: json.DnsCredentialID,
})

if err != nil {
api.ErrHandler(c, err)
return
}

content := &cert.Content{
SSLCertificatePath: json.SSLCertificatePath,
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
SSLCertificate: json.SSLCertificate,
SSLCertificateKey: json.SSLCertificateKey,
}

err = content.WriteFile()

if err != nil {
api.ErrHandler(c, err)
return
}

GetCert(c)
}

func RemoveCert(c *gin.Context) {
cosy.Core[model.Cert](c).Destroy()
cosy.Core[model.Cert](c).Destroy()
}
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nginx-ui-app-next",
"version": "2.0.0-beta.11",
"version": "2.0.0-beta.12",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
Loading

0 comments on commit b0a3989

Please sign in to comment.