2022-11-23 16:19:05 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-11-29 17:39:10 +08:00
|
|
|
"errors"
|
2022-11-24 16:06:18 +08:00
|
|
|
"fmt"
|
2022-11-29 17:39:10 +08:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2022-11-23 16:19:05 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2022-11-24 16:06:18 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2022-11-23 16:19:05 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NginxService struct {
|
|
|
|
}
|
|
|
|
|
2022-11-24 10:28:39 +08:00
|
|
|
func (n NginxService) GetNginxConfig() (dto.FileInfo, error) {
|
|
|
|
|
|
|
|
nginxInstall, err := getAppInstallByKey("nginx")
|
2022-11-23 16:19:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return dto.FileInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
configPath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "conf", "nginx.conf")
|
|
|
|
|
|
|
|
info, err := files.NewFileInfo(files.FileOption{
|
|
|
|
Path: configPath,
|
|
|
|
Expand: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return dto.FileInfo{}, err
|
|
|
|
}
|
|
|
|
return dto.FileInfo{FileInfo: *info}, nil
|
|
|
|
}
|
2022-11-24 10:28:39 +08:00
|
|
|
|
|
|
|
func (n NginxService) GetConfigByScope(req dto.NginxScopeReq) ([]dto.NginxParam, error) {
|
|
|
|
|
|
|
|
keys, ok := dto.ScopeKeyMap[req.Scope]
|
|
|
|
if !ok || len(keys) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return getHttpConfigByKeys(keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n NginxService) UpdateConfigByScope(req dto.NginxConfigReq) error {
|
|
|
|
keys, ok := dto.ScopeKeyMap[req.Scope]
|
|
|
|
if !ok || len(keys) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return updateHttpNginxConfig(getNginxParams(req.Params, keys))
|
|
|
|
}
|
2022-11-24 16:06:18 +08:00
|
|
|
|
|
|
|
func (n NginxService) GetStatus() (dto.NginxStatus, error) {
|
|
|
|
nginxInstall, err := getAppInstallByKey("nginx")
|
|
|
|
if err != nil {
|
|
|
|
return dto.NginxStatus{}, err
|
|
|
|
}
|
|
|
|
res, err := cmd.Exec(fmt.Sprintf("docker exec -i %s curl http://127.0.0.1/nginx_status", nginxInstall.ContainerName))
|
|
|
|
if err != nil {
|
2022-11-29 17:39:10 +08:00
|
|
|
return dto.NginxStatus{}, errors.New(res)
|
2022-11-24 16:06:18 +08:00
|
|
|
}
|
|
|
|
var status dto.NginxStatus
|
|
|
|
resArray := strings.Split(res, " ")
|
|
|
|
status.Active = resArray[2]
|
|
|
|
status.Accepts = resArray[7]
|
|
|
|
status.Handled = resArray[8]
|
|
|
|
status.Requests = resArray[9]
|
|
|
|
status.Reading = resArray[11]
|
|
|
|
status.Writing = resArray[13]
|
|
|
|
status.Waiting = resArray[15]
|
|
|
|
|
|
|
|
return status, nil
|
|
|
|
}
|