http.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/service/main/sms/conf"
  5. "go-common/app/service/main/sms/service"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/http/blademaster/middleware/verify"
  9. )
  10. var (
  11. smsSvc *service.Service
  12. idfSvc *verify.Verify
  13. )
  14. // Init init http sever instance.
  15. func Init(c *conf.Config, s *service.Service) {
  16. idfSvc = verify.New(c.Verify)
  17. smsSvc = s
  18. engine := bm.DefaultServer(c.HTTPServer)
  19. route(engine)
  20. if err := engine.Start(); err != nil {
  21. log.Error("engine.Start error(%v)", err)
  22. panic(err)
  23. }
  24. }
  25. func route(e *bm.Engine) {
  26. e.Ping(ping)
  27. e.Register(register)
  28. g := e.Group("/x/internal/sms", bm.CORS())
  29. {
  30. g.POST("/send", idfSvc.Verify, send)
  31. g.POST("/sendBatch", idfSvc.Verify, sendBatch)
  32. }
  33. }
  34. func ping(c *bm.Context) {
  35. if err := smsSvc.Ping(c); err != nil {
  36. log.Error("sms-service ping error(%v)", err)
  37. c.AbortWithStatus(http.StatusServiceUnavailable)
  38. }
  39. }
  40. func register(c *bm.Context) {
  41. c.JSON(map[string]interface{}{}, nil)
  42. }