Skip to content
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
5 changes: 2 additions & 3 deletions agent/app/api/v2/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/1Panel-dev/1Panel/agent/app/api/v2/helper"
"github.com/1Panel-dev/1Panel/agent/app/dto"
"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -35,7 +34,7 @@ func (b *BaseApi) LoadDashboardOsInfo(c *gin.Context) {
func (b *BaseApi) LoadAppLauncher(c *gin.Context) {
data, err := dashboardService.LoadAppLauncher()
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
helper.InternalServer(c, err)
return
}
helper.SuccessWithData(c, data)
Expand All @@ -56,7 +55,7 @@ func (b *BaseApi) LoadAppLauncherOption(c *gin.Context) {
}
data, err := dashboardService.ListLauncherOption(req.Filter)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
helper.InternalServer(c, err)
return
}
helper.SuccessWithData(c, data)
Expand Down
18 changes: 9 additions & 9 deletions agent/app/api/v2/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func (b *BaseApi) UploadFiles(c *gin.Context) {
}

if len(paths) == 0 || !strings.Contains(paths[0], "/") {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, errors.New("error paths in request"))
helper.BadRequest(c, errors.New("error paths in request"))
return
}
dir := path.Dir(paths[0])
Expand All @@ -327,7 +327,7 @@ func (b *BaseApi) UploadFiles(c *gin.Context) {
return
}
if err = os.MkdirAll(dir, mode); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, fmt.Errorf("mkdir %s failed, err: %v", dir, err))
helper.BadRequest(c, fmt.Errorf("mkdir %s failed, err: %v", dir, err))
return
}
}
Expand Down Expand Up @@ -390,7 +390,7 @@ func (b *BaseApi) UploadFiles(c *gin.Context) {
success++
}
if success == 0 {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, failures)
helper.InternalServer(c, failures)
} else {
helper.SuccessWithMsg(c, fmt.Sprintf("%d files upload success", success))
}
Expand Down Expand Up @@ -518,7 +518,7 @@ func (b *BaseApi) DownloadChunkFiles(c *gin.Context) {
}
fileOp := files.NewFileOp()
if !fileOp.Stat(req.Path) {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrPathNotFound, nil)
helper.ErrorWithDetail(c, http.StatusInternalServerError, "ErrPathNotFound", nil)
return
}
filePath := req.Path
Expand All @@ -533,7 +533,7 @@ func (b *BaseApi) DownloadChunkFiles(c *gin.Context) {
return
}
if info.IsDir() {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrFileDownloadDir, err)
helper.ErrorWithDetail(c, http.StatusInternalServerError, "ErrFileDownloadDir", err)
return
}

Expand Down Expand Up @@ -679,7 +679,7 @@ func (b *BaseApi) UploadChunkFiles(c *gin.Context) {
return
}
fileOp := files.NewFileOp()
tmpDir := path.Join(global.CONF.System.TmpDir, "upload")
tmpDir := path.Join(global.Dir.TmpDir, "upload")
if !fileOp.Stat(tmpDir) {
if err := fileOp.CreateDir(tmpDir, constant.DirPerm); err != nil {
helper.BadRequest(c, err)
Expand Down Expand Up @@ -715,14 +715,14 @@ func (b *BaseApi) UploadChunkFiles(c *gin.Context) {

chunkData, err = io.ReadAll(uploadFile)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, buserr.WithMap(constant.ErrFileUpload, map[string]interface{}{"name": filename, "detail": err.Error()}, err))
helper.InternalServer(c, buserr.WithMap("ErrFileUpload", map[string]interface{}{"name": filename, "detail": err.Error()}, err))
return
}

chunkPath := filepath.Join(fileDir, fmt.Sprintf("%s.%d", filename, chunkIndex))
err = os.WriteFile(chunkPath, chunkData, constant.DirPerm)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, buserr.WithMap(constant.ErrFileUpload, map[string]interface{}{"name": filename, "detail": err.Error()}, err))
helper.InternalServer(c, buserr.WithMap("ErrFileUpload", map[string]interface{}{"name": filename, "detail": err.Error()}, err))
return
}

Expand All @@ -733,7 +733,7 @@ func (b *BaseApi) UploadChunkFiles(c *gin.Context) {
}
err = mergeChunks(filename, fileDir, c.PostForm("path"), chunkCount, overwrite)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, buserr.WithMap(constant.ErrFileUpload, map[string]interface{}{"name": filename, "detail": err.Error()}, err))
helper.InternalServer(c, buserr.WithMap("ErrFileUpload", map[string]interface{}{"name": filename, "detail": err.Error()}, err))
return
}
helper.SuccessWithData(c, true)
Expand Down
15 changes: 7 additions & 8 deletions agent/app/api/v2/ftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/1Panel-dev/1Panel/agent/app/api/v2/helper"
"github.com/1Panel-dev/1Panel/agent/app/dto"
"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -41,7 +40,7 @@ func (b *BaseApi) LoadFtpLogInfo(c *gin.Context) {

