2022-11-23 16:19:05 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2023-08-24 18:20:15 +08:00
|
|
|
"fmt"
|
2023-04-07 11:30:10 +08:00
|
|
|
"io"
|
2022-12-09 10:02:36 +08:00
|
|
|
"net/http"
|
2022-12-09 17:16:07 +08:00
|
|
|
"os"
|
2022-11-29 17:39:10 +08:00
|
|
|
"path"
|
|
|
|
"strings"
|
2022-12-09 17:16:07 +08:00
|
|
|
"time"
|
2022-11-29 17:39:10 +08:00
|
|
|
|
2023-04-07 11:30:10 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
|
|
|
|
|
2022-11-23 16:19:05 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NginxService struct {
|
|
|
|
}
|
|
|
|
|
2023-03-28 18:00:06 +08:00
|
|
|
type INginxService interface {
|
2023-08-02 22:38:32 +08:00
|
|
|
GetNginxConfig() (*response.NginxFile, error)
|
2023-03-28 18:00:06 +08:00
|
|
|
GetConfigByScope(req request.NginxScopeReq) ([]response.NginxParam, error)
|
|
|
|
UpdateConfigByScope(req request.NginxConfigUpdate) error
|
|
|
|
GetStatus() (response.NginxStatus, error)
|
|
|
|
UpdateConfigFile(req request.NginxConfigFileUpdate) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewINginxService() INginxService {
|
|
|
|
return &NginxService{}
|
|
|
|
}
|
|
|
|
|
2023-08-02 22:38:32 +08:00
|
|
|
func (n NginxService) GetNginxConfig() (*response.NginxFile, error) {
|
2023-02-22 17:13:08 +08:00
|
|
|
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
2022-11-23 16:19:05 +08:00
|
|
|
if err != nil {
|
2023-08-02 22:38:32 +08:00
|
|
|
return nil, err
|
2022-11-23 16:19:05 +08:00
|
|
|
}
|
2023-02-22 17:13:08 +08:00
|
|
|
configPath := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, "conf", "nginx.conf")
|
2023-08-02 22:38:32 +08:00
|
|
|
byteContent, err := files.NewFileOp().GetContent(configPath)
|
2022-11-23 16:19:05 +08:00
|
|
|
if err != nil {
|
2023-08-02 22:38:32 +08:00
|
|
|
return nil, err
|
2022-11-23 16:19:05 +08:00
|
|
|
}
|
2023-08-02 22:38:32 +08:00
|
|
|
return &response.NginxFile{Content: string(byteContent)}, nil
|
2022-11-23 16:19:05 +08:00
|
|
|
}
|
2022-11-24 10:28:39 +08:00
|
|
|
|
2022-12-13 18:54:46 +08:00
|
|
|
func (n NginxService) GetConfigByScope(req request.NginxScopeReq) ([]response.NginxParam, error) {
|
2022-11-24 10:28:39 +08:00
|
|
|
keys, ok := dto.ScopeKeyMap[req.Scope]
|
|
|
|
if !ok || len(keys) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-12-01 00:41:50 +08:00
|
|
|
return getNginxParamsByKeys(constant.NginxScopeHttp, keys, nil)
|
2022-11-24 10:28:39 +08:00
|
|
|
}
|
|
|
|
|
2022-12-13 18:54:46 +08:00
|
|
|
func (n NginxService) UpdateConfigByScope(req request.NginxConfigUpdate) error {
|
2022-11-24 10:28:39 +08:00
|
|
|
keys, ok := dto.ScopeKeyMap[req.Scope]
|
|
|
|
if !ok || len(keys) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2022-12-01 00:41:50 +08:00
|
|
|
return updateNginxConfig(constant.NginxScopeHttp, getNginxParams(req.Params, keys), nil)
|
2022-11-24 10:28:39 +08:00
|
|
|
}
|
2022-11-24 16:06:18 +08:00
|
|
|
|
2022-12-13 18:54:46 +08:00
|
|
|
func (n NginxService) GetStatus() (response.NginxStatus, error) {
|
2023-08-24 18:20:15 +08:00
|
|
|
httpPort, _, err := getAppInstallPort(constant.AppOpenresty)
|
|
|
|
if err != nil {
|
|
|
|
return response.NginxStatus{}, err
|
|
|
|
}
|
|
|
|
url := "http://127.0.0.1/nginx_status"
|
|
|
|
if httpPort != 80 {
|
|
|
|
url = fmt.Sprintf("http://127.0.0.1:%v/nginx_status", httpPort)
|
|
|
|
}
|
|
|
|
res, err := http.Get(url)
|
2022-11-24 16:06:18 +08:00
|
|
|
if err != nil {
|
2022-12-13 18:54:46 +08:00
|
|
|
return response.NginxStatus{}, err
|
2022-11-24 16:06:18 +08:00
|
|
|
}
|
2023-04-07 11:30:10 +08:00
|
|
|
content, err := io.ReadAll(res.Body)
|
2022-11-24 16:06:18 +08:00
|
|
|
if err != nil {
|
2022-12-13 18:54:46 +08:00
|
|
|
return response.NginxStatus{}, err
|
2022-11-24 16:06:18 +08:00
|
|
|
}
|
2022-12-13 18:54:46 +08:00
|
|
|
var status response.NginxStatus
|
2022-12-09 10:02:36 +08:00
|
|
|
resArray := strings.Split(string(content), " ")
|
2022-11-24 16:06:18 +08:00
|
|
|
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
|
|
|
|
}
|
2022-12-09 17:16:07 +08:00
|
|
|
|
|
|
|
func (n NginxService) UpdateConfigFile(req request.NginxConfigFileUpdate) error {
|
|
|
|
fileOp := files.NewFileOp()
|
2023-08-02 22:38:32 +08:00
|
|
|
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
|
|
|
filePath := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, "conf", "nginx.conf")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-09 17:16:07 +08:00
|
|
|
if req.Backup {
|
2023-08-02 22:38:32 +08:00
|
|
|
backupPath := path.Join(path.Dir(filePath), "bak")
|
2022-12-09 17:16:07 +08:00
|
|
|
if !fileOp.Stat(backupPath) {
|
|
|
|
if err := fileOp.CreateDir(backupPath, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newFile := path.Join(backupPath, "nginx.bak"+"-"+time.Now().Format("2006-01-02-15-04-05"))
|
2023-08-02 22:38:32 +08:00
|
|
|
if err := fileOp.Copy(filePath, backupPath); err != nil {
|
2022-12-09 17:16:07 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := fileOp.Rename(path.Join(backupPath, "nginx.conf"), newFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-08-02 22:38:32 +08:00
|
|
|
oldContent, err := os.ReadFile(filePath)
|
2022-12-09 17:16:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-02 22:38:32 +08:00
|
|
|
if err = fileOp.WriteFile(filePath, strings.NewReader(req.Content), 0644); err != nil {
|
2022-12-09 17:16:07 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-08-02 22:38:32 +08:00
|
|
|
return nginxCheckAndReload(string(oldContent), filePath, nginxInstall.ContainerName)
|
2022-12-09 17:16:07 +08:00
|
|
|
}
|