config.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package api
  2. import (
  3. "github.com/0xJacky/Nginx-UI/server/tool"
  4. "github.com/gin-gonic/gin"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. "os"
  9. "path/filepath"
  10. )
  11. func GetConfigs(c *gin.Context) {
  12. orderBy := c.Query("order_by")
  13. sort := c.DefaultQuery("sort", "desc")
  14. mySort := map[string]string{
  15. "name": "string",
  16. "modify": "time",
  17. }
  18. configFiles, err := ioutil.ReadDir(tool.GetNginxConfPath("/"))
  19. if err != nil {
  20. ErrHandler(c, err)
  21. return
  22. }
  23. var configs []gin.H
  24. for i := range configFiles {
  25. file := configFiles[i]
  26. if !file.IsDir() && "." != file.Name()[0:1] {
  27. configs = append(configs, gin.H{
  28. "name": file.Name(),
  29. "size": file.Size(),
  30. "modify": file.ModTime(),
  31. })
  32. }
  33. }
  34. configs = tool.Sort(orderBy, sort, mySort[orderBy], configs)
  35. c.JSON(http.StatusOK, gin.H{
  36. "configs": configs,
  37. })
  38. }
  39. func GetConfig(c *gin.Context) {
  40. name := c.Param("name")
  41. path := filepath.Join(tool.GetNginxConfPath("/"), name)
  42. content, err := ioutil.ReadFile(path)
  43. if err != nil {
  44. ErrHandler(c, err)
  45. return
  46. }
  47. c.JSON(http.StatusOK, gin.H{
  48. "config": string(content),
  49. })
  50. }
  51. type AddConfigJson struct {
  52. Name string `json:"name" binding:"required"`
  53. Content string `json:"content" binding:"required"`
  54. }
  55. func AddConfig(c *gin.Context) {
  56. var request AddConfigJson
  57. err := c.BindJSON(&request)
  58. if err != nil {
  59. ErrHandler(c, err)
  60. return
  61. }
  62. name := request.Name
  63. content := request.Content
  64. path := filepath.Join(tool.GetNginxConfPath("/"), name)
  65. log.Println(path)
  66. if _, err = os.Stat(path); err == nil {
  67. c.JSON(http.StatusNotAcceptable, gin.H{
  68. "message": "config exist",
  69. })
  70. return
  71. }
  72. if content != "" {
  73. err := ioutil.WriteFile(path, []byte(content), 0644)
  74. if err != nil {
  75. ErrHandler(c, err)
  76. return
  77. }
  78. }
  79. output := tool.ReloadNginx()
  80. if output != "" {
  81. c.JSON(http.StatusInternalServerError, gin.H{
  82. "message": output,
  83. })
  84. return
  85. }
  86. c.JSON(http.StatusOK, gin.H{
  87. "name": name,
  88. "content": content,
  89. })
  90. }
  91. type EditConfigJson struct {
  92. Content string `json:"content" binding:"required"`
  93. }
  94. func EditConfig(c *gin.Context) {
  95. name := c.Param("name")
  96. var request EditConfigJson
  97. err := c.BindJSON(&request)
  98. if err != nil {
  99. ErrHandler(c, err)
  100. return
  101. }
  102. path := filepath.Join(tool.GetNginxConfPath("/"), name)
  103. content := request.Content
  104. origContent, err := ioutil.ReadFile(path)
  105. if err != nil {
  106. ErrHandler(c, err)
  107. return
  108. }
  109. if content != "" && content != string(origContent) {
  110. // model.CreateBackup(path)
  111. err := ioutil.WriteFile(path, []byte(content), 0644)
  112. if err != nil {
  113. ErrHandler(c, err)
  114. return
  115. }
  116. }
  117. output := tool.ReloadNginx()
  118. if output != "" {
  119. c.JSON(http.StatusInternalServerError, gin.H{
  120. "message": output,
  121. })
  122. return
  123. }
  124. GetConfig(c)
  125. }