123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- package nginx_controller
- import (
- "encoding/json"
- "github.com/astaxie/beego/logs"
- "github.com/astaxie/beego/orm"
- "nginx-ui/server/config"
- "nginx-ui/server/models"
- ngx "nginx-ui/server/nginx"
- )
- type NginxController struct {
- BaseController
- }
- // Get getAll,
- // 管理员获取全部,非管理员或者自己名下的
- func (c *NginxController) Get() {
- current := c.RequiredUser()
- if current == nil {
- return
- }
- o := orm.NewOrm()
- qs := o.QueryTable("nginx")
- if !current.IsAdmin() {
- qs = qs.Filter("Uid", current.Account)
- }
- var list []*models.Nginx
- _, err := qs.All(&list)
- for i := range list {
- item := list[i]
- if item.Password != "" {
- item.Password = config.ReplacePassword
- }
- }
- if err != nil {
- c.ErrorJson(err)
- return
- } else {
- c.SetData(list).Json()
- }
- }
- // Post add nginx instance
- func (c *NginxController) Post() {
- current := c.RequiredUser()
- if current == nil {
- return
- }
- var nginx models.Nginx
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &nginx)
- if err != nil {
- logs.Error(err, string(c.Ctx.Input.RequestBody))
- c.ErrorJson(err)
- return
- }
- nginx.Check()
- o := orm.NewOrm()
- nginx.Uid = current.Account
- nginx.NginxPath = "/usr/sbin/nginx"
- nginx.NginxDir = "/etc/nginx"
- if nginx.IsLocal {
- nginx.IsServer = true
- }
- nginx.DataDir = "/app/data"
- _, err = o.Insert(&nginx)
- if err != nil {
- c.ErrorJson(err)
- return
- }
- logs.Info("post", nginx)
- instance := ngx.GetInstance(&nginx)
- err = instance.Connect()
- if err != nil {
- c.SetCode(1).SetMsg(err.Error()).SetData(nginx)
- c.Json()
- return
- }
- out, err := instance.GetVersion()
- if err != nil {
- c.SetCode(1).SetMsg(err.Error()).SetData(nginx)
- c.Json()
- return
- }
- nginx.VersionInfo = out
- _, _ = o.Update(&nginx, "VersionInfo")
- c.SetData(nginx).Json()
- }
- // Update modify nginx instance
- // post /nginx/:id
- func (c *NginxController) Update() {
- exist, err := c.CheckNginxPermission()
- if err != nil {
- return
- }
- var nginx models.Nginx
- err = json.Unmarshal(c.Ctx.Input.RequestBody, &nginx)
- if err != nil {
- logs.Error(err, string(c.Ctx.Input.RequestBody))
- c.ErrorJson(err)
- return
- }
- nginx.Id = exist.Id
- nginx.Uid = exist.Uid
- nginx.Check()
- o := orm.NewOrm()
- if nginx.Password == config.ReplacePassword {
- nginx.Password = exist.Password
- }
- nginx.HttpConf = exist.HttpConf
- _, err = o.Update(&nginx)
- if err != nil {
- c.ErrorJson(err)
- return
- }
- logs.Info("post", nginx)
- instance := ngx.GetInstance(&nginx)
- err = instance.Connect()
- if err != nil {
- c.SetCode(1).SetMsg(err.Error()).SetData(nginx)
- c.Json()
- return
- }
- out, err := instance.GetVersion()
- if err != nil {
- c.SetCode(1).SetMsg(err.Error()).SetData(nginx)
- c.Json()
- return
- }
- nginx.VersionInfo = out
- _, _ = o.Update(&nginx, "VersionInfo")
- c.SetData(nginx).Json()
- }
- // StartNginx startNginx
- // post /nginx/:id/start
- func (c *NginxController) StartNginx() {
- nginx, err := c.CheckNginxPermission()
- if err != nil {
- return
- }
- instance := ngx.GetInstance(nginx)
- err = instance.Start()
- isRun, msg := instance.Status()
- c.SetData(isRun).SetMsg(msg).Json()
- }
- // StopNginx add nginx instance
- // post /nginx/:id/stop
- func (c *NginxController) StopNginx() {
- nginx, err := c.CheckNginxPermission()
- if err != nil {
- return
- }
- instance := ngx.GetInstance(nginx)
- err = instance.Stop()
- isRun, msg := instance.Status()
- c.SetData(isRun).SetMsg(msg).Json()
- }
- // RefreshHttp nginx detail data
- // post /nginx/:id/http/refresh
- func (c *NginxController) RefreshHttp() {
- exist, err := c.CheckNginxPermission()
- if err != nil {
- return
- }
- var nginx models.Nginx
- err = json.Unmarshal(c.Ctx.Input.RequestBody, &nginx)
- if err != nil {
- logs.Error(err, string(c.Ctx.Input.RequestBody))
- c.ErrorJson(err)
- return
- }
- logs.Info("id", nginx)
- o := orm.NewOrm()
- if nginx.HttpConf != "" {
- _, err = o.Update(&nginx, "HttpConf", "HttpData")
- if err != nil {
- c.ErrorJson(err)
- return
- }
- exist.HttpConf = nginx.HttpConf
- exist.HttpData = nginx.HttpData
- }
- ins := ngx.GetInstance(exist)
- err = ins.RefreshHttp(*exist)
- if err != nil {
- c.ErrorJson(err)
- return
- }
- c.Json()
- }
- // GetNginx nginx detail data
- // get /nginx/:id
- func (c *NginxController) GetNginx() {
- nginx, err := c.CheckNginxPermission()
- if err != nil {
- return
- }
- if nginx.Password != "" {
- nginx.Password = config.ReplacePassword
- }
- c.AddRespData("nginx", nginx)
- o := orm.NewOrm()
- var servers []models.ServerHost
- _, err = o.QueryTable((*models.ServerHost)(nil)).Filter("NginxId", nginx.Id).All(&servers)
- if err != nil {
- c.ErrorJson(err)
- return
- }
- c.AddRespData("servers", servers)
- c.Json()
- }
- // DelNginx delete a instance
- // delete /nginx/:id
- func (c *NginxController) DelNginx() {
- nginx, err := c.CheckNginxPermission()
- if err != nil {
- return
- }
- o := orm.NewOrm()
- count, err := o.Delete(nginx, "Id")
- if err != nil {
- c.ErrorJson(err)
- } else {
- c.SetData(count).Json()
- }
- }
- // StatusNginx add nginx instance
- // post /nginx/:id/status
- func (c *NginxController) StatusNginx() {
- nginx, err := c.CheckNginxPermission()
- if err != nil {
- return
- }
- instance := ngx.GetInstance(nginx)
- isRun, msg := instance.Status()
- c.SetData(isRun).SetMsg(msg).Json()
- }
|