http.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/interface/main/upload/conf"
  5. xanti "go-common/app/interface/main/upload/http/antispam"
  6. "go-common/app/interface/main/upload/service"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/http/blademaster/middleware/auth"
  10. "go-common/library/net/http/blademaster/middleware/verify"
  11. )
  12. var (
  13. uploadSvr *service.Service
  14. authInterSvr *auth.Auth
  15. authSvr *auth.Auth
  16. verifySvr *verify.Verify
  17. anti *xanti.Antispam
  18. )
  19. // Init init http
  20. func Init(c *conf.Config, s *service.Service) {
  21. initService(c, s)
  22. engine := bm.DefaultServer(c.BM)
  23. initRouter(engine)
  24. // init Outer serve
  25. if err := engine.Start(); err != nil {
  26. log.Error("xhttp.Serve error(%v)", err)
  27. panic(err)
  28. }
  29. }
  30. func initService(c *conf.Config, s *service.Service) {
  31. uploadSvr = s
  32. authInterSvr = auth.New(c.AuthInter)
  33. authSvr = auth.New(c.AuthOut)
  34. verifySvr = verify.New(nil)
  35. anti = xanti.New(c.Antispam, s.GetRateLimit) //mid+dir 限流
  36. }
  37. func initRouter(e *bm.Engine) {
  38. e.Ping(ping)
  39. uploadInternal := e.Group("/x/internal")
  40. {
  41. uploadInternal.POST("/upload", verifySvr.Verify, internalUpload)
  42. uploadInternal.POST("/upload/image", authInterSvr.User, anti.Handler(), internalUploadImage)
  43. uploadInternal.POST("/upload/admin/image", verifySvr.Verify, anti.Handler(), internalUploadAdminImage)
  44. uploadInternal.POST("/image/gen", verifySvr.Verify, anti.Handler(), genImageUpload)
  45. }
  46. upload := e.Group("/x/upload")
  47. {
  48. upload.POST("/image", uploadImagePublic)
  49. upload.POST("/app/image", authSvr.UserMobile, anti.Handler(), uploadMobileImage)
  50. upload.POST("/web/image", authSvr.UserWeb, anti.Handler(), uploadWebImage)
  51. }
  52. }
  53. // ping check server ok.
  54. func ping(c *bm.Context) {
  55. var err error
  56. if err = uploadSvr.Ping(c); err != nil {
  57. log.Error("upload service ping error(%v)", err)
  58. c.AbortWithStatus(http.StatusServiceUnavailable)
  59. }
  60. }