http.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package http
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/lejianwen/rustdesk-api/global"
  5. "github.com/lejianwen/rustdesk-api/http/middleware"
  6. "github.com/lejianwen/rustdesk-api/http/router"
  7. "github.com/sirupsen/logrus"
  8. "net/http"
  9. "strings"
  10. )
  11. func ApiInit() {
  12. gin.SetMode(global.Config.Gin.Mode)
  13. g := gin.New()
  14. //[WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
  15. //Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
  16. if global.Config.Gin.TrustProxy != "" {
  17. pro := strings.Split(global.Config.Gin.TrustProxy, ",")
  18. err := g.SetTrustedProxies(pro)
  19. if err != nil {
  20. panic(err)
  21. }
  22. }
  23. if global.Config.Gin.Mode == gin.ReleaseMode {
  24. //修改gin Recovery日志 输出为logger的输出点
  25. if global.Logger != nil {
  26. gin.DefaultErrorWriter = global.Logger.WriterLevel(logrus.ErrorLevel)
  27. }
  28. }
  29. g.NoRoute(func(c *gin.Context) {
  30. c.String(http.StatusNotFound, "404 not found")
  31. })
  32. g.Use(middleware.Logger(), gin.Recovery())
  33. router.WebInit(g)
  34. router.Init(g)
  35. router.ApiInit(g)
  36. Run(g, global.Config.Gin.ApiAddr)
  37. }