nginx.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package desktop
  2. import (
  3. "context"
  4. "github.com/astaxie/beego/logs"
  5. "nginx-ui/server/models"
  6. "nginx-ui/server/modules/nginx/nginx_service"
  7. "nginx-ui/server/routers"
  8. "strings"
  9. )
  10. var logger = logs.GetLogger("NginxApi")
  11. var nginxService = nginx_service.NginxService{}
  12. // NginxApi struct
  13. type NginxApi struct {
  14. ctx context.Context
  15. }
  16. // NewNginxApi creates a new App application struct
  17. func NewNginxApi() *NginxApi {
  18. return &NginxApi{}
  19. }
  20. func (a *NginxApi) Match(path string) bool {
  21. return strings.HasPrefix(path, "/nginx")
  22. }
  23. // PostApi 做一个统一的适配层
  24. func (a *NginxApi) PostApi(path string, req string) ApiResp {
  25. logger.Printf("[POST] path: %s, data: %s", path, req)
  26. var user = ApiSession.user
  27. if path == routers.NginxR {
  28. return ApiResp{
  29. Data: nginxService.Add(user, []byte(req)),
  30. }
  31. }
  32. var data *models.RespData
  33. if result := ParsePathParam(path, routers.NginxGetR); result.Match {
  34. id := result.GetParam("id")
  35. data = nginxService.GetNginx(id, user)
  36. } else if result := ParsePathParam(path, routers.NginxStatusR); result.Match {
  37. //id := result.GetParam("id")
  38. }
  39. return ApiResp{Data: data}
  40. }
  41. func (a *NginxApi) GetApi(path string, req string) ApiResp {
  42. logger.Printf("[GET] path: %s, data: %s", path, req)
  43. var user = ApiSession.user
  44. if path == routers.NginxR {
  45. return ApiResp{
  46. Data: nginxService.ListNginx(user),
  47. }
  48. }
  49. if r := ParsePathParam(path, routers.NginxGetR); r.Match {
  50. id := r.GetParam("id")
  51. logs.Info("param: ", r, id)
  52. return ApiResp{
  53. Data: nginxService.GetNginx(id, user),
  54. }
  55. }
  56. var data *models.RespData
  57. logs.Info("resp:{}", data)
  58. return ApiResp{Data: data}
  59. }
  60. func (a *NginxApi) DeleteApi(path string, req string) ApiResp {
  61. logger.Printf("[DELETE] path: %s, data: %s", path, req)
  62. return ApiResp{}
  63. }
  64. func (a *NginxApi) PutApi(path string, req string) ApiResp {
  65. logger.Printf("[PUT] path: %s, data: %s", path, req)
  66. return ApiResp{}
  67. }