total, list, err := ftpService.LoadLog(req)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
helper.InternalServer(c, err)
return
}

Expand All @@ -66,7 +65,7 @@ func (b *BaseApi) OperateFtp(c *gin.Context) {
}

if err := ftpService.Operate(req.Operation); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
helper.InternalServer(c, err)
return
}

Expand All @@ -89,7 +88,7 @@ func (b *BaseApi) SearchFtp(c *gin.Context) {

total, list, err := ftpService.SearchWithPage(req)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
helper.InternalServer(c, err)
return
}

Expand Down Expand Up @@ -123,7 +122,7 @@ func (b *BaseApi) CreateFtp(c *gin.Context) {
req.Password = string(pass)
}
if _, err := ftpService.Create(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
helper.InternalServer(c, err)
return
}
helper.SuccessWithOutData(c)
Expand All @@ -145,7 +144,7 @@ func (b *BaseApi) DeleteFtp(c *gin.Context) {
}

if err := ftpService.Delete(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
helper.InternalServer(c, err)
return
}
helper.SuccessWithOutData(c)
Expand All @@ -162,7 +161,7 @@ func (b *BaseApi) DeleteFtp(c *gin.Context) {
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"同步 FTP 账户","formatEN":"sync FTP users"}
func (b *BaseApi) SyncFtp(c *gin.Context) {
if err := ftpService.Sync(); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
helper.InternalServer(c, err)
return
}
helper.SuccessWithOutData(c)
Expand Down Expand Up @@ -192,7 +191,7 @@ func (b *BaseApi) UpdateFtp(c *gin.Context) {
req.Password = string(pass)
}
if err := ftpService.Update(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
helper.InternalServer(c, err)
return
}
helper.SuccessWithOutData(c)
Expand Down
36 changes: 9 additions & 27 deletions agent/app/api/v2/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"gorm.io/gorm"

"github.com/1Panel-dev/1Panel/agent/app/dto"
"github.com/1Panel-dev/1Panel/agent/buserr"
"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/i18n"
"github.com/gin-gonic/gin"
Expand All @@ -22,42 +21,25 @@ func ErrorWithDetail(ctx *gin.Context, code int, msgKey string, err error) {
Code: code,
Message: "",
}
if msgKey == constant.ErrTypeInternalServer {
switch {
case errors.Is(err, constant.ErrRecordExist):
res.Message = i18n.GetMsgWithMap("ErrRecordExist", nil)
case errors.Is(constant.ErrRecordNotFound, err):
res.Message = i18n.GetMsgWithMap("ErrRecordNotFound", nil)
case errors.Is(constant.ErrInvalidParams, err):
res.Message = i18n.GetMsgWithMap("ErrInvalidParams", nil)
case errors.Is(constant.ErrStructTransform, err):
res.Message = i18n.GetMsgWithMap("ErrStructTransform", map[string]interface{}{"detail": err})
case errors.As(err, &buserr.BusinessError{}):
res.Message = err.Error()
default:
res.Message = i18n.GetMsgWithDetail(msgKey, err.Error())
}
} else {
res.Message = i18n.GetMsgWithDetail(msgKey, err.Error())
}
res.Message = i18n.GetMsgWithDetail(msgKey, err.Error())
ctx.JSON(http.StatusOK, res)
ctx.Abort()
}

