domain.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package api
  2. import (
  3. "github.com/0xJacky/Nginx-UI/model"
  4. "github.com/0xJacky/Nginx-UI/tool"
  5. "github.com/gin-gonic/gin"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "os"
  10. "path/filepath"
  11. "strconv"
  12. "strings"
  13. )
  14. func GetDomains(c *gin.Context) {
  15. configFiles, err := ioutil.ReadDir(tool.GetNginxConfPath("sites-available"))
  16. if err != nil {
  17. log.Println(err)
  18. }
  19. enabledConfig, err := ioutil.ReadDir(filepath.Join(tool.GetNginxConfPath("sites-enabled")))
  20. enabledConfigMap := make(map[string]bool)
  21. for i := range enabledConfig {
  22. enabledConfigMap[enabledConfig[i].Name()] = true
  23. }
  24. if err != nil {
  25. log.Println(err)
  26. }
  27. var configs []gin.H
  28. for i := range configFiles {
  29. file := configFiles[i]
  30. if !file.IsDir() {
  31. configs = append(configs, gin.H{
  32. "name": file.Name(),
  33. "size": file.Size(),
  34. "modify": file.ModTime(),
  35. "enabled": enabledConfigMap[file.Name()],
  36. })
  37. }
  38. }
  39. c.JSON(http.StatusOK, gin.H{
  40. "configs": configs,
  41. })
  42. }
  43. func GetDomain(c *gin.Context) {
  44. name := c.Param("name")
  45. path := filepath.Join(tool.GetNginxConfPath("sites-available"), name)
  46. enabled := true
  47. if _, err := os.Stat(filepath.Join(tool.GetNginxConfPath("sites-enabled"), name)); os.IsNotExist(err) {
  48. enabled = false
  49. }
  50. content, err := ioutil.ReadFile(path)
  51. if err != nil {
  52. if os.IsNotExist(err) {
  53. c.JSON(http.StatusNotFound, gin.H{
  54. "message": err.Error(),
  55. })
  56. }
  57. log.Println(err)
  58. return
  59. }
  60. c.JSON(http.StatusOK, gin.H{
  61. "enabled": enabled,
  62. "config": string(content),
  63. })
  64. }
  65. func AddDomain(c *gin.Context) {
  66. name := c.PostForm("name")
  67. SupportSSL := c.PostForm("support_ssl")
  68. baseKeys := []string{"http_listen_port",
  69. "https_listen_port",
  70. "server_name",
  71. "ssl_certificate", "ssl_certificate_key",
  72. "root", "extra",
  73. }
  74. tmp, err := ioutil.ReadFile("template/http-conf")
  75. log.Println(SupportSSL)
  76. if SupportSSL == "true" {
  77. tmp, err = ioutil.ReadFile("template/https-conf")
  78. }
  79. if err != nil {
  80. log.Println(err)
  81. c.JSON(http.StatusInternalServerError, gin.H{
  82. "message": "server error",
  83. })
  84. return
  85. }
  86. template := string(tmp)
  87. content := template
  88. for i := range baseKeys {
  89. content = strings.Replace(content, "{{ " + baseKeys[i] +" }}",
  90. c.PostForm(baseKeys[i]),
  91. -1)
  92. }
  93. log.Println(name, content)
  94. c.JSON(http.StatusOK, gin.H{
  95. "name": name,
  96. "content": content,
  97. })
  98. }
  99. func EditDomain(c *gin.Context) {
  100. name := c.Param("name")
  101. content := c.PostForm("content")
  102. path := filepath.Join(tool.GetNginxConfPath("sites-available"), name)
  103. s, err := strconv.Unquote(`"` + content + `"`)
  104. if err != nil {
  105. log.Println(err)
  106. }
  107. origContent, err := ioutil.ReadFile(path)
  108. if err != nil {
  109. log.Println(err)
  110. }
  111. if s != "" && s != string(origContent) {
  112. model.CreateBackup(path)
  113. err := ioutil.WriteFile(path, []byte(s), 0644)
  114. if err != nil {
  115. log.Println(err)
  116. }
  117. }
  118. if _, err := os.Stat(filepath.Join(tool.GetNginxConfPath("sites-enabled"), name)); os.IsExist(err) {
  119. tool.ReloadNginx()
  120. }
  121. GetDomain(c)
  122. }
  123. func EnableDomain(c *gin.Context) {
  124. configFilePath := filepath.Join(tool.GetNginxConfPath("sites-available"), c.Param("name"))
  125. enabledConfigFilePath := filepath.Join(tool.GetNginxConfPath("sites-enabled"), c.Param("name"))
  126. _, err := os.Stat(configFilePath)
  127. if err != nil {
  128. log.Println(err)
  129. }
  130. err = os.Symlink(configFilePath, enabledConfigFilePath)
  131. if err != nil {
  132. log.Println(err)
  133. }
  134. tool.ReloadNginx()
  135. c.JSON(http.StatusOK, gin.H{
  136. "message": "ok",
  137. })
  138. }
  139. func DisableDomain(c *gin.Context) {
  140. enabledConfigFilePath := filepath.Join(tool.GetNginxConfPath("sites-enabled"), c.Param("name"))
  141. _, err := os.Stat(enabledConfigFilePath)
  142. if err != nil {
  143. log.Println(err)
  144. }
  145. err = os.Remove(enabledConfigFilePath)
  146. if err != nil {
  147. log.Println(err)
  148. }
  149. tool.ReloadNginx()
  150. c.JSON(http.StatusOK, gin.H{
  151. "message": "ok",
  152. })
  153. }
  154. func DeleteDomain(c *gin.Context) {
  155. }