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

104 lines
2.9 KiB
Go
Raw Normal View History

2022-11-23 08:19:05 +00:00
package service
import (
2022-12-13 09:20:13 +00:00
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
2022-12-13 10:54:46 +00:00
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
"io/ioutil"
"net/http"
2022-12-09 09:16:07 +00:00
"os"
2022-11-29 09:39:10 +00:00
"path"
"strings"
2022-12-09 09:16:07 +00:00
"time"
2022-11-29 09:39:10 +00:00
2022-11-23 08:19:05 +00: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 {
}
2022-12-14 11:39:32 +00:00
func (n NginxService) GetNginxConfig() (response.FileInfo, error) {
2022-12-13 12:03:54 +00:00
nginxInstall, err := getAppInstallByKey(constant.AppNginx)
2022-11-23 08:19:05 +00:00
if err != nil {
2022-12-14 11:39:32 +00:00
return response.FileInfo{}, err
2022-11-23 08:19:05 +00:00
}
2022-12-13 12:03:54 +00:00
configPath := path.Join(constant.AppInstallDir, constant.AppNginx, nginxInstall.Name, "conf", "nginx.conf")
2022-11-23 08:19:05 +00:00
info, err := files.NewFileInfo(files.FileOption{
Path: configPath,
Expand: true,
})
if err != nil {
2022-12-14 11:39:32 +00:00
return response.FileInfo{}, err
2022-11-23 08:19:05 +00:00
}
2022-12-14 11:39:32 +00:00
return response.FileInfo{FileInfo: *info}, nil
2022-11-23 08:19:05 +00:00
}
2022-11-24 02:28:39 +00:00
2022-12-13 10:54:46 +00:00
func (n NginxService) GetConfigByScope(req request.NginxScopeReq) ([]response.NginxParam, error) {
2022-11-24 02:28:39 +00:00
keys, ok := dto.ScopeKeyMap[req.Scope]
if !ok || len(keys) == 0 {
return nil, nil
}
2022-11-30 16:41:50 +00:00
return getNginxParamsByKeys(constant.NginxScopeHttp, keys, nil)
2022-11-24 02:28:39 +00:00
}
2022-12-13 10:54:46 +00:00
func (n NginxService) UpdateConfigByScope(req request.NginxConfigUpdate) error {
2022-11-24 02:28:39 +00:00
keys, ok := dto.ScopeKeyMap[req.Scope]
if !ok || len(keys) == 0 {
return nil
}
2022-11-30 16:41:50 +00:00
return updateNginxConfig(constant.NginxScopeHttp, getNginxParams(req.Params, keys), nil)
2022-11-24 02:28:39 +00:00
}
2022-11-24 08:06:18 +00:00
2022-12-13 10:54:46 +00:00
func (n NginxService) GetStatus() (response.NginxStatus, error) {
res, err := http.Get("http://127.0.0.1/nginx_status")
2022-11-24 08:06:18 +00:00
if err != nil {
2022-12-13 10:54:46 +00:00
return response.NginxStatus{}, err
2022-11-24 08:06:18 +00:00
}
content, err := ioutil.ReadAll(res.Body)
2022-11-24 08:06:18 +00:00
if err != nil {
2022-12-13 10:54:46 +00:00
return response.NginxStatus{}, err
2022-11-24 08:06:18 +00:00
}
2022-12-13 10:54:46 +00:00
var status response.NginxStatus
resArray := strings.Split(string(content), " ")
2022-11-24 08:06:18 +00: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 09:16:07 +00: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
}
2022-12-13 12:03:54 +00:00
nginxInstall, err := getAppInstallByKey(constant.AppNginx)
2022-12-09 09:16:07 +00:00
if err != nil {
return err
}
return nginxCheckAndReload(string(oldContent), req.FilePath, nginxInstall.ContainerName)
}