1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-31 22:18:07 +08:00

feat: 修改网站nginx配置文件 reload

This commit is contained in:
zhengkunwang223 2022-12-28 11:37:04 +08:00 committed by zhengkunwang223
parent 78a437d0ca
commit 0f739b4afa
8 changed files with 50 additions and 6 deletions

View File

@ -304,3 +304,16 @@ func (b *BaseApi) UpdateWebsiteWafConfig(c *gin.Context) {
}
helper.SuccessWithData(c, nil)
}
func (b *BaseApi) UpdateWebsiteNginxConfig(c *gin.Context) {
var req request.WebsiteNginxUpdate
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
if err := websiteService.UpdateNginxConfigFile(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, nil)
}

View File

@ -103,3 +103,8 @@ type WebsiteHTTPSOp struct {
PrivateKey string `json:"privateKey"`
Certificate string `json:"certificate"`
}
type WebsiteNginxUpdate struct {
ID uint `json:"id" validate:"required"`
Content string `json:"content" validate:"required"`
}

View File

@ -653,7 +653,7 @@ func (w WebsiteService) GetWafConfig(req request.WebsiteWafReq) (response.Websit
func (w WebsiteService) UpdateWafConfig(req request.WebsiteWafUpdate) error {
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
if err != nil {
return nil
return err
}
updateValue := "on"
if !req.Enable {
@ -663,3 +663,19 @@ func (w WebsiteService) UpdateWafConfig(req request.WebsiteWafUpdate) error {
{Name: "set", Params: []string{req.Key, updateValue}},
}, &website)
}
func (w WebsiteService) UpdateNginxConfigFile(req request.WebsiteNginxUpdate) error {
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
if err != nil {
return err
}
nginxFull, err := getNginxFull(&website)
if err != nil {
return err
}
filePath := nginxFull.SiteConfig.FilePath
if err := files.NewFileOp().WriteFile(filePath, strings.NewReader(req.Content), 0755); err != nil {
return err
}
return nginxCheckAndReload(nginxFull.SiteConfig.OldContent, filePath, nginxFull.Install.ContainerName)
}

View File

@ -38,5 +38,6 @@ func (a *WebsiteRouter) InitWebsiteRouter(Router *gin.RouterGroup) {
groupRouter.POST("/:id/https", baseApi.UpdateHTTPSConfig)
groupRouter.POST("/waf/config", baseApi.GetWebsiteWafConfig)
groupRouter.POST("/waf/update", baseApi.UpdateWebsiteWafConfig)
groupRouter.POST("/nginx/update", baseApi.UpdateWebsiteNginxConfig)
}
}

View File

@ -247,4 +247,9 @@ export namespace Website {
export interface BackupReq {
id: number;
}
export interface NginxUpdate {
id: number;
content: string;
}
}

View File

@ -162,3 +162,7 @@ export const GetWafConfig = (req: Website.WafReq) => {
export const UpdateWafEnable = (req: Website.WafUpdate) => {
return http.post<any>(`/websites/waf/update`, req);
};
export const UpdateNginxFile = (req: Website.NginxUpdate) => {
return http.post<any>(`/websites/nginx/update`, req);
};

View File

@ -939,6 +939,7 @@ export default {
status: '当前状态',
nginxConfig: 'OpenResty 设置',
configResource: '配置修改',
saveAndReload: '保存并重载',
},
ssl: {
provider: '类型',

View File

@ -16,7 +16,7 @@
/>
<div style="margin-top: 10px">
<el-button type="primary" @click="submit()" :loading="loading">
{{ $t('commons.button.save') }}
{{ $t('nginx.saveAndReload') }}
</el-button>
</div>
</div>
@ -24,10 +24,9 @@
<script lang="ts" setup>
import { Codemirror } from 'vue-codemirror';
import { oneDark } from '@codemirror/theme-one-dark';
import { GetWebsiteNginx } from '@/api/modules/website';
import { GetWebsiteNginx, UpdateNginxFile } from '@/api/modules/website';
import { computed, onMounted, ref } from 'vue';
import { File } from '@/api/interface/file';
import { SaveFileContent } from '@/api/modules/files';
import { ElMessage } from 'element-plus';
import i18n from '@/lang';
import { StreamLanguage } from '@codemirror/language';
@ -64,8 +63,8 @@ const get = () => {
const submit = () => {
loading.value = true;
SaveFileContent({
path: data.value.path,
UpdateNginxFile({
id: id.value,
content: content.value,
})
.then(() => {