http.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/job/openplatform/article/conf"
  5. "go-common/app/job/openplatform/article/service"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. )
  9. var ajSrv *service.Service
  10. // Init .
  11. func Init(conf *conf.Config, srv *service.Service) {
  12. ajSrv = srv
  13. // init outer router
  14. engineOuter := bm.DefaultServer(conf.BM)
  15. outerRouter(engineOuter)
  16. if err := engineOuter.Start(); err != nil {
  17. log.Error("xhttp.Serve error(%v)", err)
  18. panic(err)
  19. }
  20. }
  21. // outerRouter init outer router
  22. func outerRouter(r *bm.Engine) {
  23. r.Ping(ping)
  24. r.Register(register)
  25. cr := r.Group("/sitemap")
  26. {
  27. cr.GET("/read/detail.xml", sitemap)
  28. }
  29. }
  30. func ping(c *bm.Context) {
  31. if err := ajSrv.Ping(c); err != nil {
  32. log.Error("ping error(%v)", err)
  33. c.AbortWithStatus(http.StatusServiceUnavailable)
  34. }
  35. }
  36. func register(c *bm.Context) {
  37. c.JSON(map[string]interface{}{}, nil)
  38. }
  39. func sitemap(c *bm.Context) {
  40. res, l := ajSrv.SitemapXML(c)
  41. c.Writer.Header().Set("Content-Type", "text/xml")
  42. c.Writer.Header().Set("Content-Length", l)
  43. c.Status(http.StatusOK)
  44. c.Writer.Write([]byte(res))
  45. }