Skip to content

Commit b0a3989

Browse files
committed
enhance: show the form error details of CertificateEditor
1 parent 8581bdd commit b0a3989

File tree

4 files changed

+185
-147
lines changed

4 files changed

+185
-147
lines changed

api/certificate/certificate.go

Lines changed: 141 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,174 +1,174 @@
11
package certificate
22

33
import (
4-
"github.com/0xJacky/Nginx-UI/api"
5-
"github.com/0xJacky/Nginx-UI/api/cosy"
6-
"github.com/0xJacky/Nginx-UI/internal/cert"
7-
"github.com/0xJacky/Nginx-UI/model"
8-
"github.com/0xJacky/Nginx-UI/query"
9-
"github.com/gin-gonic/gin"
10-
"github.com/spf13/cast"
11-
"net/http"
12-
"os"
4+
"github.com/0xJacky/Nginx-UI/api"
5+
"github.com/0xJacky/Nginx-UI/api/cosy"
6+
"github.com/0xJacky/Nginx-UI/internal/cert"
7+
"github.com/0xJacky/Nginx-UI/model"
8+
"github.com/0xJacky/Nginx-UI/query"
9+
"github.com/gin-gonic/gin"
10+
"github.com/spf13/cast"
11+
"net/http"
12+
"os"
1313
)
1414

1515
type APICertificate struct {
16-
*model.Cert
17-
SSLCertificate string `json:"ssl_certificate,omitempty"`
18-
SSLCertificateKey string `json:"ssl_certificate_key,omitempty"`
19-
CertificateInfo *cert.Info `json:"certificate_info,omitempty"`
16+
*model.Cert
17+
SSLCertificate string `json:"ssl_certificate,omitempty"`
18+
SSLCertificateKey string `json:"ssl_certificate_key,omitempty"`
19+
CertificateInfo *cert.Info `json:"certificate_info,omitempty"`
2020
}
2121

2222
func Transformer(certModel *model.Cert) (certificate *APICertificate) {
23-
var sslCertificationBytes, sslCertificationKeyBytes []byte
24-
var certificateInfo *cert.Info
25-
if certModel.SSLCertificatePath != "" {
26-
if _, err := os.Stat(certModel.SSLCertificatePath); err == nil {
27-
sslCertificationBytes, _ = os.ReadFile(certModel.SSLCertificatePath)
28-
if !cert.IsPublicKey(string(sslCertificationBytes)) {
29-
sslCertificationBytes = []byte{}
30-
}
31-
}
32-
33-
certificateInfo, _ = cert.GetCertInfo(certModel.SSLCertificatePath)
34-
}
35-
36-
if certModel.SSLCertificateKeyPath != "" {
37-
if _, err := os.Stat(certModel.SSLCertificateKeyPath); err == nil {
38-
sslCertificationKeyBytes, _ = os.ReadFile(certModel.SSLCertificateKeyPath)
39-
if !cert.IsPrivateKey(string(sslCertificationKeyBytes)) {
40-
sslCertificationKeyBytes = []byte{}
41-
}
42-
}
43-
}
44-
45-
return &APICertificate{
46-
Cert: certModel,
47-
SSLCertificate: string(sslCertificationBytes),
48-
SSLCertificateKey: string(sslCertificationKeyBytes),
49-
CertificateInfo: certificateInfo,
50-
}
23+
var sslCertificationBytes, sslCertificationKeyBytes []byte
24+
var certificateInfo *cert.Info
25+
if certModel.SSLCertificatePath != "" {
26+
if _, err := os.Stat(certModel.SSLCertificatePath); err == nil {
27+
sslCertificationBytes, _ = os.ReadFile(certModel.SSLCertificatePath)
28+
if !cert.IsPublicKey(string(sslCertificationBytes)) {
29+
sslCertificationBytes = []byte{}
30+
}
31+
}
32+
33+
certificateInfo, _ = cert.GetCertInfo(certModel.SSLCertificatePath)
34+
}
35+
36+
if certModel.SSLCertificateKeyPath != "" {
37+
if _, err := os.Stat(certModel.SSLCertificateKeyPath); err == nil {
38+
sslCertificationKeyBytes, _ = os.ReadFile(certModel.SSLCertificateKeyPath)
39+
if !cert.IsPrivateKey(string(sslCertificationKeyBytes)) {
40+
sslCertificationKeyBytes = []byte{}
41+
}
42+
}
43+
}
44+
45+
return &APICertificate{
46+
Cert: certModel,
47+
SSLCertificate: string(sslCertificationBytes),
48+
SSLCertificateKey: string(sslCertificationKeyBytes),
49+
CertificateInfo: certificateInfo,
50+
}
5151
}
5252

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

