config.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package api
  2. import (
  3. "github.com/0xJacky/Nginx-UI/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. ErrorHandler(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. ErrorHandler(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. ErrorHandler(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. ErrorHandler(c, err)
  76. return
  77. }
  78. }
  79. tool.ReloadNginx()
  80. c.JSON(http.StatusOK, gin.H{
  81. "name": name,
  82. "content": content,
  83. })
  84. }
  85. type EditConfigJson struct {
  86. Content string `json:"content" binding:"required"`
  87. }
  88. func EditConfig(c *gin.Context) {
  89. name := c.Param("name")
  90. var request EditConfigJson
  91. err := c.BindJSON(&request)
  92. if err != nil {
  93. ErrorHandler(c, err)
  94. return
  95. }
  96. path := filepath.Join(tool.GetNginxConfPath("/"), name)
  97. content := request.Content
  98. origContent, err := ioutil.ReadFile(path)
  99. if err != nil {
  100. ErrorHandler(c, err)
  101. return
  102. }
  103. if content != "" && content != string(origContent) {
  104. // model.CreateBackup(path)
  105. err := ioutil.WriteFile(path, []byte(content), 0644)
  106. if err != nil {
  107. ErrorHandler(c, err)
  108. return
  109. }
  110. }
  111. tool.ReloadNginx()
  112. GetConfig(c)
  113. }