diff --git a/backend/app/api/v1/app_install.go b/backend/app/api/v1/app_install.go index 334d378e0..e473d9f90 100644 --- a/backend/app/api/v1/app_install.go +++ b/backend/app/api/v1/app_install.go @@ -183,3 +183,19 @@ func (b *BaseApi) ChangeAppPort(c *gin.Context) { helper.SuccessWithData(c, nil) } + +func (b *BaseApi) GetDefaultConfig(c *gin.Context) { + key := c.Param("key") + if key == "" { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInternalServer, nil) + return + } + + content, err := appInstallService.GetDefaultConfigByKey(key) + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + + helper.SuccessWithData(c, content) +} diff --git a/backend/app/api/v1/nginx.go b/backend/app/api/v1/nginx.go index ba81e78e5..8db5c4337 100644 --- a/backend/app/api/v1/nginx.go +++ b/backend/app/api/v1/nginx.go @@ -3,12 +3,12 @@ package v1 import ( "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" "github.com/1Panel-dev/1Panel/backend/app/dto" + "github.com/1Panel-dev/1Panel/backend/app/request" "github.com/1Panel-dev/1Panel/backend/constant" "github.com/gin-gonic/gin" ) func (b *BaseApi) GetNginx(c *gin.Context) { - fileInfo, err := nginxService.GetNginxConfig() if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) @@ -18,7 +18,6 @@ func (b *BaseApi) GetNginx(c *gin.Context) { } func (b *BaseApi) GetNginxConfigByScope(c *gin.Context) { - var req dto.NginxScopeReq if err := c.ShouldBindJSON(&req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) @@ -34,7 +33,6 @@ func (b *BaseApi) GetNginxConfigByScope(c *gin.Context) { } func (b *BaseApi) UpdateNginxConfigBy(c *gin.Context) { - var req dto.NginxConfigReq if err := c.ShouldBindJSON(&req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) @@ -49,7 +47,6 @@ func (b *BaseApi) UpdateNginxConfigBy(c *gin.Context) { } func (b *BaseApi) GetNginxStatus(c *gin.Context) { - res, err := nginxService.GetStatus() if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) @@ -57,3 +54,17 @@ func (b *BaseApi) GetNginxStatus(c *gin.Context) { } helper.SuccessWithData(c, res) } + +func (b *BaseApi) UpdateNginxFile(c *gin.Context) { + var req request.NginxConfigFileUpdate + if err := c.ShouldBindJSON(&req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + + if err := nginxService.UpdateConfigFile(req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + helper.SuccessWithData(c, nil) +} diff --git a/backend/app/request/nginx.go b/backend/app/request/nginx.go new file mode 100644 index 000000000..7a216cb4e --- /dev/null +++ b/backend/app/request/nginx.go @@ -0,0 +1,7 @@ +package request + +type NginxConfigFileUpdate struct { + Content string `json:"content" validate:"required"` + FilePath string `json:"filePath" validate:"required"` + Backup bool `json:"backup" validate:"required"` +} diff --git a/backend/app/service/app_install.go b/backend/app/service/app_install.go index 8063aa15c..4f6873e51 100644 --- a/backend/app/service/app_install.go +++ b/backend/app/service/app_install.go @@ -366,6 +366,28 @@ func (a AppInstallService) DeleteCheck(installId uint) ([]dto.AppResource, error return res, nil } +func (a AppInstallService) GetDefaultConfigByKey(key string) (string, error) { + appInstall, err := getAppInstallByKey(key) + if err != nil { + return "", err + } + filePath := path.Join(constant.AppResourceDir, appInstall.App.Key, "versions", appInstall.Version, "conf") + if key == "mysql" { + filePath = path.Join(filePath, "my.cnf") + } + if key == "redis" { + filePath = path.Join(filePath, "redis.conf") + } + if key == "nginx" { + filePath = path.Join(filePath, "nginx.conf") + } + contentByte, err := os.ReadFile(filePath) + if err != nil { + return "", err + } + return string(contentByte), nil +} + func syncById(installId uint) error { appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(installId)) if err != nil { diff --git a/backend/app/service/nginx.go b/backend/app/service/nginx.go index 788581460..08729ccdc 100644 --- a/backend/app/service/nginx.go +++ b/backend/app/service/nginx.go @@ -1,10 +1,13 @@ package service import ( + "github.com/1Panel-dev/1Panel/backend/app/request" "io/ioutil" "net/http" + "os" "path" "strings" + "time" "github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/constant" @@ -66,3 +69,34 @@ func (n NginxService) GetStatus() (dto.NginxStatus, error) { status.Waiting = resArray[15] return status, nil } + +func (n NginxService) UpdateConfigFile(req request.NginxConfigFileUpdate) error { + fileOp := files.NewFileOp() + if req.Backup { + backupPath := path.Join(path.Dir(req.FilePath), "bak") + if !fileOp.Stat(backupPath) { + if err := fileOp.CreateDir(backupPath, 0755); err != nil { + return err + } + } + newFile := path.Join(backupPath, "nginx.bak"+"-"+time.Now().Format("2006-01-02-15-04-05")) + if err := fileOp.Copy(req.FilePath, backupPath); err != nil { + return err + } + if err := fileOp.Rename(path.Join(backupPath, "nginx.conf"), newFile); err != nil { + return err + } + } + oldContent, err := os.ReadFile(req.FilePath) + if err != nil { + return err + } + if err := fileOp.WriteFile(req.FilePath, strings.NewReader(req.Content), 0644); err != nil { + return err + } + nginxInstall, err := getAppInstallByKey("nginx") + if err != nil { + return err + } + return nginxCheckAndReload(string(oldContent), req.FilePath, nginxInstall.ContainerName) +} diff --git a/backend/app/service/nginx_utils.go b/backend/app/service/nginx_utils.go index 53f07743f..23bf426cd 100644 --- a/backend/app/service/nginx_utils.go +++ b/backend/app/service/nginx_utils.go @@ -201,7 +201,6 @@ func opNginx(containerName, operate string) error { } func nginxCheckAndReload(oldContent string, filePath string, containerName string) error { - if err := opNginx(containerName, constant.NginxCheck); err != nil { _ = files.NewFileOp().WriteFile(filePath, strings.NewReader(oldContent), 0644) return err @@ -211,6 +210,5 @@ func nginxCheckAndReload(oldContent string, filePath string, containerName strin _ = files.NewFileOp().WriteFile(filePath, strings.NewReader(oldContent), 0644) return err } - return nil } diff --git a/backend/router/ro_app.go b/backend/router/ro_app.go index fce5320db..b6fb9dcca 100644 --- a/backend/router/ro_app.go +++ b/backend/router/ro_app.go @@ -31,5 +31,6 @@ func (a *AppRouter) InitAppRouter(Router *gin.RouterGroup) { appRouter.POST("/installed/backups/del", baseApi.DeleteAppBackup) appRouter.POST("/installed/port/change", baseApi.ChangeAppPort) appRouter.GET("/services/:key", baseApi.GetServices) + appRouter.GET("/installed/conf/:key", baseApi.GetDefaultConfig) } } diff --git a/backend/router/ro_nginx.go b/backend/router/ro_nginx.go index eb9632f35..90e85a859 100644 --- a/backend/router/ro_nginx.go +++ b/backend/router/ro_nginx.go @@ -19,5 +19,6 @@ func (a *NginxRouter) InitNginxRouter(Router *gin.RouterGroup) { groupRouter.POST("/scope", baseApi.GetNginxConfigByScope) groupRouter.POST("/update", baseApi.UpdateNginxConfigBy) groupRouter.GET("/status", baseApi.GetNginxStatus) + groupRouter.POST("/file", baseApi.UpdateNginxFile) } } diff --git a/frontend/src/api/interface/nginx.ts b/frontend/src/api/interface/nginx.ts index d2502f3f4..34a1c5f06 100644 --- a/frontend/src/api/interface/nginx.ts +++ b/frontend/src/api/interface/nginx.ts @@ -23,4 +23,10 @@ export namespace Nginx { writing: string; waiting: string; } + + export interface NginxFileUpdate { + content: string; + filePath: string; + backup: boolean; + } } diff --git a/frontend/src/api/modules/app.ts b/frontend/src/api/modules/app.ts index 3ee69ae07..6a5a05353 100644 --- a/frontend/src/api/modules/app.ts +++ b/frontend/src/api/modules/app.ts @@ -69,3 +69,7 @@ export const DelAppBackups = (req: App.AppBackupDelReq) => { export const GetAppUpdateVersions = (id: number) => { return http.get(`apps/installed/${id}/versions`); }; + +export const GetAppDefaultConfig = (key: string) => { + return http.get(`apps/installed/conf/${key}`); +}; diff --git a/frontend/src/api/modules/nginx.ts b/frontend/src/api/modules/nginx.ts index f007c8985..d966b3112 100644 --- a/frontend/src/api/modules/nginx.ts +++ b/frontend/src/api/modules/nginx.ts @@ -17,3 +17,7 @@ export const UpdateNginxConfigByScope = (req: Nginx.NginxConfigReq) => { export const GetNginxStatus = () => { return http.get(`/nginx/status`); }; + +export const UpdateNginxConfigFile = (req: Nginx.NginxFileUpdate) => { + return http.post(`/nginx/file`, req); +}; diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index 65e1402e2..5b4cb1991 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -777,6 +777,8 @@ export default { checkTitle: '提示', website: '网站', database: '数据库', + defaultConfig: '默认配置', + defaultConfigHelper: '已恢复为默认配置,保存后生效', }, website: { website: '网站', diff --git a/frontend/src/views/website/website/nginx/source/index.vue b/frontend/src/views/website/website/nginx/source/index.vue index 4a4378542..b87eb9bfa 100644 --- a/frontend/src/views/website/website/nginx/source/index.vue +++ b/frontend/src/views/website/website/nginx/source/index.vue @@ -15,36 +15,53 @@ :readOnly="true" />
+ + {{ $t('app.defaultConfig') }} + {{ $t('commons.button.save') }}
+ + + + +