Impl swag and auth switch
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/opentdp/go-helper/logman"
|
||||
"github.com/opentdp/go-helper/request"
|
||||
"github.com/opentdp/go-helper/strutil"
|
||||
"github.com/opentdp/wechat-rest/wcferry"
|
||||
|
||||
@@ -14,13 +13,12 @@ import (
|
||||
)
|
||||
|
||||
var wc *wcferry.Client
|
||||
var forwardUrls = map[string]bool{}
|
||||
|
||||
func initService() {
|
||||
|
||||
host, port, err := net.SplitHostPort(args.Wcf.Address)
|
||||
if err != nil {
|
||||
logman.Fatal("failed to start wcf", "error", err)
|
||||
logman.Fatal("invalid address", "error", err)
|
||||
}
|
||||
|
||||
wc = &wcferry.Client{
|
||||
@@ -578,20 +576,7 @@ func enableForwardMsg(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if _, ok := forwardUrls[req.Url]; ok {
|
||||
c.Set("Error", "url already exists")
|
||||
return
|
||||
}
|
||||
|
||||
err := wc.EnrollReceiver(true, func(msg *wcferry.WxMsg) {
|
||||
logman.Info("forward msg", "url", req.Url, "Id", msg.Id)
|
||||
request.JsonPost(req.Url, msg, request.H{})
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
forwardUrls[req.Url] = true
|
||||
}
|
||||
|
||||
err := enableForwardToUrl(req.Url)
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: err == nil,
|
||||
Error: err,
|
||||
@@ -606,8 +591,13 @@ func enableForwardMsg(c *gin.Context) {
|
||||
// @Router /disable_forward_msg [post]
|
||||
func disableForwardMsg(c *gin.Context) {
|
||||
|
||||
err := wc.DisableReceiver()
|
||||
var req ForwardMsgRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Set("Error", err)
|
||||
return
|
||||
}
|
||||
|
||||
err := disableForwardToUrl(req.Url)
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: err == nil,
|
||||
Error: err,
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package wcfrest
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/opentdp/go-helper/logman"
|
||||
"github.com/opentdp/go-helper/request"
|
||||
|
||||
"github.com/opentdp/wechat-rest/wcferry"
|
||||
)
|
||||
|
||||
var forwardToUrlStat = false
|
||||
var forwardToUrlList = map[string]bool{}
|
||||
|
||||
func enableForwardToUrl(url string) error {
|
||||
|
||||
if !forwardToUrlStat {
|
||||
err := wc.EnrollReceiver(true, func(msg *wcferry.WxMsg) {
|
||||
for url := range forwardToUrlList {
|
||||
logman.Info("forward msg", "url", url, "Id", msg.Id)
|
||||
request.JsonPost(url, msg, request.H{})
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if _, ok := forwardToUrlList[url]; ok {
|
||||
return errors.New("url already exists")
|
||||
}
|
||||
|
||||
forwardToUrlStat = true
|
||||
forwardToUrlList[url] = true
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func disableForwardToUrl(url string) error {
|
||||
|
||||
if _, ok := forwardToUrlList[url]; !ok {
|
||||
return errors.New("url not exists")
|
||||
}
|
||||
|
||||
delete(forwardToUrlList, url)
|
||||
|
||||
if len(forwardToUrlList) == 0 {
|
||||
if err := wc.DisableReceiver(false); err != nil {
|
||||
return err
|
||||
}
|
||||
forwardToUrlStat = false
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user