56-
info, _ := cert.GetCertInfo(m.SSLCertificatePath)
56+
info, _ := cert.GetCertInfo(m.SSLCertificatePath)
5757

58-
return APICertificate{
59-
Cert: m,
60-
CertificateInfo: info,
61-
}
62-
}).PagingList()
58+
return APICertificate{
59+
Cert: m,
60+
CertificateInfo: info,
61+
}
62+
}).PagingList()
6363
}
6464

6565
func GetCert(c *gin.Context) {
66-
q := query.Cert
66+
q := query.Cert
6767

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

70-
if err != nil {
71-
api.ErrHandler(c, err)
72-
return
73-
}
70+
if err != nil {
71+
api.ErrHandler(c, err)
72+
return
73+
}
7474

75-
c.JSON(http.StatusOK, Transformer(certModel))
75+
c.JSON(http.StatusOK, Transformer(certModel))
7676
}
7777

7878
type certJson struct {
79-
Name string `json:"name"`
80-
SSLCertificatePath string `json:"ssl_certificate_path" binding:"publickey_path"`
81-
SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"privatekey_path"`
82-
SSLCertificate string `json:"ssl_certificate" binding:"omitempty,publickey"`
83-
SSLCertificateKey string `json:"ssl_certificate_key" binding:"omitempty,privatekey"`
84-
ChallengeMethod string `json:"challenge_method"`
85-
DnsCredentialID int `json:"dns_credential_id"`
79+
Name string `json:"name" binding:"required"`
80+
SSLCertificatePath string `json:"ssl_certificate_path" binding:"required,publickey_path"`
81+
SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required,privatekey_path"`
82+
SSLCertificate string `json:"ssl_certificate" binding:"omitempty,publickey"`
83+
SSLCertificateKey string `json:"ssl_certificate_key" binding:"omitempty,privatekey"`
84+
ChallengeMethod string `json:"challenge_method"`
85+
DnsCredentialID int `json:"dns_credential_id"`
8686
}
8787

8888
func AddCert(c *gin.Context) {
89-
var json certJson
90-
if !api.BindAndValid(c, &json) {
91-
return
92-
}
93-
certModel := &model.Cert{
94-
Name: json.Name,
95-
SSLCertificatePath: json.SSLCertificatePath,
96-
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
97-
ChallengeMethod: json.ChallengeMethod,
98-
DnsCredentialID: json.DnsCredentialID,
99-
}
100-
101-
err := certModel.Insert()
102-
103-
if err != nil {
104-
api.ErrHandler(c, err)
105-
return
106-
}
107-
108-
content := &cert.Content{
109-
SSLCertificatePath: json.SSLCertificatePath,
110-
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
111-
SSLCertificate: json.SSLCertificate,
112-
SSLCertificateKey: json.SSLCertificateKey,
113-
}
114-
115-
err = content.WriteFile()
116-
117-
if err != nil {
118-
api.ErrHandler(c, err)
119-
return
120-
}
121-
122-
c.JSON(http.StatusOK, Transformer(certModel))
89+
var json certJson
90+
if !api.BindAndValid(c, &json) {
91+
return
92+
}
93+
certModel := &model.Cert{
94+
Name: json.Name,
95+
SSLCertificatePath: json.SSLCertificatePath,
96+
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
97+
ChallengeMethod: json.ChallengeMethod,
98+
DnsCredentialID: json.DnsCredentialID,
99+
}
100+
101+
err := certModel.Insert()
102+
103+
if err != nil {
104+
api.ErrHandler(c, err)
105+
return
106+
}
107+
108+
content := &cert.Content{
109+
SSLCertificatePath: json.SSLCertificatePath,
110+
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
111+
SSLCertificate: json.SSLCertificate,
112+
SSLCertificateKey: json.SSLCertificateKey,
113+
}
114+
115+
err = content.WriteFile()
116+
117+
if err != nil {
118+
api.ErrHandler(c, err)
119+
return
120+
}
121+
122+
c.JSON(http.StatusOK, Transformer(certModel))
123123
}
124124

