|
@@ -2,10 +2,9 @@ package api
|
|
|
|
|
|
import (
|
|
import (
|
|
"github.com/0xJacky/Nginx-UI/server/model"
|
|
"github.com/0xJacky/Nginx-UI/server/model"
|
|
- "github.com/0xJacky/Nginx-UI/server/tool"
|
|
|
|
- "github.com/0xJacky/Nginx-UI/server/tool/nginx"
|
|
|
|
|
|
+ "github.com/0xJacky/Nginx-UI/server/pkg/config_list"
|
|
|
|
+ nginx2 "github.com/0xJacky/Nginx-UI/server/pkg/nginx"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin"
|
|
- "io/ioutil"
|
|
|
|
"net/http"
|
|
"net/http"
|
|
"os"
|
|
"os"
|
|
"path/filepath"
|
|
"path/filepath"
|
|
@@ -22,14 +21,14 @@ func GetDomains(c *gin.Context) {
|
|
"modify": "time",
|
|
"modify": "time",
|
|
}
|
|
}
|
|
|
|
|
|
- configFiles, err := ioutil.ReadDir(nginx.GetNginxConfPath("sites-available"))
|
|
|
|
|
|
+ configFiles, err := os.ReadDir(nginx2.GetNginxConfPath("sites-available"))
|
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
ErrHandler(c, err)
|
|
ErrHandler(c, err)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- enabledConfig, err := ioutil.ReadDir(filepath.Join(nginx.GetNginxConfPath("sites-enabled")))
|
|
|
|
|
|
+ enabledConfig, err := os.ReadDir(filepath.Join(nginx2.GetNginxConfPath("sites-enabled")))
|
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
ErrHandler(c, err)
|
|
ErrHandler(c, err)
|
|
@@ -45,17 +44,18 @@ func GetDomains(c *gin.Context) {
|
|
|
|
|
|
for i := range configFiles {
|
|
for i := range configFiles {
|
|
file := configFiles[i]
|
|
file := configFiles[i]
|
|
|
|
+ fileInfo, _ := file.Info()
|
|
if !file.IsDir() {
|
|
if !file.IsDir() {
|
|
configs = append(configs, gin.H{
|
|
configs = append(configs, gin.H{
|
|
"name": file.Name(),
|
|
"name": file.Name(),
|
|
- "size": file.Size(),
|
|
|
|
- "modify": file.ModTime(),
|
|
|
|
|
|
+ "size": fileInfo.Size(),
|
|
|
|
+ "modify": fileInfo.ModTime(),
|
|
"enabled": enabledConfigMap[file.Name()],
|
|
"enabled": enabledConfigMap[file.Name()],
|
|
})
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- configs = tool.Sort(orderBy, sort, mySort[orderBy], configs)
|
|
|
|
|
|
+ configs = config_list.Sort(orderBy, sort, mySort[orderBy], configs)
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"data": configs,
|
|
"data": configs,
|
|
@@ -64,14 +64,14 @@ func GetDomains(c *gin.Context) {
|
|
|
|
|
|
func GetDomain(c *gin.Context) {
|
|
func GetDomain(c *gin.Context) {
|
|
name := c.Param("name")
|
|
name := c.Param("name")
|
|
- path := filepath.Join(nginx.GetNginxConfPath("sites-available"), name)
|
|
|
|
|
|
+ path := filepath.Join(nginx2.GetNginxConfPath("sites-available"), name)
|
|
|
|
|
|
enabled := true
|
|
enabled := true
|
|
- if _, err := os.Stat(filepath.Join(nginx.GetNginxConfPath("sites-enabled"), name)); os.IsNotExist(err) {
|
|
|
|
|
|
+ if _, err := os.Stat(filepath.Join(nginx2.GetNginxConfPath("sites-enabled"), name)); os.IsNotExist(err) {
|
|
enabled = false
|
|
enabled = false
|
|
}
|
|
}
|
|
|
|
|
|
- config, err := nginx.ParseNgxConfig(path)
|
|
|
|
|
|
+ config, err := nginx2.ParseNgxConfig(path)
|
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
ErrHandler(c, err)
|
|
ErrHandler(c, err)
|
|
@@ -95,18 +95,18 @@ func EditDomain(c *gin.Context) {
|
|
name := c.Param("name")
|
|
name := c.Param("name")
|
|
request := make(gin.H)
|
|
request := make(gin.H)
|
|
err = c.BindJSON(&request)
|
|
err = c.BindJSON(&request)
|
|
- path := filepath.Join(nginx.GetNginxConfPath("sites-available"), name)
|
|
|
|
|
|
+ path := filepath.Join(nginx2.GetNginxConfPath("sites-available"), name)
|
|
|
|
|
|
- err = ioutil.WriteFile(path, []byte(request["content"].(string)), 0644)
|
|
|
|
|
|
+ err = os.WriteFile(path, []byte(request["content"].(string)), 0644)
|
|
if err != nil {
|
|
if err != nil {
|
|
ErrHandler(c, err)
|
|
ErrHandler(c, err)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- enabledConfigFilePath := filepath.Join(nginx.GetNginxConfPath("sites-enabled"), name)
|
|
|
|
|
|
+ enabledConfigFilePath := filepath.Join(nginx2.GetNginxConfPath("sites-enabled"), name)
|
|
if _, err = os.Stat(enabledConfigFilePath); err == nil {
|
|
if _, err = os.Stat(enabledConfigFilePath); err == nil {
|
|
// Test nginx configuration
|
|
// Test nginx configuration
|
|
- err = nginx.TestNginxConf()
|
|
|
|
|
|
+ err = nginx2.TestNginxConf()
|
|
if err != nil {
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"message": err.Error(),
|
|
"message": err.Error(),
|
|
@@ -114,7 +114,7 @@ func EditDomain(c *gin.Context) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- output := nginx.ReloadNginx()
|
|
|
|
|
|
+ output := nginx2.ReloadNginx()
|
|
|
|
|
|
if output != "" && strings.Contains(output, "error") {
|
|
if output != "" && strings.Contains(output, "error") {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
@@ -128,8 +128,8 @@ func EditDomain(c *gin.Context) {
|
|
}
|
|
}
|
|
|
|
|
|
func EnableDomain(c *gin.Context) {
|
|
func EnableDomain(c *gin.Context) {
|
|
- configFilePath := filepath.Join(nginx.GetNginxConfPath("sites-available"), c.Param("name"))
|
|
|
|
- enabledConfigFilePath := filepath.Join(nginx.GetNginxConfPath("sites-enabled"), c.Param("name"))
|
|
|
|
|
|
+ configFilePath := filepath.Join(nginx2.GetNginxConfPath("sites-available"), c.Param("name"))
|
|
|
|
+ enabledConfigFilePath := filepath.Join(nginx2.GetNginxConfPath("sites-enabled"), c.Param("name"))
|
|
|
|
|
|
_, err := os.Stat(configFilePath)
|
|
_, err := os.Stat(configFilePath)
|
|
|
|
|
|
@@ -148,7 +148,7 @@ func EnableDomain(c *gin.Context) {
|
|
}
|
|
}
|
|
|
|
|
|
// Test nginx config, if not pass then rollback.
|
|
// Test nginx config, if not pass then rollback.
|
|
- err = nginx.TestNginxConf()
|
|
|
|
|
|
+ err = nginx2.TestNginxConf()
|
|
if err != nil {
|
|
if err != nil {
|
|
_ = os.Remove(enabledConfigFilePath)
|
|
_ = os.Remove(enabledConfigFilePath)
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
@@ -157,7 +157,7 @@ func EnableDomain(c *gin.Context) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- output := nginx.ReloadNginx()
|
|
|
|
|
|
+ output := nginx2.ReloadNginx()
|
|
|
|
|
|
if output != "" && strings.Contains(output, "error") {
|
|
if output != "" && strings.Contains(output, "error") {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
@@ -172,7 +172,7 @@ func EnableDomain(c *gin.Context) {
|
|
}
|
|
}
|
|
|
|
|
|
func DisableDomain(c *gin.Context) {
|
|
func DisableDomain(c *gin.Context) {
|
|
- enabledConfigFilePath := filepath.Join(nginx.GetNginxConfPath("sites-enabled"), c.Param("name"))
|
|
|
|
|
|
+ enabledConfigFilePath := filepath.Join(nginx2.GetNginxConfPath("sites-enabled"), c.Param("name"))
|
|
|
|
|
|
_, err := os.Stat(enabledConfigFilePath)
|
|
_, err := os.Stat(enabledConfigFilePath)
|
|
|
|
|
|
@@ -196,7 +196,7 @@ func DisableDomain(c *gin.Context) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- output := nginx.ReloadNginx()
|
|
|
|
|
|
+ output := nginx2.ReloadNginx()
|
|
|
|
|
|
if output != "" {
|
|
if output != "" {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
@@ -213,8 +213,8 @@ func DisableDomain(c *gin.Context) {
|
|
func DeleteDomain(c *gin.Context) {
|
|
func DeleteDomain(c *gin.Context) {
|
|
var err error
|
|
var err error
|
|
name := c.Param("name")
|
|
name := c.Param("name")
|
|
- availablePath := filepath.Join(nginx.GetNginxConfPath("sites-available"), name)
|
|
|
|
- enabledPath := filepath.Join(nginx.GetNginxConfPath("sites-enabled"), name)
|
|
|
|
|
|
+ availablePath := filepath.Join(nginx2.GetNginxConfPath("sites-available"), name)
|
|
|
|
+ enabledPath := filepath.Join(nginx2.GetNginxConfPath("sites-enabled"), name)
|
|
|
|
|
|
if _, err = os.Stat(availablePath); os.IsNotExist(err) {
|
|
if _, err = os.Stat(availablePath); os.IsNotExist(err) {
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
c.JSON(http.StatusNotFound, gin.H{
|