1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-19 08:19:15 +08:00

style: 修改nginx的API和dto

This commit is contained in:
zhengkunwang223 2022-12-13 18:54:46 +08:00 committed by zhengkunwang223
parent 09ef0ebc80
commit f85d17cf65
15 changed files with 86 additions and 71 deletions

View File

@ -2,7 +2,6 @@ package v1
import ( import (
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" "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/dto/request" "github.com/1Panel-dev/1Panel/backend/app/dto/request"
"github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/constant"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -18,7 +17,7 @@ func (b *BaseApi) GetNginx(c *gin.Context) {
} }
func (b *BaseApi) GetNginxConfigByScope(c *gin.Context) { func (b *BaseApi) GetNginxConfigByScope(c *gin.Context) {
var req dto.NginxScopeReq var req request.NginxScopeReq
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
@ -32,13 +31,12 @@ func (b *BaseApi) GetNginxConfigByScope(c *gin.Context) {
helper.SuccessWithData(c, params) helper.SuccessWithData(c, params)
} }
func (b *BaseApi) UpdateNginxConfigBy(c *gin.Context) { func (b *BaseApi) UpdateNginxConfigByScope(c *gin.Context) {
var req dto.NginxConfigReq var req request.NginxConfigUpdate
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
} }
if err := nginxService.UpdateConfigByScope(req); err != nil { if err := nginxService.UpdateConfigByScope(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return return

View File

@ -197,7 +197,7 @@ func (b *BaseApi) CreateWebDomain(c *gin.Context) {
} }
func (b *BaseApi) GetNginxConfig(c *gin.Context) { func (b *BaseApi) GetNginxConfig(c *gin.Context) {
var req dto.NginxConfigReq var req request.NginxScopeReq
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
@ -211,7 +211,7 @@ func (b *BaseApi) GetNginxConfig(c *gin.Context) {
} }
func (b *BaseApi) UpdateNginxConfig(c *gin.Context) { func (b *BaseApi) UpdateNginxConfig(c *gin.Context) {
var req dto.NginxConfigReq var req request.NginxConfigUpdate
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return

View File

@ -17,30 +17,15 @@ type NginxFull struct {
} }
type NginxConfig struct { type NginxConfig struct {
FilePath string `json:"filePath"` FilePath string
Config *components.Config `json:"config"` Config *components.Config
OldContent string `json:"oldContent"` OldContent string
} }
type NginxConfigReq struct { type NginxParam struct {
Scope NginxKey `json:"scope"` UpdateScope string
Operate NginxOp `json:"operate"` Name string
WebsiteID uint `json:"webSiteId"` Params []string
Params interface{} `json:"params"`
}
type NginxScopeReq struct {
Scope NginxKey `json:"scope"`
}
type NginxStatus struct {
Active string `json:"active"`
Accepts string `json:"accepts"`
Handled string `json:"handled"`
Requests string `json:"requests"`
Reading string `json:"reading"`
Writing string `json:"writing"`
Waiting string `json:"waiting"`
} }
type NginxKey string type NginxKey string
@ -52,14 +37,6 @@ const (
HttpPer NginxKey = "http-per" HttpPer NginxKey = "http-per"
) )
type NginxOp string
const (
ConfigNew NginxOp = "add"
ConfigUpdate NginxOp = "update"
ConfigDel NginxOp = "delete"
)
var ScopeKeyMap = map[NginxKey][]string{ var ScopeKeyMap = map[NginxKey][]string{
Index: {"index"}, Index: {"index"},
LimitConn: {"limit_conn", "limit_rate", "limit_conn_zone"}, LimitConn: {"limit_conn", "limit_rate", "limit_conn_zone"},
@ -72,9 +49,3 @@ var StaticFileKeyMap = map[NginxKey]struct {
SSL: {}, SSL: {},
LimitConn: {}, LimitConn: {},
} }
type NginxParam struct {
UpdateScope string `json:"scope"`
Name string `json:"name"`
Params []string `json:"params"`
}

View File

@ -1,7 +1,21 @@
package request package request
import "github.com/1Panel-dev/1Panel/backend/app/dto"
type NginxConfigFileUpdate struct { type NginxConfigFileUpdate struct {
Content string `json:"content" validate:"required"` Content string `json:"content" validate:"required"`
FilePath string `json:"filePath" validate:"required"` FilePath string `json:"filePath" validate:"required"`
Backup bool `json:"backup" validate:"required"` Backup bool `json:"backup" validate:"required"`
} }
type NginxScopeReq struct {
Scope dto.NginxKey `json:"scope" validate:"required"`
WebsiteID uint `json:"websiteId"`
}
type NginxConfigUpdate struct {
Scope dto.NginxKey `json:"scope"`
Operate string `json:"operate" validate:"required;oneof=add update delete"`
WebsiteID uint `json:"websiteId" validate:"required"`
Params interface{} `json:"params"`
}

View File

@ -0,0 +1,16 @@
package response
type NginxStatus struct {
Active string `json:"active"`
Accepts string `json:"accepts"`
Handled string `json:"handled"`
Requests string `json:"requests"`
Reading string `json:"reading"`
Writing string `json:"writing"`
Waiting string `json:"waiting"`
}
type NginxParam struct {
Name string `json:"name"`
Params []string `json:"params"`
}

View File

@ -1,7 +1,6 @@
package response package response
import ( import (
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/app/model"
) )
@ -17,8 +16,8 @@ type WebsitePreInstallCheck struct {
} }
type WebsiteNginxConfig struct { type WebsiteNginxConfig struct {
Enable bool `json:"enable"` Enable bool `json:"enable"`
Params []dto.NginxParam `json:"params"` Params []NginxParam `json:"params"`
} }
type WebsiteWafConfig struct { type WebsiteWafConfig struct {

View File

@ -2,6 +2,7 @@ package service
import ( import (
"github.com/1Panel-dev/1Panel/backend/app/dto/request" "github.com/1Panel-dev/1Panel/backend/app/dto/request"
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
@ -33,7 +34,7 @@ func (n NginxService) GetNginxConfig() (dto.FileInfo, error) {
return dto.FileInfo{FileInfo: *info}, nil return dto.FileInfo{FileInfo: *info}, nil
} }
func (n NginxService) GetConfigByScope(req dto.NginxScopeReq) ([]dto.NginxParam, error) { func (n NginxService) GetConfigByScope(req request.NginxScopeReq) ([]response.NginxParam, error) {
keys, ok := dto.ScopeKeyMap[req.Scope] keys, ok := dto.ScopeKeyMap[req.Scope]
if !ok || len(keys) == 0 { if !ok || len(keys) == 0 {
return nil, nil return nil, nil
@ -41,7 +42,7 @@ func (n NginxService) GetConfigByScope(req dto.NginxScopeReq) ([]dto.NginxParam,
return getNginxParamsByKeys(constant.NginxScopeHttp, keys, nil) return getNginxParamsByKeys(constant.NginxScopeHttp, keys, nil)
} }
func (n NginxService) UpdateConfigByScope(req dto.NginxConfigReq) error { func (n NginxService) UpdateConfigByScope(req request.NginxConfigUpdate) error {
keys, ok := dto.ScopeKeyMap[req.Scope] keys, ok := dto.ScopeKeyMap[req.Scope]
if !ok || len(keys) == 0 { if !ok || len(keys) == 0 {
return nil return nil
@ -49,16 +50,16 @@ func (n NginxService) UpdateConfigByScope(req dto.NginxConfigReq) error {
return updateNginxConfig(constant.NginxScopeHttp, getNginxParams(req.Params, keys), nil) return updateNginxConfig(constant.NginxScopeHttp, getNginxParams(req.Params, keys), nil)
} }
func (n NginxService) GetStatus() (dto.NginxStatus, error) { func (n NginxService) GetStatus() (response.NginxStatus, error) {
res, err := http.Get("http://127.0.0.1/nginx_status") res, err := http.Get("http://127.0.0.1/nginx_status")
if err != nil { if err != nil {
return dto.NginxStatus{}, err return response.NginxStatus{}, err
} }
content, err := ioutil.ReadAll(res.Body) content, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
return dto.NginxStatus{}, err return response.NginxStatus{}, err
} }
var status dto.NginxStatus var status response.NginxStatus
resArray := strings.Split(string(content), " ") resArray := strings.Split(string(content), " ")
status.Active = resArray[2] status.Active = resArray[2]
status.Accepts = resArray[7] status.Accepts = resArray[7]

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
"github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/cmd"
@ -62,12 +63,12 @@ func getNginxFull(website *model.Website) (dto.NginxFull, error) {
return nginxFull, nil return nginxFull, nil
} }
func getNginxParamsByKeys(scope string, keys []string, website *model.Website) ([]dto.NginxParam, error) { func getNginxParamsByKeys(scope string, keys []string, website *model.Website) ([]response.NginxParam, error) {
nginxFull, err := getNginxFull(website) nginxFull, err := getNginxFull(website)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var res []dto.NginxParam var res []response.NginxParam
var block components.IBlock var block components.IBlock
if scope == constant.NginxScopeHttp { if scope == constant.NginxScopeHttp {
block = nginxFull.RootConfig.Config.FindHttp() block = nginxFull.RootConfig.Config.FindHttp()
@ -77,14 +78,14 @@ func getNginxParamsByKeys(scope string, keys []string, website *model.Website) (
for _, key := range keys { for _, key := range keys {
dirs := block.FindDirectives(key) dirs := block.FindDirectives(key)
for _, dir := range dirs { for _, dir := range dirs {
nginxParam := dto.NginxParam{ nginxParam := response.NginxParam{
Name: dir.GetName(), Name: dir.GetName(),
Params: dir.GetParameters(), Params: dir.GetParameters(),
} }
res = append(res, nginxParam) res = append(res, nginxParam)
} }
if len(dirs) == 0 { if len(dirs) == 0 {
nginxParam := dto.NginxParam{ nginxParam := response.NginxParam{
Name: key, Name: key,
Params: []string{}, Params: []string{},
} }

View File

@ -39,8 +39,8 @@ type IWebsiteService interface {
CreateWebsiteDomain(create request.WebsiteDomainCreate) (model.WebsiteDomain, error) CreateWebsiteDomain(create request.WebsiteDomainCreate) (model.WebsiteDomain, error)
GetWebsiteDomain(websiteId uint) ([]model.WebsiteDomain, error) GetWebsiteDomain(websiteId uint) ([]model.WebsiteDomain, error)
DeleteWebsiteDomain(domainId uint) error DeleteWebsiteDomain(domainId uint) error
GetNginxConfigByScope(req dto.NginxConfigReq) (*response.WebsiteNginxConfig, error) GetNginxConfigByScope(req request.NginxScopeReq) (*response.WebsiteNginxConfig, error)
UpdateNginxConfigByScope(req dto.NginxConfigReq) error UpdateNginxConfigByScope(req request.NginxConfigUpdate) error
GetWebsiteNginxConfig(websiteId uint) (dto.FileInfo, error) GetWebsiteNginxConfig(websiteId uint) (dto.FileInfo, error)
GetWebsiteHTTPS(websiteId uint) (response.WebsiteHTTPS, error) GetWebsiteHTTPS(websiteId uint) (response.WebsiteHTTPS, error)
OpWebsiteHTTPS(req request.WebsiteHTTPSOp) (response.WebsiteHTTPS, error) OpWebsiteHTTPS(req request.WebsiteHTTPSOp) (response.WebsiteHTTPS, error)
@ -340,7 +340,7 @@ func (w WebsiteService) DeleteWebsiteDomain(domainId uint) error {
return websiteDomainRepo.DeleteBy(context.TODO(), commonRepo.WithByID(domainId)) return websiteDomainRepo.DeleteBy(context.TODO(), commonRepo.WithByID(domainId))
} }
func (w WebsiteService) GetNginxConfigByScope(req dto.NginxConfigReq) (*response.WebsiteNginxConfig, error) { func (w WebsiteService) GetNginxConfigByScope(req request.NginxScopeReq) (*response.WebsiteNginxConfig, error) {
keys, ok := dto.ScopeKeyMap[req.Scope] keys, ok := dto.ScopeKeyMap[req.Scope]
if !ok || len(keys) == 0 { if !ok || len(keys) == 0 {
return nil, nil return nil, nil
@ -361,7 +361,7 @@ func (w WebsiteService) GetNginxConfigByScope(req dto.NginxConfigReq) (*response
return &config, nil return &config, nil
} }
func (w WebsiteService) UpdateNginxConfigByScope(req dto.NginxConfigReq) error { func (w WebsiteService) UpdateNginxConfigByScope(req request.NginxConfigUpdate) error {
keys, ok := dto.ScopeKeyMap[req.Scope] keys, ok := dto.ScopeKeyMap[req.Scope]
if !ok || len(keys) == 0 { if !ok || len(keys) == 0 {
return nil return nil
@ -370,11 +370,11 @@ func (w WebsiteService) UpdateNginxConfigByScope(req dto.NginxConfigReq) error {
if err != nil { if err != nil {
return err return err
} }
if req.Operate == dto.ConfigDel { if req.Operate == constant.ConfigDel {
return deleteNginxConfig(constant.NginxScopeServer, keys, &website) return deleteNginxConfig(constant.NginxScopeServer, keys, &website)
} }
params := getNginxParams(req.Params, keys) params := getNginxParams(req.Params, keys)
if req.Operate == dto.ConfigNew { if req.Operate == constant.ConfigNew {
if _, ok := dto.StaticFileKeyMap[req.Scope]; ok { if _, ok := dto.StaticFileKeyMap[req.Scope]; ok {
params = getNginxParamsFromStaticFile(req.Scope, params) params = getNginxParamsFromStaticFile(req.Scope, params)
} }

View File

@ -178,7 +178,12 @@ func delNginxConfig(website model.Website, force bool) error {
if err := fileOp.DeleteFile(configPath); err != nil { if err := fileOp.DeleteFile(configPath); err != nil {
return err return err
} }
if err := opNginx(nginxInstall.ContainerName, "reload"); err != nil { sitePath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "www", "sites", website.PrimaryDomain)
if fileOp.Stat(sitePath) {
_ = fileOp.DeleteDir(sitePath)
}
if err := opNginx(nginxInstall.ContainerName, constant.NginxReload); err != nil {
if force { if force {
return nil return nil
} }

View File

@ -4,9 +4,11 @@ const (
NginxScopeServer = "server" NginxScopeServer = "server"
NginxScopeHttp = "http" NginxScopeHttp = "http"
NginxScopeOut = "out" NginxScopeOut = "out"
)
const (
NginxReload = "reload" NginxReload = "reload"
NginxCheck = "check" NginxCheck = "check"
ConfigNew = "add"
ConfigUpdate = "update"
ConfigDel = "delete"
) )

View File

@ -17,7 +17,7 @@ func (a *NginxRouter) InitNginxRouter(Router *gin.RouterGroup) {
{ {
groupRouter.GET("", baseApi.GetNginx) groupRouter.GET("", baseApi.GetNginx)
groupRouter.POST("/scope", baseApi.GetNginxConfigByScope) groupRouter.POST("/scope", baseApi.GetNginxConfigByScope)
groupRouter.POST("/update", baseApi.UpdateNginxConfigBy) groupRouter.POST("/update", baseApi.UpdateNginxConfigByScope)
groupRouter.GET("/status", baseApi.GetNginxStatus) groupRouter.GET("/status", baseApi.GetNginxStatus)
groupRouter.POST("/file", baseApi.UpdateNginxFile) groupRouter.POST("/file", baseApi.UpdateNginxFile)
} }

View File

@ -98,6 +98,11 @@ export namespace Website {
params?: any; params?: any;
} }
export interface NginxScopeReq {
websiteId: number;
scope: string;
}
export interface NginxParam { export interface NginxParam {
name: string; name: string;
params: string[]; params: string[];

View File

@ -71,7 +71,7 @@ export const CreateDomain = (req: Website.DomainCreate) => {
return http.post<any>(`/websites/domains`, req); return http.post<any>(`/websites/domains`, req);
}; };
export const GetNginxConfig = (req: Website.NginxConfigReq) => { export const GetNginxConfig = (req: Website.NginxScopeReq) => {
return http.post<Website.NginxScopeConfig>(`/websites/config`, req); return http.post<Website.NginxScopeConfig>(`/websites/config`, req);
}; };

View File

@ -57,12 +57,16 @@ let req = reactive({
websiteId: websiteId.value, websiteId: websiteId.value,
params: [{}], params: [{}],
}); });
let scopeReq = reactive({
scope: 'limit-conn',
websiteId: websiteId.value,
});
let enable = ref(false); let enable = ref(false);
let loading = ref(false); let loading = ref(false);
const search = (req: Website.NginxConfigReq) => { const search = (scopeReq: Website.NginxScopeReq) => {
loading.value = true; loading.value = true;
GetNginxConfig(req) GetNginxConfig(scopeReq)
.then((res) => { .then((res) => {
if (res.data) { if (res.data) {
enable.value = res.data.enable; enable.value = res.data.enable;
@ -125,10 +129,9 @@ const changeEnable = () => {
} else { } else {
req.operate = 'add'; req.operate = 'add';
} }
// submit(limitForm.value);
}; };
onMounted(() => { onMounted(() => {
search(req); search(scopeReq);
}); });
</script> </script>