| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 | package apiimport (	"github.com/0xJacky/Nginx-UI/server/tool"	"github.com/gin-gonic/gin"	"io/ioutil"	"log"	"net/http"	"os"	"path/filepath")func GetConfigs(c *gin.Context) {	orderBy := c.Query("order_by")	sort := c.DefaultQuery("sort", "desc")	mySort := map[string]string{		"name":   "string",		"modify": "time",	}	configFiles, err := ioutil.ReadDir(tool.GetNginxConfPath("/"))	if err != nil {		ErrHandler(c, err)		return	}	var configs []gin.H	for i := range configFiles {		file := configFiles[i]		if !file.IsDir() && "." != file.Name()[0:1] {			configs = append(configs, gin.H{				"name":   file.Name(),				"size":   file.Size(),				"modify": file.ModTime(),			})		}	}	configs = tool.Sort(orderBy, sort, mySort[orderBy], configs)	c.JSON(http.StatusOK, gin.H{		"configs": configs,	})}func GetConfig(c *gin.Context) {	name := c.Param("name")	path := filepath.Join(tool.GetNginxConfPath("/"), name)	content, err := ioutil.ReadFile(path)	if err != nil {		ErrHandler(c, err)		return	}	c.JSON(http.StatusOK, gin.H{		"config": string(content),	})}type AddConfigJson struct {	Name    string `json:"name" binding:"required"`	Content string `json:"content" binding:"required"`}func AddConfig(c *gin.Context) {	var request AddConfigJson	err := c.BindJSON(&request)	if err != nil {		ErrHandler(c, err)		return	}	name := request.Name	content := request.Content	path := filepath.Join(tool.GetNginxConfPath("/"), name)	log.Println(path)	if _, err = os.Stat(path); err == nil {		c.JSON(http.StatusNotAcceptable, gin.H{			"message": "config exist",		})		return	}	if content != "" {		err := ioutil.WriteFile(path, []byte(content), 0644)		if err != nil {			ErrHandler(c, err)			return		}	}	output := tool.ReloadNginx()	if output != "" {		c.JSON(http.StatusInternalServerError, gin.H{			"message": output,		})		return	}	c.JSON(http.StatusOK, gin.H{		"name":    name,		"content": content,	})}type EditConfigJson struct {	Content string `json:"content" binding:"required"`}func EditConfig(c *gin.Context) {	name := c.Param("name")	var request EditConfigJson	err := c.BindJSON(&request)	if err != nil {		ErrHandler(c, err)		return	}	path := filepath.Join(tool.GetNginxConfPath("/"), name)	content := request.Content	origContent, err := ioutil.ReadFile(path)	if err != nil {		ErrHandler(c, err)		return	}	if content != "" && content != string(origContent) {		// model.CreateBackup(path)		err := ioutil.WriteFile(path, []byte(content), 0644)		if err != nil {			ErrHandler(c, err)			return		}	}	output := tool.ReloadNginx()	if output != "" {		c.JSON(http.StatusInternalServerError, gin.H{			"message": output,		})		return	}	GetConfig(c)}
 |