self_check.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/internal/self_check"
  9. "github.com/gin-gonic/gin"
  10. )
  11. func SelfCheck(c *gin.Context) {
  12. report := self_check.Run()
  13. c.JSON(http.StatusOK, report)
  14. }
  15. func SelfCheckFix(c *gin.Context) {
  16. result := self_check.AttemptFix(c.Param("name"))
  17. c.JSON(http.StatusOK, result)
  18. }
  19. func CheckWebSocket(c *gin.Context) {
  20. var upgrader = websocket.Upgrader{
  21. CheckOrigin: func(r *http.Request) bool {
  22. return true
  23. },
  24. }
  25. ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
  26. if err != nil {
  27. logger.Error(err)
  28. return
  29. }
  30. defer ws.Close()
  31. err = ws.WriteJSON(gin.H{
  32. "message": "ok",
  33. })
  34. if err != nil {
  35. logger.Error(err)
  36. return
  37. }
  38. }
  39. func CheckSSE(c *gin.Context) {
  40. notify := c.Writer.CloseNotify()
  41. for i := 0; i < 10; i++ {
  42. select {
  43. case <-notify:
  44. return
  45. default:
  46. c.Stream(func(w io.Writer) bool {
  47. c.SSEvent("message", time.Now())
  48. return false
  49. })
  50. time.Sleep(time.Second * 2)
  51. }
  52. }
  53. }