func InternalServer(ctx *gin.Context, err error) {
ErrorWithDetail(ctx, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
ErrorWithDetail(ctx, http.StatusInternalServerError, "ErrInternalServer", err)
}

func BadRequest(ctx *gin.Context, err error) {
ErrorWithDetail(ctx, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
ErrorWithDetail(ctx, http.StatusBadRequest, "ErrInvalidParams", err)
}

func SuccessWithData(ctx *gin.Context, data interface{}) {
if data == nil {
data = gin.H{}
}
res := dto.Response{
Code: constant.CodeSuccess,
Code: http.StatusOK,
Data: data,
}
ctx.JSON(http.StatusOK, res)
Expand All @@ -66,7 +48,7 @@ func SuccessWithData(ctx *gin.Context, data interface{}) {

func SuccessWithOutData(ctx *gin.Context) {
res := dto.Response{
Code: constant.CodeSuccess,
Code: http.StatusOK,
Message: "success",
}
ctx.JSON(http.StatusOK, res)
Expand All @@ -75,7 +57,7 @@ func SuccessWithOutData(ctx *gin.Context) {

func SuccessWithMsg(ctx *gin.Context, msg string) {
res := dto.Response{
Code: constant.CodeSuccess,
Code: http.StatusOK,
Message: msg,
}
ctx.JSON(http.StatusOK, res)
Expand Down Expand Up @@ -116,19 +98,19 @@ func GetTxAndContext() (tx *gorm.DB, ctx context.Context) {

func CheckBindAndValidate(req interface{}, c *gin.Context) error {
if err := c.ShouldBindJSON(req); err != nil {
ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
ErrorWithDetail(c, http.StatusBadRequest, "ErrInvalidParams", err)
return err
}
if err := global.VALID.Struct(req); err != nil {
ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
ErrorWithDetail(c, http.StatusBadRequest, "ErrInvalidParams", err)
return err
}
return nil
}

func CheckBind(req interface{}, c *gin.Context) error {
if err := c.ShouldBindJSON(&req); err != nil {
ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
ErrorWithDetail(c, http.StatusBadRequest, "ErrInvalidParams", err)
return err
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion agent/app/api/v2/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (b *BaseApi) UpdateSetting(c *gin.Context) {
// @Security Timestamp
// @Router /settings/basedir [get]
func (b *BaseApi) LoadBaseDir(c *gin.Context) {
helper.SuccessWithData(c, global.CONF.System.DataDir)
helper.SuccessWithData(c, global.Dir.DataDir)
}

func (b *BaseApi) ReloadConn(c *gin.Context) {
Expand Down
4 changes: 2 additions & 2 deletions agent/app/api/v2/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (b *BaseApi) RedisWsSsh(c *gin.Context) {
}
defer wsConn.Close()

if global.CONF.System.IsDemo {
if global.CONF.Base.IsDemo {
if wshandleError(wsConn, errors.New(" demo server, prohibit this operation!")) {
return
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func (b *BaseApi) ContainerWsSsh(c *gin.Context) {
}
defer wsConn.Close()

if global.CONF.System.IsDemo {
if global.CONF.Base.IsDemo {
if wshandleError(wsConn, errors.New(" demo server, prohibit this operation!")) {
return
}
Expand Down
5 changes: 3 additions & 2 deletions agent/app/model/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/global"
)

type App struct {
Expand Down Expand Up @@ -43,7 +44,7 @@ func (i *App) IsLocalApp() bool {
func (i *App) GetAppResourcePath() string {
if i.IsLocalApp() {
//这里要去掉本地应用的local前缀
return filepath.Join(constant.LocalAppResourceDir, strings.TrimPrefix(i.Key, "local"))
return filepath.Join(global.Dir.LocalAppResourceDir, strings.TrimPrefix(i.Key, "local"))
}
return filepath.Join(constant.RemoteAppResourceDir, i.Key)
return filepath.Join(global.Dir.RemoteAppResourceDir, i.Key)
}
5 changes: 3 additions & 2 deletions agent/app/model/app_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/global"
)

type AppInstall struct {
Expand Down Expand Up @@ -41,8 +42,8 @@ func (i *AppInstall) GetEnvPath() string {

func (i *AppInstall) GetAppPath() string {
if i.App.Resource == constant.AppResourceLocal {
return path.Join(constant.LocalAppInstallDir, strings.TrimPrefix(i.App.Key, constant.AppResourceLocal))
return path.Join(global.Dir.LocalAppInstallDir, strings.TrimPrefix(i.App.Key, constant.AppResourceLocal))
} else {
return path.Join(constant.AppInstallDir, i.App.Key)
return path.Join(global.Dir.AppInstallDir, i.App.Key)
}
}
8 changes: 4 additions & 4 deletions agent/app/model/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package model
import (
"path"

"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/global"
)

type Runtime struct {
Expand Down Expand Up @@ -34,15 +34,15 @@ func (r *Runtime) GetEnvPath() string {
}

func (r *Runtime) GetPath() string {
return path.Join(constant.RuntimeDir, r.Type, r.Name)
return path.Join(global.Dir.RuntimeDir, r.Type, r.Name)
}

func (r *Runtime) GetFPMPath() string {
return path.Join(constant.RuntimeDir, r.Type, r.Name, "conf", "php-fpm.conf")
return path.Join(global.Dir.RuntimeDir, r.Type, r.Name, "conf", "php-fpm.conf")
}

func (r *Runtime) GetPHPPath() string {
return path.Join(constant.RuntimeDir, r.Type, r.Name, "conf", "php.ini")
return path.Join(global.Dir.RuntimeDir, r.Type, r.Name, "conf", "php.ini")
}

func (r *Runtime) GetLogPath() string {
Expand Down
4 changes: 2 additions & 2 deletions agent/app/model/website_ssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"path"
"time"

"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/global"
)

type WebsiteSSL struct {
Expand Down Expand Up @@ -47,5 +47,5 @@ func (w WebsiteSSL) TableName() string {
}

func (w WebsiteSSL) GetLogPath() string {
return path.Join(constant.SSLLogDir, fmt.Sprintf("%s-ssl-%d.log", w.PrimaryDomain, w.ID))
return path.Join(global.Dir.SSLLogDir, fmt.Sprintf("%s-ssl-%d.log", w.PrimaryDomain, w.ID))
}
Loading
Loading