self_check.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package system
  2. import (
  3. "io"
  4. "net/http"
  5. "github.com/gorilla/websocket"
  6. "github.com/uozi-tech/cosy/logger"
  7. "time"
  8. "github.com/0xJacky/Nginx-UI/api"
  9. "github.com/0xJacky/Nginx-UI/internal/self_check"
  10. "github.com/gin-gonic/gin"
  11. )
  12. func SelfCheck(c *gin.Context) {
  13. report := self_check.Run()
  14. c.JSON(http.StatusOK, report)
  15. }
  16. func SelfCheckFix(c *gin.Context) {
  17. result := self_check.AttemptFix(c.Param("name"))
  18. c.JSON(http.StatusOK, result)
  19. }
  20. func CheckWebSocket(c *gin.Context) {
  21. var upgrader = websocket.Upgrader{
  22. CheckOrigin: func(r *http.Request) bool {
  23. return true
  24. },
  25. }
  26. ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
  27. if err != nil {
  28. logger.Error(err)
  29. return
  30. }
  31. defer ws.Close()
  32. err = ws.WriteJSON(gin.H{
  33. "message": "ok",
  34. })
  35. if err != nil {
  36. logger.Error(err)
  37. return
  38. }
  39. }
  40. func CheckSSE(c *gin.Context) {
  41. api.SetSSEHeaders(c)
  42. notify := c.Writer.CloseNotify()
  43. for i := 0; i < 10; i++ {
  44. select {
  45. case <-notify:
  46. return
  47. default:
  48. c.Stream(func(w io.Writer) bool {
  49. c.SSEvent("message", time.Now())
  50. return false
  51. })
  52. time.Sleep(time.Second * 2)
  53. }
  54. }
  55. }
  56. func TimeoutCheck(c *gin.Context) {
  57. time.Sleep(time.Minute)
  58. c.JSON(http.StatusOK, gin.H{
  59. "message": "ok",
  60. })
  61. }