server.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package nginx_controller
  2. import (
  3. "encoding/json"
  4. "github.com/astaxie/beego/orm"
  5. "nginx-ui/server/models"
  6. nginx2 "nginx-ui/server/nginx"
  7. )
  8. type ServerController struct {
  9. BaseController
  10. }
  11. // Get getAllServers
  12. // Get /nginx/:id/server
  13. func (c *ServerController) Get() {
  14. nginx, err := c.CheckNginxPermission()
  15. if err != nil {
  16. return
  17. }
  18. id, err := c.GetInt("id", 0)
  19. if err != nil {
  20. c.ErrorJson(err)
  21. return
  22. }
  23. o := orm.NewOrm()
  24. server := models.ServerHost{Id: id, NginxId: nginx.Id}
  25. err = o.Read(&server, "Id", "NginxId")
  26. if err != nil {
  27. c.ErrorJson(err)
  28. return
  29. }
  30. c.SetData(server).Json()
  31. }
  32. // Post add or update nginx instance
  33. // POST /nginx/:id/server
  34. func (c *ServerController) Post() {
  35. nginx, err := c.CheckNginxPermission()
  36. if err != nil {
  37. return
  38. }
  39. var server models.ServerHost
  40. err = json.Unmarshal(c.Ctx.Input.RequestBody, &server)
  41. if err != nil {
  42. c.ErrorJson(err)
  43. return
  44. }
  45. o := orm.NewOrm()
  46. var saveErr error
  47. server.NginxId = nginx.Id
  48. server.Uid = nginx.Uid
  49. if server.Id > 0 {
  50. tmp := models.ServerHost{Id: server.Id, NginxId: nginx.Id}
  51. err := o.Read(&tmp, "Id", "NginxId")
  52. if err == nil {
  53. server.LastName = tmp.LastName
  54. server.ServerConf = tmp.ServerConf
  55. }
  56. _, saveErr = o.Update(&server)
  57. } else {
  58. _, saveErr = o.Insert(&server)
  59. }
  60. if saveErr != nil {
  61. c.ErrorJson(saveErr)
  62. } else {
  63. c.SetData(server).Json()
  64. }
  65. }
  66. // Delete add or update nginx instance
  67. // DELETE /nginx/:id/server
  68. func (c *ServerController) Delete() {
  69. nginx, err := c.CheckNginxPermission()
  70. if err != nil {
  71. return
  72. }
  73. var server models.ServerHost
  74. err = json.Unmarshal(c.Ctx.Input.RequestBody, &server)
  75. if err != nil {
  76. c.ErrorJson(err)
  77. return
  78. }
  79. o := orm.NewOrm()
  80. err = o.Read(&server)
  81. if err != nil {
  82. c.ErrorJson(err)
  83. return
  84. }
  85. ins := nginx2.GetInstance(nginx)
  86. server.Enable = false
  87. err = ins.RefreshServer(server)
  88. if err != nil {
  89. c.ErrorJson(err)
  90. return
  91. }
  92. delServer := models.ServerHost{Id: server.Id, NginxId: nginx.Id}
  93. _, err = o.Delete(&delServer, "Id", "NginxId")
  94. if err != nil {
  95. c.ErrorJson(err)
  96. return
  97. }
  98. c.SetData("success").Json()
  99. }
  100. // Refresh check and refresh to disk
  101. // POST /nginx/:id/server/refresh
  102. func (c *ServerController) Refresh() {
  103. nginx, err := c.CheckNginxPermission()
  104. if err != nil {
  105. return
  106. }
  107. var postData models.ServerHost
  108. err = json.Unmarshal(c.Ctx.Input.RequestBody, &postData)
  109. if err != nil {
  110. c.ErrorJson(err)
  111. return
  112. }
  113. postData.NginxId = nginx.Id
  114. postData.Uid = nginx.Uid
  115. o := orm.NewOrm()
  116. _, err = o.Update(&postData)
  117. if err != nil {
  118. c.ErrorJson(err)
  119. return
  120. }
  121. ins := nginx2.GetInstance(nginx)
  122. err = ins.RefreshServer(postData)
  123. if err != nil {
  124. c.ErrorJson(err)
  125. return
  126. }
  127. postData.LastName = postData.Name
  128. _, _ = o.Update(&postData)
  129. c.SetData(true).Json()
  130. }