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

147 lines
4.1 KiB
Go
Raw Normal View History

2022-11-23 16:19:05 +08:00
package service
import (
"fmt"
"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/utils/compose"
"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.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
ClearProxyCache() error
2023-03-28 18:00:06 +08:00
}
func NewINginxService() INginxService {
return &NginxService{}
}
func (n NginxService) GetNginxConfig() (*response.NginxFile, error) {
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
2022-11-23 16:19:05 +08:00
if err != nil {
return nil, err
2022-11-23 16:19:05 +08:00
}
configPath := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, "conf", "nginx.conf")
byteContent, err := files.NewFileOp().GetContent(configPath)
2022-11-23 16:19:05 +08:00
if err != nil {
return nil, err
2022-11-23 16:19:05 +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) {
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
}
defer res.Body.Close()
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()
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 {
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"))
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
}
}
oldContent, err := os.ReadFile(filePath)
2022-12-09 17:16:07 +08:00
if err != nil {
return err
}
if err = fileOp.WriteFile(filePath, strings.NewReader(req.Content), 0644); err != nil {
2022-12-09 17:16:07 +08:00
return err
}
return nginxCheckAndReload(string(oldContent), filePath, nginxInstall.ContainerName)
2022-12-09 17:16:07 +08:00
}
func (n NginxService) ClearProxyCache() error {
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
if err != nil {
return err
}
cacheDir := path.Join(nginxInstall.GetPath(), "www/common/proxy/proxy_cache_dir")
fileOp := files.NewFileOp()
if fileOp.Stat(cacheDir) {
if err = fileOp.CleanDir(cacheDir); err != nil {
return err
}
_, err = compose.Restart(nginxInstall.GetComposePath())
if err != nil {
return err
}
}
return nil
}