http.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/interface/bbq/user/internal/service"
  5. "go-common/library/conf/paladin"
  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. svc *service.Service
  12. )
  13. // New new a bm server.
  14. func New(s *service.Service) (engine *bm.Engine) {
  15. var (
  16. hc struct {
  17. Server *bm.ServerConfig
  18. }
  19. )
  20. if err := paladin.Get("http.toml").UnmarshalTOML(&hc); err != nil {
  21. if err != paladin.ErrNotExist {
  22. panic(err)
  23. }
  24. }
  25. svc = s
  26. engine = bm.DefaultServer(hc.Server)
  27. initRouter(engine, verify.New(nil))
  28. if err := engine.Start(); err != nil {
  29. panic(err)
  30. }
  31. return
  32. }
  33. func initRouter(e *bm.Engine, v *verify.Verify) {
  34. e.Ping(ping)
  35. e.Register(register)
  36. g := e.Group("/qing/user/v1")
  37. {
  38. g.GET("/start", v.Verify, howToStart)
  39. }
  40. }
  41. func ping(ctx *bm.Context) {
  42. if err := svc.Ping(ctx); err != nil {
  43. log.Error("ping error(%v)", err)
  44. ctx.AbortWithStatus(http.StatusServiceUnavailable)
  45. }
  46. }
  47. func register(c *bm.Context) {
  48. c.JSON(map[string]interface{}{}, nil)
  49. }
  50. // example for http request handler.
  51. func howToStart(c *bm.Context) {
  52. c.String(0, "Golang 大法好 !!!")
  53. }