go-common/app/interface/bbq/wechat/internal/server/http/http.go

59 lines
1.1 KiB
Go
Raw Normal View History

2019-04-22 10:49:16 +00:00
package http
import (
"go-common/app/interface/bbq/wechat/internal/conf"
"go-common/app/interface/bbq/wechat/internal/model"
"go-common/app/interface/bbq/wechat/internal/service"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"net/http"
"github.com/pkg/errors"
)
var (
svc *service.Service
)
// Init init
func Init(c *conf.Config, s *service.Service) {
svc = service.New(c)
engine := bm.DefaultServer(c.BM)
route(engine)
if err := engine.Start(); err != nil {
log.Error("bm Start error(%v)", err)
panic(err)
}
}
func ping(c *bm.Context) {
if err := svc.Ping(c); err != nil {
log.Error("ping error(%v)", err)
c.AbortWithStatus(http.StatusServiceUnavailable)
}
}
func register(c *bm.Context) {
c.JSON(map[string]interface{}{}, nil)
}
func route(e *bm.Engine) {
e.Ping(ping)
e.Register(register)
g := e.Group("/bbq/wechat")
{
g.GET("/token", getToken)
}
}
func getToken(c *bm.Context) {
arg := new(model.TokenReq)
if err := c.Bind(arg); err != nil {
errors.Wrap(err, "参数验证失败")
return
}
url := c.Request.Referer()
c.JSON(svc.TokenGet(c, arg, url))
}