diff --git a/backend/app/api/v1/website.go b/backend/app/api/v1/website.go index e43f32b12..0ec713628 100644 --- a/backend/app/api/v1/website.go +++ b/backend/app/api/v1/website.go @@ -96,3 +96,30 @@ func (b *BaseApi) CreateWebDomain(c *gin.Context) { } helper.SuccessWithData(c, domain) } + +func (b *BaseApi) GetNginxConfig(c *gin.Context) { + var req dto.NginxConfigReq + if err := c.ShouldBindJSON(&req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + config, err := websiteService.GetNginxConfigByScope(req) + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + helper.SuccessWithData(c, config) +} + +func (b *BaseApi) UpdateNginxConfig(c *gin.Context) { + var req dto.NginxConfigReq + if err := c.ShouldBindJSON(&req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + if err := websiteService.UpdateNginxConfigByScope(req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + helper.SuccessWithData(c, nil) +} diff --git a/backend/app/dto/nginx.go b/backend/app/dto/nginx.go new file mode 100644 index 000000000..85d9d6fed --- /dev/null +++ b/backend/app/dto/nginx.go @@ -0,0 +1,26 @@ +package dto + +import "github.com/1Panel-dev/1Panel/backend/utils/nginx/components" + +type NginxConfig struct { + FilePath string `json:"filePath"` + ContainerName string `json:"containerName"` + Config *components.Config `json:"config"` + OldContent string `json:"oldContent"` +} + +type NginxConfigReq struct { + Scope NginxScope `json:"scope"` + WebSiteID uint `json:"webSiteId" validate:"required"` + Params map[string]string `json:"params"` +} + +type NginxScope string + +const ( + Index NginxScope = "index" +) + +var ScopeKeyMap = map[NginxScope][]string{ + Index: {"index"}, +} diff --git a/backend/app/dto/website.go b/backend/app/dto/website.go index d0db5e735..dccdb1bb2 100644 --- a/backend/app/dto/website.go +++ b/backend/app/dto/website.go @@ -2,7 +2,6 @@ package dto import ( "github.com/1Panel-dev/1Panel/backend/app/model" - "github.com/1Panel-dev/1Panel/backend/utils/nginx/components" ) type WebSiteReq struct { @@ -59,10 +58,3 @@ type WebSiteDomainCreate struct { Port int `json:"port"` Domain string `json:"domain"` } - -type NginxConfig struct { - FilePath string `json:"filePath"` - ContainerName string `json:"containerName"` - Config *components.Config `json:"config"` - OldContent string `json:"oldContent"` -} diff --git a/backend/app/service/website.go b/backend/app/service/website.go index 79694d9b7..60259078d 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -185,3 +185,41 @@ func (w WebsiteService) DeleteWebsiteDomain(domainId uint) error { return websiteDomainRepo.DeleteBy(context.TODO(), commonRepo.WithByID(domainId)) } + +func (w WebsiteService) GetNginxConfigByScope(req dto.NginxConfigReq) (map[string]interface{}, error) { + + keys, ok := dto.ScopeKeyMap[req.Scope] + if !ok || len(keys) == 0 { + return nil, nil + } + + website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebSiteID)) + if err != nil { + return nil, err + } + + return getNginxConfigByKeys(website, keys) +} + +func (w WebsiteService) UpdateNginxConfigByScope(req dto.NginxConfigReq) error { + + keys, ok := dto.ScopeKeyMap[req.Scope] + if !ok || len(keys) == 0 { + return nil + } + keyValues := make(map[string][]string, len(keys)) + for k, v := range req.Params { + for _, name := range keys { + if name == k { + keyValues[k] = getNginxParams(k, v) + } + } + } + + website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebSiteID)) + if err != nil { + return err + } + + return updateNginxConfig(website, keyValues) +} diff --git a/backend/app/service/website_utils.go b/backend/app/service/website_utils.go index 417ee3b97..8c1d310b7 100644 --- a/backend/app/service/website_utils.go +++ b/backend/app/service/website_utils.go @@ -8,6 +8,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/1Panel-dev/1Panel/backend/utils/nginx" + "github.com/1Panel-dev/1Panel/backend/utils/nginx/components" "github.com/1Panel-dev/1Panel/backend/utils/nginx/parser" "github.com/1Panel-dev/1Panel/cmd/server/nginx_conf" "github.com/pkg/errors" @@ -204,3 +205,51 @@ func deleteListenAndServerName(website model.WebSite, ports []int, domains []str } return nginxCheckAndReload(nginxConfig.OldContent, nginxConfig.FilePath, nginxConfig.ContainerName) } + +func getNginxConfigByKeys(website model.WebSite, keys []string) (map[string]interface{}, error) { + nginxConfig, err := getNginxConfig(website.PrimaryDomain) + if err != nil { + return nil, err + } + config := nginxConfig.Config + server := config.FindServers()[0] + res := make(map[string]interface{}) + for _, key := range keys { + dirs := server.FindDirectives(key) + for _, dir := range dirs { + res[dir.GetName()] = dir.GetParameters() + } + } + return res, nil +} + +func updateNginxConfig(website model.WebSite, keyValues map[string][]string) error { + nginxConfig, err := getNginxConfig(website.PrimaryDomain) + if err != nil { + return err + } + config := nginxConfig.Config + server := config.FindServers()[0] + for k, v := range keyValues { + newDir := components.Directive{ + Name: k, + Parameters: v, + } + server.UpdateDirectives(k, newDir) + } + if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil { + return err + } + return nginxCheckAndReload(nginxConfig.OldContent, nginxConfig.FilePath, nginxConfig.ContainerName) +} + +func getNginxParams(key string, param interface{}) []string { + var res []string + switch param.(type) { + case string: + if key == "index" { + res = strings.Split(param.(string), "\n") + } + } + return res +} diff --git a/backend/router/ro_website.go b/backend/router/ro_website.go index 24ef61f63..9ead37dc6 100644 --- a/backend/router/ro_website.go +++ b/backend/router/ro_website.go @@ -21,5 +21,7 @@ func (a *WebsiteRouter) InitWebsiteRouter(Router *gin.RouterGroup) { groupRouter.GET("/domains/:websiteId", baseApi.GetWebDomains) groupRouter.DELETE("/domains/:id", baseApi.DeleteWebDomain) groupRouter.POST("/domains", baseApi.CreateWebDomain) + groupRouter.POST("/config", baseApi.GetNginxConfig) + groupRouter.POST("/config/update", baseApi.UpdateNginxConfig) } } diff --git a/frontend/src/api/interface/website.ts b/frontend/src/api/interface/website.ts index 565fccdf1..856b43eec 100644 --- a/frontend/src/api/interface/website.ts +++ b/frontend/src/api/interface/website.ts @@ -63,4 +63,10 @@ export namespace WebSite { port: number; domain: string; } + + export interface NginxConfigReq { + websiteId: number; + scope: string; + params?: any; + } } diff --git a/frontend/src/api/modules/website.ts b/frontend/src/api/modules/website.ts index 967d882de..7c5f06d72 100644 --- a/frontend/src/api/modules/website.ts +++ b/frontend/src/api/modules/website.ts @@ -41,3 +41,11 @@ export const DeleteDomain = (id: number) => { export const CreateDomain = (req: WebSite.DomainCreate) => { return http.post(`/websites/domains`, req); }; + +export const GetNginxConfig = (req: WebSite.NginxConfigReq) => { + return http.post(`/websites/config`, req); +}; + +export const UpdateNginxConfig = (req: WebSite.NginxConfigReq) => { + return http.post(`/websites/config/update`, req); +}; diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index c0103e37f..c15567dfb 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -685,5 +685,7 @@ export default { domainHelper: '一行一个域名,支持*和IP地址,支持域名:端口', port: '端口', addDomain: '新增域名', + domainConfig: '域名设置', + defaultDoc: '默认文档', }, }; diff --git a/frontend/src/views/website/project/config/basic/default-doc/index.vue b/frontend/src/views/website/project/config/basic/default-doc/index.vue new file mode 100644 index 000000000..c4a4601ba --- /dev/null +++ b/frontend/src/views/website/project/config/basic/default-doc/index.vue @@ -0,0 +1,94 @@ + + + diff --git a/frontend/src/views/website/project/config/basic/domain/create/index.vue b/frontend/src/views/website/project/config/basic/domain/create/index.vue index 657c98423..ff8645b71 100644 --- a/frontend/src/views/website/project/config/basic/domain/create/index.vue +++ b/frontend/src/views/website/project/config/basic/domain/create/index.vue @@ -59,7 +59,6 @@ const submit = async (formEl: FormInstance | undefined) => { return; } loading.value = true; - console.log(domain.value); CreateDomain(domain.value) .then(() => { ElMessage.success(i18n.global.t('commons.msg.createSuccess')); diff --git a/frontend/src/views/website/project/config/basic/domain/index.vue b/frontend/src/views/website/project/config/basic/domain/index.vue index 63c13af1a..045d83ec2 100644 --- a/frontend/src/views/website/project/config/basic/domain/index.vue +++ b/frontend/src/views/website/project/config/basic/domain/index.vue @@ -38,6 +38,9 @@ const buttons = [ click: function (row: WebSite.Domain) { deleteDoamin(row.id); }, + disabled: () => { + return data.value.length == 1; + }, }, ]; diff --git a/frontend/src/views/website/project/config/basic/index.vue b/frontend/src/views/website/project/config/basic/index.vue index ec52c5bee..dcd53f4bc 100644 --- a/frontend/src/views/website/project/config/basic/index.vue +++ b/frontend/src/views/website/project/config/basic/index.vue @@ -1,9 +1,11 @@