nginx.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package nginx_service
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "github.com/astaxie/beego/logs"
  6. "github.com/astaxie/beego/orm"
  7. "nginx-ui/server/config"
  8. "nginx-ui/server/models"
  9. ngx "nginx-ui/server/nginx"
  10. "strconv"
  11. )
  12. type NginxService struct {
  13. }
  14. // CheckNginxPermission 从path中获取nginx的参数
  15. func (c *NginxService) CheckNginxPermission(user *models.User, nginxId string) (*models.Nginx, error) {
  16. id, err := strconv.Atoi(nginxId)
  17. if err != nil {
  18. logs.Warn("strconv.Atoi(idStr) fail", nginxId)
  19. return nil, errors.New("请传递正确的参数!")
  20. }
  21. return c.CheckNginxPermissionById(user, id)
  22. }
  23. // CheckNginxPermissionById 验证权限,如果无权操作该nginx,返回nil,否则返回
  24. func (c *NginxService) CheckNginxPermissionById(current *models.User, nginxId int) (*models.Nginx, error) {
  25. if nginxId < 1 {
  26. return nil, errors.New("nginx ID must gt 0!")
  27. }
  28. nginx := models.Nginx{Id: nginxId}
  29. o := orm.NewOrm()
  30. err := o.Read(&nginx)
  31. if err != nil {
  32. return nil, err
  33. }
  34. if !current.IsAdmin() && current.Account != nginx.Uid {
  35. return nil, errors.New("您无权操作该实例")
  36. }
  37. return &nginx, nil
  38. }
  39. // Get getAll,
  40. // 管理员获取全部,非管理员或者自己名下的
  41. func (c *NginxService) ListNginx(current *models.User) *models.RespData {
  42. if current == nil {
  43. return models.UnAuthResp
  44. }
  45. o := orm.NewOrm()
  46. qs := o.QueryTable("nginx")
  47. if !current.IsAdmin() {
  48. qs = qs.Filter("Uid", current.Account)
  49. }
  50. var list []*models.Nginx
  51. _, err := qs.All(&list)
  52. for i := range list {
  53. item := list[i]
  54. if item.Password != "" {
  55. item.Password = config.ReplacePassword
  56. }
  57. }
  58. if err != nil {
  59. return models.NewErrorResp(err)
  60. } else {
  61. return models.SuccessResp(list)
  62. }
  63. }
  64. // Post add nginx instance
  65. func (c *NginxService) Add(current *models.User, req []byte) *models.RespData {
  66. if current == nil {
  67. return models.UnAuthResp
  68. }
  69. var nginx models.Nginx
  70. err := json.Unmarshal(req, &nginx)
  71. if err != nil {
  72. logs.Error(err, string(req))
  73. return models.NewErrorResp(err)
  74. }
  75. nginx.Check()
  76. o := orm.NewOrm()
  77. nginx.Uid = current.Account
  78. nginx.NginxPath = "/usr/sbin/nginx"
  79. nginx.NginxDir = "/etc/nginx"
  80. if nginx.IsLocal {
  81. nginx.IsServer = true
  82. }
  83. nginx.DataDir = "/app/data"
  84. _, err = o.Insert(&nginx)
  85. if err != nil {
  86. return models.NewErrorResp(err)
  87. }
  88. logs.Info("post", nginx)
  89. instance := ngx.GetInstance(&nginx)
  90. err = instance.Connect()
  91. if err != nil {
  92. return models.SuccessResp(nginx).SetCode(1).SetMsg(err.Error())
  93. }
  94. out, err := instance.GetVersion()
  95. if err != nil {
  96. return models.SuccessResp(nginx).SetCode(1).SetMsg(err.Error())
  97. }
  98. nginx.VersionInfo = out
  99. _, _ = o.Update(&nginx, "VersionInfo")
  100. return models.SuccessResp(nginx)
  101. }
  102. // Update modify nginx instance
  103. // post /nginx/:id
  104. func (c *NginxService) Update(nginxId string, current *models.User, req []byte) *models.RespData {
  105. if current == nil {
  106. return models.UnAuthResp
  107. }
  108. var nginx models.Nginx
  109. err := json.Unmarshal(req, &nginx)
  110. if err != nil {
  111. logs.Error(err, string(req))
  112. return models.NewErrorResp(err)
  113. }
  114. exist, err := c.CheckNginxPermission(current, nginxId)
  115. if err != nil {
  116. return models.NewErrorResp(err)
  117. }
  118. nginx.Id = exist.Id
  119. nginx.Uid = exist.Uid
  120. nginx.Check()
  121. o := orm.NewOrm()
  122. if nginx.Password == config.ReplacePassword {
  123. nginx.Password = exist.Password
  124. }
  125. nginx.HttpConf = exist.HttpConf
  126. _, err = o.Update(&nginx)
  127. if err != nil {
  128. return models.NewErrorResp(err)
  129. }
  130. logs.Info("post", nginx)
  131. instance := ngx.GetInstance(&nginx)
  132. err = instance.Connect()
  133. if err != nil {
  134. return models.NewResp(1, err.Error(), nginx)
  135. }
  136. out, err := instance.GetVersion()
  137. if err != nil {
  138. return models.NewResp(1, err.Error(), nginx)
  139. }
  140. nginx.VersionInfo = out
  141. _, _ = o.Update(&nginx, "VersionInfo")
  142. return models.SuccessResp(nginx)
  143. }
  144. // StartNginx startNginx
  145. // post /nginx/:id/start
  146. func (c *NginxService) StartNginx(nginxId string, user *models.User) *models.RespData {
  147. if user == nil {
  148. return models.UnAuthResp
  149. }
  150. nginx, err := c.CheckNginxPermission(user, nginxId)
  151. if err != nil {
  152. return models.NewErrorResp(err)
  153. }
  154. instance := ngx.GetInstance(nginx)
  155. err = instance.Start()
  156. isRun, msg := instance.Status()
  157. return models.SuccessResp(isRun).SetMsg(msg)
  158. }
  159. // StopNginx add nginx instance
  160. // post /nginx/:id/stop
  161. func (c *NginxService) StopNginx(nginxId string, user *models.User) *models.RespData {
  162. if user == nil {
  163. return models.UnAuthResp
  164. }
  165. nginx, err := c.CheckNginxPermission(user, nginxId)
  166. if err != nil {
  167. return models.NewErrorResp(err)
  168. }
  169. instance := ngx.GetInstance(nginx)
  170. err = instance.Stop()
  171. isRun, msg := instance.Status()
  172. return models.SuccessResp(isRun).SetMsg(msg)
  173. }
  174. // RefreshHttp nginx detail data
  175. // post /nginx/:id/http/refresh
  176. func (c *NginxService) RefreshHttp(nginxId string, user *models.User, req []byte) *models.RespData {
  177. if user == nil {
  178. return models.UnAuthResp
  179. }
  180. exist, err := c.CheckNginxPermission(user, nginxId)
  181. if err != nil {
  182. return models.NewErrorResp(err)
  183. }
  184. var nginx models.Nginx
  185. err = json.Unmarshal(req, &nginx)
  186. if err != nil {
  187. logs.Error(err, string(req))
  188. return models.NewErrorResp(err)
  189. }
  190. logs.Info("id", nginx)
  191. o := orm.NewOrm()
  192. if nginx.HttpConf != "" {
  193. _, err = o.Update(&nginx, "HttpConf", "HttpData")
  194. if err != nil {
  195. return models.NewErrorResp(err)
  196. }
  197. exist.HttpConf = nginx.HttpConf
  198. exist.HttpData = nginx.HttpData
  199. }
  200. ins := ngx.GetInstance(exist)
  201. err = ins.RefreshHttp(*exist)
  202. if err != nil {
  203. return models.NewErrorResp(err)
  204. }
  205. return models.SuccessResp(nil)
  206. }
  207. // GetNginx nginx detail data
  208. // get /nginx/:id
  209. func (c *NginxService) GetNginx(nginxId string, user *models.User) *models.RespData {
  210. if user == nil {
  211. return models.UnAuthResp
  212. }
  213. nginx, err := c.CheckNginxPermission(user, nginxId)
  214. if err != nil {
  215. return models.NewErrorResp(err)
  216. }
  217. if nginx.Password != "" {
  218. nginx.Password = config.ReplacePassword
  219. }
  220. var resp = map[string]interface{}{}
  221. resp["nginx"] = nginx
  222. o := orm.NewOrm()
  223. var servers []models.ServerHost
  224. _, err = o.QueryTable((*models.ServerHost)(nil)).Filter("NginxId", nginx.Id).All(&servers)
  225. if err != nil {
  226. return models.NewErrorResp(err)
  227. }
  228. resp["servers"] = servers
  229. return models.SuccessResp(resp)
  230. }
  231. // DelNginx delete a instance
  232. // delete /nginx/:id
  233. func (c *NginxService) DelNginx(nginxId string, user *models.User) *models.RespData {
  234. if user == nil {
  235. return models.UnAuthResp
  236. }
  237. nginx, err := c.CheckNginxPermission(user, nginxId)
  238. if err != nil {
  239. return models.NewErrorResp(err)
  240. }
  241. o := orm.NewOrm()
  242. count, err := o.Delete(nginx, "Id")
  243. if err != nil {
  244. return models.NewErrorResp(err)
  245. } else {
  246. return models.SuccessResp(count)
  247. }
  248. }
  249. // StatusNginx add nginx instance
  250. // post /nginx/:id/status
  251. func (c *NginxService) StatusNginx(nginxId string, user *models.User) *models.RespData {
  252. if user == nil {
  253. return models.UnAuthResp
  254. }
  255. nginx, err := c.CheckNginxPermission(user, nginxId)
  256. if err != nil {
  257. return models.NewErrorResp(err)
  258. }
  259. instance := ngx.GetInstance(nginx)
  260. isRun, msg := instance.Status()
  261. return models.SuccessResp(isRun).SetMsg(msg)
  262. }