Skip to content

Commit

Permalink
refactor: structure of api-router directory
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJacky committed Nov 27, 2023
1 parent d272f79 commit 50b4fbc
Show file tree
Hide file tree
Showing 38 changed files with 620 additions and 534 deletions.
8 changes: 4 additions & 4 deletions api/analytic.go → api/analytic/analytic.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package api
package analytic

import (
"fmt"
Expand Down Expand Up @@ -26,9 +26,9 @@ type CPUStat struct {

type Stat struct {
Uptime uint64 `json:"uptime"`
LoadAvg *load.AvgStat `json:"loadavg"`
CPU CPUStat `json:"cpu"`
Memory analytic2.MemStat `json:"memory"`
LoadAvg *load.AvgStat `json:"loadavg"`
CPU CPUStat `json:"cpu"`
Memory analytic2.MemStat `json:"memory"`
Disk analytic2.DiskStat `json:"disk"`
Network net.IOCountersStat `json:"network"`
}
Expand Down
15 changes: 15 additions & 0 deletions api/analytic/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package analytic

import (
"github.com/gin-gonic/gin"
)

func InitWebSocketRouter(r *gin.RouterGroup) {
r.GET("analytic", Analytic)
r.GET("analytic/intro", GetNodeStat)
r.GET("analytic/nodes", GetNodesAnalytic)
}

func InitRouter(r *gin.RouterGroup) {
r.GET("analytic/init", GetAnalyticInit)
}
4 changes: 3 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

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

verrs, ok := err.(val.ValidationErrors)
var verrs val.ValidationErrors
ok := errors.As(err, &verrs)

if !ok {
logger.Error("valid err", verrs)
Expand Down
24 changes: 0 additions & 24 deletions api/backup.go

This file was deleted.

48 changes: 25 additions & 23 deletions api/cert.go → api/certificate/cert.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package api
package certificate

import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/api/sites"
"github.com/0xJacky/Nginx-UI/internal/cert"
"github.com/0xJacky/Nginx-UI/internal/cert/dns"
"github.com/0xJacky/Nginx-UI/internal/logger"
Expand Down Expand Up @@ -157,13 +159,13 @@ func GetCertList(c *gin.Context) {
func getCert(c *gin.Context, certModel *model.Cert) {
type resp struct {
*model.Cert
SSLCertification string `json:"ssl_certification"`
SSLCertificationKey string `json:"ssl_certification_key"`
CertificateInfo *CertificateInfo `json:"certificate_info,omitempty"`
SSLCertification string `json:"ssl_certification"`
SSLCertificationKey string `json:"ssl_certification_key"`
CertificateInfo *sites.CertificateInfo `json:"certificate_info,omitempty"`
}

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

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

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

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

Expand All @@ -217,7 +219,7 @@ func AddCert(c *gin.Context) {
SSLCertification string `json:"ssl_certification"`
SSLCertificationKey string `json:"ssl_certification_key"`
}
if !BindAndValid(c, &json) {
if !api.BindAndValid(c, &json) {
return
}
certModel := &model.Cert{
Expand All @@ -229,34 +231,34 @@ func AddCert(c *gin.Context) {
err := certModel.Insert()

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

err = os.MkdirAll(filepath.Dir(json.SSLCertificatePath), 0644)
if err != nil {
ErrHandler(c, err)
api.ErrHandler(c, err)
return
}

err = os.MkdirAll(filepath.Dir(json.SSLCertificateKeyPath), 0644)
if err != nil {
ErrHandler(c, err)
api.ErrHandler(c, err)
return
}

if json.SSLCertification != "" {
err = os.WriteFile(json.SSLCertificatePath, []byte(json.SSLCertification), 0644)
if err != nil {
ErrHandler(c, err)
api.ErrHandler(c, err)
return
}
}

if json.SSLCertificationKey != "" {
err = os.WriteFile(json.SSLCertificateKeyPath, []byte(json.SSLCertificationKey), 0644)
if err != nil {
ErrHandler(c, err)
api.ErrHandler(c, err)
return
}
}
Expand All @@ -275,13 +277,13 @@ func ModifyCert(c *gin.Context) {
SSLCertificationKey string `json:"ssl_certification_key"`
}

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

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

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

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

err = os.MkdirAll(filepath.Dir(json.SSLCertificatePath), 0644)
if err != nil {
ErrHandler(c, err)
api.ErrHandler(c, err)
return
}

err = os.MkdirAll(filepath.Dir(json.SSLCertificateKeyPath), 0644)
if err != nil {
ErrHandler(c, err)
api.ErrHandler(c, err)
return
}

if json.SSLCertification != "" {
err = os.WriteFile(json.SSLCertificatePath, []byte(json.SSLCertification), 0644)
if err != nil {
ErrHandler(c, err)
api.ErrHandler(c, err)
return
}
}

if json.SSLCertificationKey != "" {
err = os.WriteFile(json.SSLCertificateKeyPath, []byte(json.SSLCertificationKey), 0644)
if err != nil {
ErrHandler(c, err)
api.ErrHandler(c, err)
return
}
}
Expand All @@ -332,14 +334,14 @@ func RemoveCert(c *gin.Context) {
certModel, err := model.FirstCertByID(id)

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

err = certModel.Remove()

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

Expand Down
21 changes: 11 additions & 10 deletions api/dns_credential.go → api/certificate/dns_credential.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api
package certificate

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

dnsCredential, err := d.FirstByID(id)
if err != nil {
ErrHandler(c, err)
api.ErrHandler(c, err)
return
}
type apiDnsCredential struct {
Expand All @@ -43,7 +44,7 @@ func GetDnsCredentialList(c *gin.Context) {
}

if err != nil {
ErrHandler(c, err)
api.ErrHandler(c, err)
return
}
c.JSON(http.StatusOK, gin.H{
Expand All @@ -59,7 +60,7 @@ type DnsCredentialManageJson struct {

func AddDnsCredential(c *gin.Context) {
var json DnsCredentialManageJson
if !BindAndValid(c, &json) {
if !api.BindAndValid(c, &json) {
return
}

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

err := d.Create(&dnsCredential)
if err != nil {
ErrHandler(c, err)
api.ErrHandler(c, err)
return
}

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

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

d := query.DnsCredential

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

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

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

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

dnsCredential, err := d.FirstByID(id)
if err != nil {
ErrHandler(c, err)
api.ErrHandler(c, err)
return
}
err = d.DeleteByID(dnsCredential.ID)
if err != nil {
ErrHandler(c, err)
api.ErrHandler(c, err)
return
}
c.JSON(http.StatusNoContent, nil)
Expand Down
22 changes: 22 additions & 0 deletions api/certificate/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package certificate

import "github.com/gin-gonic/gin"

func InitDNSCredentialRouter(r *gin.RouterGroup) {
r.GET("dns_credentials", GetDnsCredentialList)
r.GET("dns_credential/:id", GetDnsCredential)
r.POST("dns_credential", AddDnsCredential)
r.POST("dns_credential/:id", EditDnsCredential)
r.DELETE("dns_credential/:id", DeleteDnsCredential)
}

func InitCertificateRouter(r *gin.RouterGroup) {
r.GET("domain/:name/cert", IssueCert)
r.GET("certs", GetCertList)
r.GET("cert/:id", GetCert)
r.POST("cert", AddCert)
r.POST("cert/:id", ModifyCert)
r.DELETE("cert/:id", RemoveCert)
r.GET("auto_cert/dns/providers", GetDNSProvidersList)
r.GET("auto_cert/dns/provider/:code", GetDNSProvider)
}
Loading

0 comments on commit 50b4fbc

Please sign in to comment.