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

View File

@ -197,7 +197,7 @@ func (b *BaseApi) CreateWebDomain(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 {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
@ -211,7 +211,7 @@ func (b *BaseApi) GetNginxConfig(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 {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return

View File

@ -17,30 +17,15 @@ type NginxFull struct {
}
type NginxConfig struct {
FilePath string `json:"filePath"`
Config *components.Config `json:"config"`
OldContent string `json:"oldContent"`
FilePath string
Config *components.Config
OldContent string
}
type NginxConfigReq struct {
Scope NginxKey `json:"scope"`
Operate NginxOp `json:"operate"`
WebsiteID uint `json:"webSiteId"`
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 NginxParam struct {
UpdateScope string
Name string
Params []string
}
type NginxKey string
@ -52,14 +37,6 @@ const (
HttpPer NginxKey = "http-per"
)
type NginxOp string
const (
ConfigNew NginxOp = "add"
ConfigUpdate NginxOp = "update"
ConfigDel NginxOp = "delete"
)
var ScopeKeyMap = map[NginxKey][]string{
Index: {"index"},
LimitConn: {"limit_conn", "limit_rate", "limit_conn_zone"},
@ -72,9 +49,3 @@ var StaticFileKeyMap = map[NginxKey]struct {
SSL: {},
LimitConn: {},
}
type NginxParam struct {
UpdateScope string `json:"scope"`
Name string `json:"name"`
Params []string `json:"params"`
}

View File

@ -1,7 +1,21 @@
package request
import "github.com/1Panel-dev/1Panel/backend/app/dto"
type NginxConfigFileUpdate struct {
Content string `json:"content" validate:"required"`
FilePath string `json:"filePath" 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
import (
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/app/model"
)
@ -17,8 +16,8 @@ type WebsitePreInstallCheck struct {
}
type WebsiteNginxConfig struct {
Enable bool `json:"enable"`
Params []dto.NginxParam `json:"params"`
Enable bool `json:"enable"`
Params []NginxParam `json:"params"`
}
type WebsiteWafConfig struct {

View File

@ -2,6 +2,7 @@ package service
import (
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
"io/ioutil"
"net/http"
"os"
@ -33,7 +34,7 @@ func (n NginxService) GetNginxConfig() (dto.FileInfo, error) {
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]
if !ok || len(keys) == 0 {
return nil, nil
@ -41,7 +42,7 @@ func (n NginxService) GetConfigByScope(req dto.NginxScopeReq) ([]dto.NginxParam,
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]
if !ok || len(keys) == 0 {
return nil
@ -49,16 +50,16 @@ func (n NginxService) UpdateConfigByScope(req dto.NginxConfigReq) error {
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")
if err != nil {
return dto.NginxStatus{}, err
return response.NginxStatus{}, err
}
content, err := ioutil.ReadAll(res.Body)
if err != nil {
return dto.NginxStatus{}, err
return response.NginxStatus{}, err
}
var status dto.NginxStatus
var status response.NginxStatus
resArray := strings.Split(string(content), " ")
status.Active = resArray[2]
status.Accepts = resArray[7]

View File

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

View File

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

View File

@ -4,9 +4,11 @@ const (
NginxScopeServer = "server"
NginxScopeHttp = "http"
NginxScopeOut = "out"
)
const (
NginxReload = "reload"
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.POST("/scope", baseApi.GetNginxConfigByScope)
groupRouter.POST("/update", baseApi.UpdateNginxConfigBy)
groupRouter.POST("/update", baseApi.UpdateNginxConfigByScope)
groupRouter.GET("/status", baseApi.GetNginxStatus)
groupRouter.POST("/file", baseApi.UpdateNginxFile)
}

View File

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

View File

@ -71,7 +71,7 @@ export const CreateDomain = (req: Website.DomainCreate) => {
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);
};

View File

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