1Panel/backend/app/service/nginx.go

117 lines
3.2 KiB
Go
Raw Normal View History

2022-11-23 16:19:05 +08:00
package service
import (
"io"
"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
"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 {
GetNginxConfig() (response.FileInfo, error)
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{}
}
2022-12-14 19:39:32 +08:00
func (n NginxService) GetNginxConfig() (response.FileInfo, error) {
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
2022-11-23 16:19:05 +08:00
if err != nil {
2022-12-14 19:39:32 +08:00
return response.FileInfo{}, err
2022-11-23 16:19:05 +08:00
}
configPath := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, "conf", "nginx.conf")
2022-11-23 16:19:05 +08:00
info, err := files.NewFileInfo(files.FileOption{
Path: configPath,
Expand: true,
})
if err != nil {
2022-12-14 19:39:32 +08:00
return response.FileInfo{}, err
2022-11-23 16:19:05 +08:00
}
2022-12-14 19:39:32 +08:00
return response.FileInfo{FileInfo: *info}, 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) {
res, err := http.Get("http://127.0.0.1/nginx_status")
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
}
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
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()
if req.Backup {
backupPath := path.Join(path.Dir(req.FilePath), "bak")
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"))
if err := fileOp.Copy(req.FilePath, backupPath); err != nil {
return err
}
if err := fileOp.Rename(path.Join(backupPath, "nginx.conf"), newFile); err != nil {
return err
}
}
oldContent, err := os.ReadFile(req.FilePath)
if err != nil {
return err
}
if err := fileOp.WriteFile(req.FilePath, strings.NewReader(req.Content), 0644); err != nil {
return err
}
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
2022-12-09 17:16:07 +08:00
if err != nil {
return err
}
return nginxCheckAndReload(string(oldContent), req.FilePath, nginxInstall.ContainerName)
}