Create & Init Project...
This commit is contained in:
39
app/interface/bbq/wechat/internal/conf/BUILD
Normal file
39
app/interface/bbq/wechat/internal/conf/BUILD
Normal file
@ -0,0 +1,39 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["conf.go"],
|
||||
importpath = "go-common/app/interface/bbq/wechat/internal/conf",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//library/cache/redis:go_default_library",
|
||||
"//library/conf:go_default_library",
|
||||
"//library/ecode/tip:go_default_library",
|
||||
"//library/log:go_default_library",
|
||||
"//library/net/http/blademaster:go_default_library",
|
||||
"//library/net/http/blademaster/middleware/auth:go_default_library",
|
||||
"//library/net/http/blademaster/middleware/verify:go_default_library",
|
||||
"//library/net/trace:go_default_library",
|
||||
"//vendor/github.com/BurntSushi/toml:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
100
app/interface/bbq/wechat/internal/conf/conf.go
Normal file
100
app/interface/bbq/wechat/internal/conf/conf.go
Normal file
@ -0,0 +1,100 @@
|
||||
package conf
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"go-common/library/net/http/blademaster/middleware/auth"
|
||||
|
||||
"go-common/library/cache/redis"
|
||||
"go-common/library/conf"
|
||||
ecode "go-common/library/ecode/tip"
|
||||
"go-common/library/log"
|
||||
bm "go-common/library/net/http/blademaster"
|
||||
"go-common/library/net/http/blademaster/middleware/verify"
|
||||
"go-common/library/net/trace"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
var (
|
||||
confPath string
|
||||
client *conf.Client
|
||||
// Conf config
|
||||
Conf = &Config{}
|
||||
)
|
||||
|
||||
// Config .
|
||||
type Config struct {
|
||||
Log *log.Config
|
||||
BM *bm.ServerConfig
|
||||
Verify *verify.Config
|
||||
Auth *auth.Config
|
||||
Tracer *trace.Config
|
||||
Redis *redis.Config
|
||||
Ecode *ecode.Config
|
||||
Weixin *Weixin
|
||||
URLs *Urls
|
||||
}
|
||||
|
||||
// Weixin conf
|
||||
type Weixin struct {
|
||||
AppID string
|
||||
Secret string
|
||||
}
|
||||
|
||||
// Urls conf
|
||||
type Urls struct {
|
||||
Weixin string
|
||||
Jsapi string
|
||||
}
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&confPath, "conf", "", "default config path")
|
||||
}
|
||||
|
||||
// Init init conf
|
||||
func Init() error {
|
||||
if confPath != "" {
|
||||
return local()
|
||||
}
|
||||
return remote()
|
||||
}
|
||||
|
||||
func local() (err error) {
|
||||
_, err = toml.DecodeFile(confPath, &Conf)
|
||||
return
|
||||
}
|
||||
|
||||
func remote() (err error) {
|
||||
if client, err = conf.New(); err != nil {
|
||||
return
|
||||
}
|
||||
if err = load(); err != nil {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
for range client.Event() {
|
||||
log.Info("config reload")
|
||||
if load() != nil {
|
||||
log.Error("config reload error (%v)", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
return
|
||||
}
|
||||
|
||||
func load() (err error) {
|
||||
var (
|
||||
s string
|
||||
ok bool
|
||||
tmpConf *Config
|
||||
)
|
||||
if s, ok = client.Toml2(); !ok {
|
||||
return errors.New("load config center error")
|
||||
}
|
||||
if _, err = toml.Decode(s, &tmpConf); err != nil {
|
||||
return errors.New("could not decode config")
|
||||
}
|
||||
*Conf = *tmpConf
|
||||
return
|
||||
}
|
28
app/interface/bbq/wechat/internal/model/BUILD
Normal file
28
app/interface/bbq/wechat/internal/model/BUILD
Normal file
@ -0,0 +1,28 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["model.go"],
|
||||
importpath = "go-common/app/interface/bbq/wechat/internal/model",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
21
app/interface/bbq/wechat/internal/model/model.go
Normal file
21
app/interface/bbq/wechat/internal/model/model.go
Normal file
@ -0,0 +1,21 @@
|
||||
package model
|
||||
|
||||
// const 常量
|
||||
const ()
|
||||
|
||||
// WXToken 返回token
|
||||
type WXToken struct {
|
||||
Token string `json:"access_token"`
|
||||
Expires int `json:"expires_in"`
|
||||
}
|
||||
|
||||
// WXTicket 返回ticket
|
||||
type WXTicket struct {
|
||||
Ticket string `json:"ticket"`
|
||||
}
|
||||
|
||||
// TokenReq .
|
||||
type TokenReq struct {
|
||||
Noncestr string `json:"nonce" form:"nonce" validate:"required"`
|
||||
Timestamp string `json:"timestamp" form:"timestamp" validate:"required"`
|
||||
}
|
36
app/interface/bbq/wechat/internal/server/http/BUILD
Normal file
36
app/interface/bbq/wechat/internal/server/http/BUILD
Normal file
@ -0,0 +1,36 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["http.go"],
|
||||
importpath = "go-common/app/interface/bbq/wechat/internal/server/http",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//app/interface/bbq/wechat/internal/conf:go_default_library",
|
||||
"//app/interface/bbq/wechat/internal/model:go_default_library",
|
||||
"//app/interface/bbq/wechat/internal/service:go_default_library",
|
||||
"//library/log:go_default_library",
|
||||
"//library/net/http/blademaster:go_default_library",
|
||||
"//vendor/github.com/pkg/errors:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
58
app/interface/bbq/wechat/internal/server/http/http.go
Normal file
58
app/interface/bbq/wechat/internal/server/http/http.go
Normal file
@ -0,0 +1,58 @@
|
||||
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))
|
||||
}
|
34
app/interface/bbq/wechat/internal/service/BUILD
Normal file
34
app/interface/bbq/wechat/internal/service/BUILD
Normal file
@ -0,0 +1,34 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["service.go"],
|
||||
importpath = "go-common/app/interface/bbq/wechat/internal/service",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//app/interface/bbq/wechat/dao:go_default_library",
|
||||
"//app/interface/bbq/wechat/internal/conf:go_default_library",
|
||||
"//app/interface/bbq/wechat/internal/model:go_default_library",
|
||||
"//library/log:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
68
app/interface/bbq/wechat/internal/service/service.go
Normal file
68
app/interface/bbq/wechat/internal/service/service.go
Normal file
@ -0,0 +1,68 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha1"
|
||||
"fmt"
|
||||
"go-common/app/interface/bbq/wechat/dao"
|
||||
"go-common/app/interface/bbq/wechat/internal/conf"
|
||||
"go-common/app/interface/bbq/wechat/internal/model"
|
||||
"go-common/library/log"
|
||||
)
|
||||
|
||||
// Service struct
|
||||
type Service struct {
|
||||
c *conf.Config
|
||||
dao *dao.Dao
|
||||
}
|
||||
|
||||
// New init
|
||||
func New(c *conf.Config) (s *Service) {
|
||||
s = &Service{
|
||||
c: c,
|
||||
dao: dao.New(c),
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Ping Service
|
||||
func (s *Service) Ping(ctx context.Context) (err error) {
|
||||
return s.dao.Ping(ctx)
|
||||
}
|
||||
|
||||
// Close Service
|
||||
func (s *Service) Close() {
|
||||
s.dao.Close()
|
||||
}
|
||||
|
||||
// TokenGet .
|
||||
func (s *Service) TokenGet(ctx context.Context, arg *model.TokenReq, url string) (sig string, err error) {
|
||||
token := ""
|
||||
|
||||
if token, err = s.dao.TokenGetLast(ctx); err != nil {
|
||||
log.Warnv(ctx, log.KV("log", "weixin token get last fail"))
|
||||
return
|
||||
}
|
||||
|
||||
if token == "" {
|
||||
if token, err = s.dao.TokenGet(ctx); err != nil {
|
||||
log.Warnv(ctx, log.KV("log", "weixin token get fail"))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
sig = s.TokenCalc(ctx, token, arg.Noncestr, arg.Timestamp, url)
|
||||
return
|
||||
}
|
||||
|
||||
// TokenCalc 计算签名
|
||||
func (s *Service) TokenCalc(ctx context.Context, ticket string, noncestr string, timestamp string, url string) (sig string) {
|
||||
str := fmt.Sprintf("jsapi_ticket=%s&noncestr=%s×tamp=%s&url=%s", ticket, noncestr, timestamp, url)
|
||||
|
||||
hash := sha1.New()
|
||||
hash.Write([]byte(str))
|
||||
bs := hash.Sum(nil)
|
||||
|
||||
sig = fmt.Sprintf("%x", bs)
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user