control.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package nginx
  2. import (
  3. "net/http"
  4. "github.com/0xJacky/Nginx-UI/internal/nginx"
  5. "github.com/0xJacky/Nginx-UI/query"
  6. "github.com/gin-gonic/gin"
  7. "github.com/uozi-tech/cosy"
  8. )
  9. // Reload reloads the nginx
  10. func Reload(c *gin.Context) {
  11. nginx.Control(nginx.Reload).Resp(c)
  12. }
  13. // TestConfig tests the nginx config
  14. func TestConfig(c *gin.Context) {
  15. lastResult := nginx.Control(nginx.TestConfig)
  16. c.JSON(http.StatusOK, gin.H{
  17. "message": lastResult.GetOutput(),
  18. "level": lastResult.GetLevel(),
  19. })
  20. }
  21. // TestConfigWithNamespace tests nginx config in isolated sandbox for a specific namespace
  22. func TestConfigWithNamespace(c *gin.Context) {
  23. var req struct {
  24. NamespaceID uint64 `json:"namespace_id" form:"namespace_id"`
  25. }
  26. if !cosy.BindAndValid(c, &req) {
  27. return
  28. }
  29. // Get namespace and related configs
  30. var namespaceInfo *nginx.NamespaceInfo
  31. var sitePaths []string
  32. var streamPaths []string
  33. if req.NamespaceID > 0 {
  34. // Fetch namespace
  35. ns := query.Namespace
  36. namespace, err := ns.Where(ns.ID.Eq(req.NamespaceID)).First()
  37. if err != nil {
  38. cosy.ErrHandler(c, err)
  39. return
  40. }
  41. namespaceInfo = &nginx.NamespaceInfo{
  42. ID: namespace.ID,
  43. Name: namespace.Name,
  44. DeployMode: namespace.DeployMode,
  45. }
  46. // Fetch sites belonging to this namespace
  47. s := query.Site
  48. sites, err := s.Where(s.NamespaceID.Eq(req.NamespaceID)).Find()
  49. if err == nil {
  50. for _, site := range sites {
  51. sitePaths = append(sitePaths, site.Path)
  52. }
  53. }
  54. // Fetch streams belonging to this namespace
  55. st := query.Stream
  56. streams, err := st.Where(st.NamespaceID.Eq(req.NamespaceID)).Find()
  57. if err == nil {
  58. for _, stream := range streams {
  59. streamPaths = append(streamPaths, stream.Path)
  60. }
  61. }
  62. }
  63. // Use sandbox test with namespace-specific paths
  64. result := nginx.Control(func() (string, error) {
  65. return nginx.SandboxTestConfigWithPaths(namespaceInfo, sitePaths, streamPaths)
  66. })
  67. c.JSON(http.StatusOK, gin.H{
  68. "message": result.GetOutput(),
  69. "level": result.GetLevel(),
  70. "namespace_id": req.NamespaceID,
  71. })
  72. }
  73. // Restart restarts the nginx
  74. func Restart(c *gin.Context) {
  75. c.JSON(http.StatusOK, gin.H{
  76. "message": "ok",
  77. })
  78. go nginx.Restart()
  79. }
  80. // Status returns the status of the nginx
  81. func Status(c *gin.Context) {
  82. lastResult := nginx.GetLastResult()
  83. running := nginx.IsRunning()
  84. c.JSON(http.StatusOK, gin.H{
  85. "running": running,
  86. "message": lastResult.GetOutput(),
  87. "level": lastResult.GetLevel(),
  88. })
  89. }