Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: backup and restore #935

Merged
merged 7 commits into from
Mar 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,14 @@
package api

import (
"errors"
"github.com/0xJacky/Nginx-UI/model"
"github.com/gin-gonic/gin"
"github.com/uozi-tech/cosy"
"github.com/uozi-tech/cosy/logger"
"gorm.io/gorm"
"net/http"
)

func CurrentUser(c *gin.Context) *model.User {
return c.MustGet("user").(*model.User)
}

func ErrHandler(c *gin.Context, err error) {
logger.GetLogger().Errorln(err)
var cErr *cosy.Error
switch {
case errors.Is(err, gorm.ErrRecordNotFound):
c.JSON(http.StatusNotFound, &cosy.Error{
Code: http.StatusNotFound,
Message: gorm.ErrRecordNotFound.Error(),
})
case errors.As(err, &cErr):
c.JSON(http.StatusInternalServerError, cErr)
default:
c.JSON(http.StatusInternalServerError, &cosy.Error{
Code: http.StatusInternalServerError,
Message: err.Error(),
})
}
}

func SetSSEHeaders(c *gin.Context) {
c.Header("Content-Type", "text/event-stream")
c.Header("Cache-Control", "no-cache")
Expand Down
12 changes: 6 additions & 6 deletions api/certificate/acme_user.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package certificate

import (
"github.com/0xJacky/Nginx-UI/api"
"net/http"

"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/0xJacky/Nginx-UI/settings"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"github.com/uozi-tech/cosy"
"net/http"
)

func GetAcmeUser(c *gin.Context) {
u := query.AcmeUser
id := cast.ToUint64(c.Param("id"))
user, err := u.FirstByID(id)
if err != nil {
api.ErrHandler(c, err)
cosy.ErrHandler(c, err)
return
}
c.JSON(http.StatusOK, user)
Expand Down Expand Up @@ -83,17 +83,17 @@ func RegisterAcmeUser(c *gin.Context) {
u := query.AcmeUser
user, err := u.FirstByID(id)
if err != nil {
api.ErrHandler(c, err)
cosy.ErrHandler(c, err)
return
}
err = user.Register()
if err != nil {
api.ErrHandler(c, err)
cosy.ErrHandler(c, err)
return
}
_, err = u.Where(u.ID.Eq(id)).Updates(user)
if err != nil {
api.ErrHandler(c, err)
cosy.ErrHandler(c, err)
return
}
c.JSON(http.StatusOK, user)
Expand Down
22 changes: 11 additions & 11 deletions api/certificate/certificate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package certificate

import (
"github.com/0xJacky/Nginx-UI/api"
"net/http"
"os"

"github.com/0xJacky/Nginx-UI/internal/cert"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/nginx"
Expand All @@ -12,8 +14,6 @@ import (
"github.com/go-acme/lego/v4/certcrypto"
"github.com/spf13/cast"
"github.com/uozi-tech/cosy"
"net/http"
"os"
)

type APICertificate struct {
Expand Down Expand Up @@ -74,7 +74,7 @@ func GetCert(c *gin.Context) {
certModel, err := q.FirstByID(cast.ToUint64(c.Param("id")))

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

Expand Down Expand Up @@ -114,7 +114,7 @@ func AddCert(c *gin.Context) {

err := certModel.Insert()
if err != nil {
api.ErrHandler(c, err)
cosy.ErrHandler(c, err)
return
}

Expand All @@ -127,7 +127,7 @@ func AddCert(c *gin.Context) {

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

Expand All @@ -153,7 +153,7 @@ func ModifyCert(c *gin.Context) {

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

Expand All @@ -169,7 +169,7 @@ func ModifyCert(c *gin.Context) {
})

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

Expand All @@ -182,7 +182,7 @@ func ModifyCert(c *gin.Context) {

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

Expand Down Expand Up @@ -218,7 +218,7 @@ func SyncCertificate(c *gin.Context) {

err := db.Where(certModel).FirstOrCreate(certModel).Error
if err != nil {
api.ErrHandler(c, err)
cosy.ErrHandler(c, err)
return
}

Expand All @@ -231,7 +231,7 @@ func SyncCertificate(c *gin.Context) {

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

Expand Down
12 changes: 6 additions & 6 deletions api/certificate/dns_credential.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package certificate

import (
"github.com/0xJacky/Nginx-UI/api"
"net/http"

"github.com/0xJacky/Nginx-UI/internal/cert/dns"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"github.com/uozi-tech/cosy"
"net/http"
)

func GetDnsCredential(c *gin.Context) {
Expand All @@ -18,7 +18,7 @@ func GetDnsCredential(c *gin.Context) {

dnsCredential, err := d.FirstByID(id)
if err != nil {
api.ErrHandler(c, err)
cosy.ErrHandler(c, err)
return
}
type apiDnsCredential struct {
Expand Down Expand Up @@ -62,7 +62,7 @@ func AddDnsCredential(c *gin.Context) {

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

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

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions api/cluster/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func GetEnvironment(c *gin.Context) {

env, err := envQuery.FirstByID(id)
if err != nil {
api.ErrHandler(c, err)
cosy.ErrHandler(c, err)
return
}

Expand Down Expand Up @@ -161,7 +161,7 @@ func DeleteEnvironment(c *gin.Context) {
func LoadEnvironmentFromSettings(c *gin.Context) {
err := settings.ReloadCluster()
if err != nil {
api.ErrHandler(c, err)
cosy.ErrHandler(c, err)
return
}

Expand Down
4 changes: 2 additions & 2 deletions api/cluster/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package cluster
import (
"net/http"

"github.com/0xJacky/Nginx-UI/api"
analytic2 "github.com/0xJacky/Nginx-UI/internal/analytic"
"github.com/0xJacky/Nginx-UI/internal/upgrader"
"github.com/0xJacky/Nginx-UI/internal/version"
"github.com/dustin/go-humanize"
"github.com/gin-gonic/gin"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/disk"
"github.com/uozi-tech/cosy"
)

func GetCurrentNode(c *gin.Context) {
Expand All @@ -23,7 +23,7 @@ func GetCurrentNode(c *gin.Context) {

runtimeInfo, err := upgrader.GetRuntimeInfo()
if err != nil {
api.ErrHandler(c, err)
cosy.ErrHandler(c, err)
return
}
cpuInfo, _ := cpu.Info()
Expand Down
20 changes: 10 additions & 10 deletions api/config/add.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package config

import (
"github.com/0xJacky/Nginx-UI/api"
"net/http"
"os"
"path/filepath"
"time"

"github.com/0xJacky/Nginx-UI/internal/config"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/nginx"
Expand All @@ -10,10 +14,6 @@ import (
"github.com/gin-gonic/gin"
"github.com/sashabaranov/go-openai"
"github.com/uozi-tech/cosy"
"net/http"
"os"
"path/filepath"
"time"
)

func AddConfig(c *gin.Context) {
Expand Down Expand Up @@ -48,14 +48,14 @@ func AddConfig(c *gin.Context) {
if !helper.FileExists(dir) {
err := os.MkdirAll(dir, 0755)
if err != nil {
api.ErrHandler(c, err)
cosy.ErrHandler(c, err)
return
}
}

err := os.WriteFile(path, []byte(content), 0644)
if err != nil {
api.ErrHandler(c, err)
cosy.ErrHandler(c, err)
return
}

Expand All @@ -70,7 +70,7 @@ func AddConfig(c *gin.Context) {
q := query.Config
_, err = q.Where(q.Filepath.Eq(path)).Delete()
if err != nil {
api.ErrHandler(c, err)
cosy.ErrHandler(c, err)
return
}

Expand All @@ -83,13 +83,13 @@ func AddConfig(c *gin.Context) {

err = q.Create(cfg)
if err != nil {
api.ErrHandler(c, err)
cosy.ErrHandler(c, err)
return
}

err = config.SyncToRemoteServer(cfg)
if err != nil {
api.ErrHandler(c, err)
cosy.ErrHandler(c, err)
return
}

Expand Down
17 changes: 9 additions & 8 deletions api/config/get.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package config

import (
"github.com/0xJacky/Nginx-UI/api"
"net/http"
"os"
"path/filepath"

"github.com/0xJacky/Nginx-UI/internal/config"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin"
"github.com/sashabaranov/go-openai"
"net/http"
"os"
"path/filepath"
"github.com/uozi-tech/cosy"
)

type APIConfigResp struct {
Expand All @@ -32,20 +33,20 @@ func GetConfig(c *gin.Context) {

stat, err := os.Stat(absPath)
if err != nil {
api.ErrHandler(c, err)
cosy.ErrHandler(c, err)
return
}

content, err := os.ReadFile(absPath)
if err != nil {
api.ErrHandler(c, err)
cosy.ErrHandler(c, err)
return
}
q := query.Config
g := query.ChatGPTLog
chatgpt, err := g.Where(g.Name.Eq(absPath)).FirstOrCreate()
if err != nil {
api.ErrHandler(c, err)
cosy.ErrHandler(c, err)
return
}

Expand All @@ -55,7 +56,7 @@ func GetConfig(c *gin.Context) {

cfg, err := q.Where(q.Filepath.Eq(absPath)).FirstOrInit()
if err != nil {
api.ErrHandler(c, err)
cosy.ErrHandler(c, err)
return
}

Expand Down
Loading
Loading