Skip to content

Commit 50b4fbc

Browse files
committed
refactor: structure of api-router directory
1 parent d272f79 commit 50b4fbc

38 files changed

+620
-534
lines changed

api/analytic.go renamed to api/analytic/analytic.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package api
1+
package analytic
22

33
import (
44
"fmt"
@@ -26,9 +26,9 @@ type CPUStat struct {
2626

2727
type Stat struct {
2828
Uptime uint64 `json:"uptime"`
29-
LoadAvg *load.AvgStat `json:"loadavg"`
30-
CPU CPUStat `json:"cpu"`
31-
Memory analytic2.MemStat `json:"memory"`
29+
LoadAvg *load.AvgStat `json:"loadavg"`
30+
CPU CPUStat `json:"cpu"`
31+
Memory analytic2.MemStat `json:"memory"`
3232
Disk analytic2.DiskStat `json:"disk"`
3333
Network net.IOCountersStat `json:"network"`
3434
}

api/analytic/router.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package analytic
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
)
6+
7+
func InitWebSocketRouter(r *gin.RouterGroup) {
8+
r.GET("analytic", Analytic)
9+
r.GET("analytic/intro", GetNodeStat)
10+
r.GET("analytic/nodes", GetNodesAnalytic)
11+
}
12+
13+
func InitRouter(r *gin.RouterGroup) {
14+
r.GET("analytic/init", GetAnalyticInit)
15+
}

api/api.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package api
22

33
import (
4+
"errors"
45
"github.com/0xJacky/Nginx-UI/internal/logger"
56
"github.com/gin-gonic/gin"
67
val "github.com/go-playground/validator/v10"
@@ -26,7 +27,8 @@ func BindAndValid(c *gin.Context, target interface{}) bool {
2627
if err != nil {
2728
logger.Error("bind err", err)
2829

29-
verrs, ok := err.(val.ValidationErrors)
30+
var verrs val.ValidationErrors
31+
ok := errors.As(err, &verrs)
3032

3133
if !ok {
3234
logger.Error("valid err", verrs)

api/backup.go

Lines changed: 0 additions & 24 deletions
This file was deleted.

api/cert.go renamed to api/certificate/cert.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
package api
1+
package certificate
22

33
import (
4+
"github.com/0xJacky/Nginx-UI/api"
5+
"github.com/0xJacky/Nginx-UI/api/sites"
46
"github.com/0xJacky/Nginx-UI/internal/cert"
57
"github.com/0xJacky/Nginx-UI/internal/cert/dns"
68
"github.com/0xJacky/Nginx-UI/internal/logger"
@@ -157,13 +159,13 @@ func GetCertList(c *gin.Context) {
157159
func getCert(c *gin.Context, certModel *model.Cert) {
158160
type resp struct {
159161
*model.Cert
160-
SSLCertification string `json:"ssl_certification"`
161-
SSLCertificationKey string `json:"ssl_certification_key"`
162-
CertificateInfo *CertificateInfo `json:"certificate_info,omitempty"`
162+
SSLCertification string `json:"ssl_certification"`
163+
SSLCertificationKey string `json:"ssl_certification_key"`
164+
CertificateInfo *sites.CertificateInfo `json:"certificate_info,omitempty"`
163165
}
164166

165167
var sslCertificationBytes, sslCertificationKeyBytes []byte
166-
var certificateInfo *CertificateInfo
168+
var certificateInfo *sites.CertificateInfo
167169
if certModel.SSLCertificatePath != "" {
168170
if _, err := os.Stat(certModel.SSLCertificatePath); err == nil {
169171
sslCertificationBytes, _ = os.ReadFile(certModel.SSLCertificatePath)
@@ -172,11 +174,11 @@ func getCert(c *gin.Context, certModel *model.Cert) {
172174
pubKey, err := cert.GetCertInfo(certModel.SSLCertificatePath)
173175

174176
if err != nil {
175-
ErrHandler(c, err)
177+
api.ErrHandler(c, err)
176178
return
177179
}
178180

179-
certificateInfo = &CertificateInfo{
181+
certificateInfo = &sites.CertificateInfo{
180182
SubjectName: pubKey.Subject.CommonName,
181183
IssuerName: pubKey.Issuer.CommonName,
182184
NotAfter: pubKey.NotAfter,
@@ -202,7 +204,7 @@ func GetCert(c *gin.Context) {
202204
certModel, err := model.FirstCertByID(cast.ToInt(c.Param("id")))
203205

204206
if err != nil {
205-
ErrHandler(c, err)
207+
api.ErrHandler(c, err)
206208
return
207209
}
208210

@@ -217,7 +219,7 @@ func AddCert(c *gin.Context) {
217219
SSLCertification string `json:"ssl_certification"`
218220
SSLCertificationKey string `json:"ssl_certification_key"`
219221
}
220-
if !BindAndValid(c, &json) {
222+
if !api.BindAndValid(c, &json) {
221223
return
222224
}
223225
certModel := &model.Cert{
@@ -229,34 +231,34 @@ func AddCert(c *gin.Context) {
229231
err := certModel.Insert()
230232

231233
if err != nil {
232-
ErrHandler(c, err)
234+
api.ErrHandler(c, err)
233235
return
234236
}
235237

236238
err = os.MkdirAll(filepath.Dir(json.SSLCertificatePath), 0644)
237239
if err != nil {
238-
ErrHandler(c, err)
240+
api.ErrHandler(c, err)
239241
return
240242
}
241243

242244
err = os.MkdirAll(filepath.Dir(json.SSLCertificateKeyPath), 0644)
243245
if err != nil {
244-
ErrHandler(c, err)
246+
api.ErrHandler(c, err)
245247
return
246248
}
247249

248250
if json.SSLCertification != "" {
249251
err = os.WriteFile(json.SSLCertificatePath, []byte(json.SSLCertification), 0644)
250252
if err != nil {
251-
ErrHandler(c, err)
253+
api.ErrHandler(c, err)
252254
return
253255
}
254256
}
255257

256258
if json.SSLCertificationKey != "" {
257259
err = os.WriteFile(json.SSLCertificateKeyPath, []byte(json.SSLCertificationKey), 0644)
258260
if err != nil {
259-
ErrHandler(c, err)
261+
api.ErrHandler(c, err)
260262
return
261263
}
262264
}
@@ -275,13 +277,13 @@ func ModifyCert(c *gin.Context) {
275277
SSLCertificationKey string `json:"ssl_certification_key"`
276278
}
277279

278-
if !BindAndValid(c, &json) {
280+
if !api.BindAndValid(c, &json) {
279281
return
280282
}
281283

282284
certModel, err := model.FirstCertByID(id)
283285
if err != nil {
284-
ErrHandler(c, err)
286+
api.ErrHandler(c, err)
285287
return
286288
}
287289

@@ -292,34 +294,34 @@ func ModifyCert(c *gin.Context) {
292294
})
293295

294296
if err != nil {
295-
ErrHandler(c, err)
297+
api.ErrHandler(c, err)
296298
return
297299
}
298300

299301
err = os.MkdirAll(filepath.Dir(json.SSLCertificatePath), 0644)
300302
if err != nil {
301-
ErrHandler(c, err)
303+
api.ErrHandler(c, err)
302304
return
303305
}
304306

305307
err = os.MkdirAll(filepath.Dir(json.SSLCertificateKeyPath), 0644)
306308
if err != nil {
307-
ErrHandler(c, err)
309+
api.ErrHandler(c, err)
308310
return
309311
}
310312

311313
if json.SSLCertification != "" {
312314
err = os.WriteFile(json.SSLCertificatePath, []byte(json.SSLCertification), 0644)
313315
if err != nil {
314-
ErrHandler(c, err)
316+
api.ErrHandler(c, err)
315317
return
316318
}
317319
}
318320

319321
if json.SSLCertificationKey != "" {
320322
err = os.WriteFile(json.SSLCertificateKeyPath, []byte(json.SSLCertificationKey), 0644)
321323
if err != nil {
322-
ErrHandler(c, err)
324+
api.ErrHandler(c, err)
323325
return
324326
}
325327
}
@@ -332,14 +334,14 @@ func RemoveCert(c *gin.Context) {
332334
certModel, err := model.FirstCertByID(id)
333335

334336
if err != nil {
335-
ErrHandler(c, err)
337+
api.ErrHandler(c, err)
336338
return
337339
}
338340

339341
err = certModel.Remove()
340342

341343
if err != nil {
342-
ErrHandler(c, err)
344+
api.ErrHandler(c, err)
343345
return
344346
}
345347

api/dns_credential.go renamed to api/certificate/dns_credential.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
package api
1+
package certificate
22

33
import (
4+
"github.com/0xJacky/Nginx-UI/api"
45
"github.com/0xJacky/Nginx-UI/internal/cert/dns"
56
model2 "github.com/0xJacky/Nginx-UI/model"
67
"github.com/0xJacky/Nginx-UI/query"
@@ -16,7 +17,7 @@ func GetDnsCredential(c *gin.Context) {
1617

1718
dnsCredential, err := d.FirstByID(id)
1819
if err != nil {
19-
ErrHandler(c, err)
20+
api.ErrHandler(c, err)
2021
return
2122
}
2223
type apiDnsCredential struct {
@@ -43,7 +44,7 @@ func GetDnsCredentialList(c *gin.Context) {
4344
}
4445

4546
if err != nil {
46-
ErrHandler(c, err)
47+
api.ErrHandler(c, err)
4748
return
4849
}
4950
c.JSON(http.StatusOK, gin.H{
@@ -59,7 +60,7 @@ type DnsCredentialManageJson struct {
5960

6061
func AddDnsCredential(c *gin.Context) {
6162
var json DnsCredentialManageJson
62-
if !BindAndValid(c, &json) {
63+
if !api.BindAndValid(c, &json) {
6364
return
6465
}
6566

@@ -74,7 +75,7 @@ func AddDnsCredential(c *gin.Context) {
7475

7576
err := d.Create(&dnsCredential)
7677
if err != nil {
77-
ErrHandler(c, err)
78+
api.ErrHandler(c, err)
7879
return
7980
}
8081

@@ -85,15 +86,15 @@ func EditDnsCredential(c *gin.Context) {
8586
id := cast.ToInt(c.Param("id"))
8687

8788
var json DnsCredentialManageJson
88-
if !BindAndValid(c, &json) {
89+
if !api.BindAndValid(c, &json) {
8990
return
9091
}
9192

9293
d := query.DnsCredential
9394

9495
dnsCredential, err := d.FirstByID(id)
9596
if err != nil {
96-
ErrHandler(c, err)
97+
api.ErrHandler(c, err)
9798
return
9899
}
99100

@@ -105,7 +106,7 @@ func EditDnsCredential(c *gin.Context) {
105106
})
106107

107108
if err != nil {
108-
ErrHandler(c, err)
109+
api.ErrHandler(c, err)
109110
return
110111
}
111112

@@ -118,12 +119,12 @@ func DeleteDnsCredential(c *gin.Context) {
118119

119120
dnsCredential, err := d.FirstByID(id)
120121
if err != nil {
121-
ErrHandler(c, err)
122+
api.ErrHandler(c, err)
122123
return
123124
}
124125
err = d.DeleteByID(dnsCredential.ID)
125126
if err != nil {
126-
ErrHandler(c, err)
127+
api.ErrHandler(c, err)
127128
return
128129
}
129130
c.JSON(http.StatusNoContent, nil)

api/certificate/router.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package certificate
2+
3+
import "github.com/gin-gonic/gin"
4+
5+
func InitDNSCredentialRouter(r *gin.RouterGroup) {
6+
r.GET("dns_credentials", GetDnsCredentialList)
7+
r.GET("dns_credential/:id", GetDnsCredential)
8+
r.POST("dns_credential", AddDnsCredential)
9+
r.POST("dns_credential/:id", EditDnsCredential)
10+
r.DELETE("dns_credential/:id", DeleteDnsCredential)
11+
}
12+
13+
func InitCertificateRouter(r *gin.RouterGroup) {
14+
r.GET("domain/:name/cert", IssueCert)
15+
r.GET("certs", GetCertList)
16+
r.GET("cert/:id", GetCert)
17+
r.POST("cert", AddCert)
18+
r.POST("cert/:id", ModifyCert)
19+
r.DELETE("cert/:id", RemoveCert)
20+
r.GET("auto_cert/dns/providers", GetDNSProvidersList)
21+
r.GET("auto_cert/dns/provider/:code", GetDNSProvider)
22+
}

0 commit comments

Comments
 (0)