nginx.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "github.com/astaxie/beego/logs"
  5. "github.com/astaxie/beego/orm"
  6. "nginx-ui/server/models"
  7. ngx "nginx-ui/server/nginx"
  8. )
  9. type NginxController struct {
  10. BaseController
  11. }
  12. const ReplacePassword = "******"
  13. // Get getAll,
  14. // 管理员获取全部,非管理员或者自己名下的
  15. func (c *NginxController) Get() {
  16. current := c.RequiredUser()
  17. if current == nil {
  18. return
  19. }
  20. o := orm.NewOrm()
  21. qs := o.QueryTable("nginx")
  22. if !current.IsAdmin() {
  23. qs = qs.Filter("Uid", current.Account)
  24. }
  25. var list []*models.Nginx
  26. _, err := qs.All(&list)
  27. for i := range list {
  28. item := list[i]
  29. if item.Password != "" {
  30. item.Password = ReplacePassword
  31. }
  32. }
  33. if err != nil {
  34. c.ErrorJson(err)
  35. return
  36. } else {
  37. c.setData(list).json()
  38. }
  39. }
  40. // Post add nginx instance
  41. func (c *NginxController) Post() {
  42. current := c.RequiredUser()
  43. if current == nil {
  44. return
  45. }
  46. var nginx models.Nginx
  47. err := json.Unmarshal(c.Ctx.Input.RequestBody, &nginx)
  48. if err != nil {
  49. logs.Error(err, string(c.Ctx.Input.RequestBody))
  50. c.ErrorJson(err)
  51. return
  52. }
  53. nginx.Check()
  54. o := orm.NewOrm()
  55. nginx.Uid = current.Account
  56. nginx.NginxPath = "/usr/sbin/nginx"
  57. nginx.NginxDir = "/etc/nginx"
  58. if nginx.IsLocal {
  59. nginx.IsServer = true
  60. }
  61. nginx.DataDir = "/app/data"
  62. _, err = o.Insert(&nginx)
  63. if err != nil {
  64. c.ErrorJson(err)
  65. return
  66. }
  67. logs.Info("post", nginx)
  68. instance := ngx.GetInstance(&nginx)
  69. err = instance.Connect()
  70. if err != nil {
  71. c.setCode(1).setMsg(err.Error()).setData(nginx)
  72. c.json()
  73. return
  74. }
  75. out, err := instance.GetVersion()
  76. if err != nil {
  77. c.setCode(1).setMsg(err.Error()).setData(nginx)
  78. c.json()
  79. return
  80. }
  81. nginx.VersionInfo = out
  82. _, _ = o.Update(&nginx, "VersionInfo")
  83. c.setData(nginx).json()
  84. }
  85. // Update modify nginx instance
  86. // post /nginx/:id
  87. func (c *NginxController) Update() {
  88. exist, err := c.CheckNginxPermission()
  89. if err != nil {
  90. return
  91. }
  92. var nginx models.Nginx
  93. err = json.Unmarshal(c.Ctx.Input.RequestBody, &nginx)
  94. if err != nil {
  95. logs.Error(err, string(c.Ctx.Input.RequestBody))
  96. c.ErrorJson(err)
  97. return
  98. }
  99. nginx.Id = exist.Id
  100. nginx.Uid = exist.Uid
  101. nginx.Check()
  102. o := orm.NewOrm()
  103. if nginx.Password == ReplacePassword {
  104. nginx.Password = exist.Password
  105. }
  106. nginx.HttpConf = exist.HttpConf
  107. _, err = o.Update(&nginx)
  108. if err != nil {
  109. c.ErrorJson(err)
  110. return
  111. }
  112. logs.Info("post", nginx)
  113. instance := ngx.GetInstance(&nginx)
  114. err = instance.Connect()
  115. if err != nil {
  116. c.setCode(1).setMsg(err.Error()).setData(nginx)
  117. c.json()
  118. return
  119. }
  120. out, err := instance.GetVersion()
  121. if err != nil {
  122. c.setCode(1).setMsg(err.Error()).setData(nginx)
  123. c.json()
  124. return
  125. }
  126. nginx.VersionInfo = out
  127. _, _ = o.Update(&nginx, "VersionInfo")
  128. c.setData(nginx).json()
  129. }
  130. // StartNginx startNginx
  131. // post /nginx/:id/start
  132. func (c *NginxController) StartNginx() {
  133. nginx, err := c.CheckNginxPermission()
  134. if err != nil {
  135. return
  136. }
  137. instance := ngx.GetInstance(nginx)
  138. err = instance.Start()
  139. isRun, msg := instance.Status()
  140. c.setData(isRun).setMsg(msg).json()
  141. }
  142. // StopNginx add nginx instance
  143. // post /nginx/:id/stop
  144. func (c *NginxController) StopNginx() {
  145. nginx, err := c.CheckNginxPermission()
  146. if err != nil {
  147. return
  148. }
  149. instance := ngx.GetInstance(nginx)
  150. err = instance.Stop()
  151. isRun, msg := instance.Status()
  152. c.setData(isRun).setMsg(msg).json()
  153. }
  154. // RefreshHttp nginx detail data
  155. // post /nginx/:id/http/refresh
  156. func (c *NginxController) RefreshHttp() {
  157. exist, err := c.CheckNginxPermission()
  158. if err != nil {
  159. return
  160. }
  161. var nginx models.Nginx
  162. err = json.Unmarshal(c.Ctx.Input.RequestBody, &nginx)
  163. if err != nil {
  164. logs.Error(err, string(c.Ctx.Input.RequestBody))
  165. c.ErrorJson(err)
  166. return
  167. }
  168. logs.Info("id", nginx)
  169. o := orm.NewOrm()
  170. if nginx.HttpConf != "" {
  171. _, err = o.Update(&nginx, "HttpConf", "HttpData")
  172. if err != nil {
  173. c.ErrorJson(err)
  174. return
  175. }
  176. exist.HttpConf = nginx.HttpConf
  177. exist.HttpData = nginx.HttpData
  178. }
  179. ins := ngx.GetInstance(exist)
  180. err = ins.RefreshHttp(*exist)
  181. if err != nil {
  182. c.ErrorJson(err)
  183. return
  184. }
  185. c.json()
  186. }
  187. // GetNginx nginx detail data
  188. // get /nginx/:id
  189. func (c *NginxController) GetNginx() {
  190. nginx, err := c.CheckNginxPermission()
  191. if err != nil {
  192. return
  193. }
  194. if nginx.Password != "" {
  195. nginx.Password = ReplacePassword
  196. }
  197. c.addRespData("nginx", nginx)
  198. o := orm.NewOrm()
  199. var servers []models.ServerHost
  200. _, err = o.QueryTable((*models.ServerHost)(nil)).Filter("NginxId", nginx.Id).All(&servers)
  201. if err != nil {
  202. c.ErrorJson(err)
  203. return
  204. }
  205. c.addRespData("servers", servers)
  206. c.json()
  207. }
  208. // DelNginx delete a instance
  209. // delete /nginx/:id
  210. func (c *NginxController) DelNginx() {
  211. nginx, err := c.CheckNginxPermission()
  212. if err != nil {
  213. return
  214. }
  215. o := orm.NewOrm()
  216. count, err := o.Delete(nginx, "Id")
  217. if err != nil {
  218. c.ErrorJson(err)
  219. } else {
  220. c.setData(count).json()
  221. }
  222. }
  223. // StatusNginx add nginx instance
  224. // post /nginx/:id/status
  225. func (c *NginxController) StatusNginx() {
  226. nginx, err := c.CheckNginxPermission()
  227. if err != nil {
  228. return
  229. }
  230. instance := ngx.GetInstance(nginx)
  231. isRun, msg := instance.Status()
  232. c.setData(isRun).setMsg(msg).json()
  233. }