125125
func ModifyCert(c *gin.Context) {
126-
id := cast.ToInt(c.Param("id"))
127-
128-
var json certJson
129-
130-
if !api.BindAndValid(c, &json) {
131-
return
132-
}
133-
134-
q := query.Cert
135-
136-
certModel, err := q.FirstByID(id)
137-
if err != nil {
138-
api.ErrHandler(c, err)
139-
return
140-
}
141-
142-
err = certModel.Updates(&model.Cert{
143-
Name: json.Name,
144-
SSLCertificatePath: json.SSLCertificatePath,
145-
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
146-
ChallengeMethod: json.ChallengeMethod,
147-
DnsCredentialID: json.DnsCredentialID,
148-
})
149-
150-
if err != nil {
151-
api.ErrHandler(c, err)
152-
return
153-
}
154-
155-
content := &cert.Content{
156-
SSLCertificatePath: json.SSLCertificatePath,
157-
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
158-
SSLCertificate: json.SSLCertificate,
159-
SSLCertificateKey: json.SSLCertificateKey,
160-
}
161-
162-
err = content.WriteFile()
163-
164-
if err != nil {
165-
api.ErrHandler(c, err)
166-
return
167-
}
168-
169-
GetCert(c)
126+
id := cast.ToInt(c.Param("id"))
127+
128+
var json certJson
129+
130+
if !api.BindAndValid(c, &json) {
131+
return
132+
}
133+
134+
q := query.Cert
135+
136+
certModel, err := q.FirstByID(id)
137+
if err != nil {
138+
api.ErrHandler(c, err)
139+
return
140+
}
141+
142+
err = certModel.Updates(&model.Cert{
143+
Name: json.Name,
144+
SSLCertificatePath: json.SSLCertificatePath,
145+
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
146+
ChallengeMethod: json.ChallengeMethod,
147+
DnsCredentialID: json.DnsCredentialID,
148+
})
149+
150+
if err != nil {
151+
api.ErrHandler(c, err)
152+
return
153+
}
154+
155+
content := &cert.Content{
156+
SSLCertificatePath: json.SSLCertificatePath,
157+
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
158+
SSLCertificate: json.SSLCertificate,
159+
SSLCertificateKey: json.SSLCertificateKey,
160+
}
161+
162+
err = content.WriteFile()
163+
164+
if err != nil {
165+
api.ErrHandler(c, err)
166+
return
167+
}
168+
169+
GetCert(c)
170170
}
171171

172172
func RemoveCert(c *gin.Context) {
173-
cosy.Core[model.Cert](c).Destroy()
173+
cosy.Core[model.Cert](c).Destroy()
174174
}

app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nginx-ui-app-next",
3-
"version": "2.0.0-beta.11",
3+
"version": "2.0.0-beta.12",
44
"type": "module",
55
"scripts": {
66
"dev": "vite",

0 commit comments

Comments
 (0)