8 Commits

5 changed files with 114 additions and 41 deletions

View File

@@ -4,8 +4,8 @@ COMMIT_SHA1 := $(shell git rev-parse HEAD)
ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
DIST_DIR := $(ROOT_DIR)/dist/ DIST_DIR := $(ROOT_DIR)/dist/
#VERSION_PATH := $(shell cat `go env GOMOD` | awk '/^module/{print $$2}')/ VERSION_PATH := $(shell cat `go env GOMOD` | awk '/^module/{print $$2}')/pkg
VERSION_PATH := main #VERSION_PATH := main
LD_APP_NAMW := -X '$(VERSION_PATH).AppName=$(shell basename `pwd`)' LD_APP_NAMW := -X '$(VERSION_PATH).AppName=$(shell basename `pwd`)'
LD_GIT_COMMIT := -X '$(VERSION_PATH).GitCommit=$(COMMIT_SHA1)' LD_GIT_COMMIT := -X '$(VERSION_PATH).GitCommit=$(COMMIT_SHA1)'
LD_BUILD_TIME := -X '$(VERSION_PATH).BuildTime=$(BUILD_TIME)' LD_BUILD_TIME := -X '$(VERSION_PATH).BuildTime=$(BUILD_TIME)'

View File

@@ -1,5 +1,10 @@
# DDGO # DDGO
[![license](https://badgen.net/badge/license/MIT/blue)](./LICENSE)
[![](https://badgen.net/github/commits/ehlxr/ddgo)](https://github.com/ehlxr/ddgo/commits/)
[![](https://badgen.net/github/last-commit/ehlxr/ddgo)]((https://github.com/ehlxr/ddgo/commits/))
[![](https://badgen.net/github/releases/ehlxr/ddgo)](https://github.com/ehlxr/ddgo/releases)
```bash ```bash
Usage: Usage:
ddgo [OPTIONS] ddgo [OPTIONS]

110
main.go
View File

@@ -2,8 +2,10 @@ package main
import ( import (
"context" "context"
"encoding/json"
"errors"
"fmt" "fmt"
dt "github.com/JetBlink/dingtalk-notify-go-sdk" dtn "github.com/JetBlink/dingtalk-notify-go-sdk"
"github.com/ehlxr/ddgo/pkg" "github.com/ehlxr/ddgo/pkg"
"io" "io"
"net/http" "net/http"
@@ -15,7 +17,7 @@ import (
) )
var ( var (
dingTalk *dt.Robot dingTalk *dtn.Robot
limiter *pkg.LimiterServer limiter *pkg.LimiterServer
) )
@@ -26,7 +28,9 @@ func init() {
func main() { func main() {
pkg.ParseArg() pkg.ParseArg()
dingTalk = dt.NewRobot(pkg.Opts.Robot.Token, pkg.Opts.Robot.Secret) dingTalk = dtn.NewRobot(pkg.Opts.Robot.Token, pkg.Opts.Robot.Secret)
// 每个机器人每分钟最多发送 20 条。如果超过 20 条,会限流 10 分钟 https://ding-doc.dingtalk.com/doc#/serverapi2/krgddi/3446b47e
limiter = pkg.NewLimiterServer(1*time.Minute, 20) limiter = pkg.NewLimiterServer(1*time.Minute, 20)
start() start()
@@ -34,7 +38,14 @@ func main() {
func start() { func start() {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", requestHandle) mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.Header.Get("Content-Type") {
case "application/json":
handlePostJson(w, r)
case "application/x-www-form-urlencoded":
handlePostForm(w, r)
}
})
server := &http.Server{ server := &http.Server{
Addr: pkg.Opts.Addr, Addr: pkg.Opts.Addr,
@@ -80,7 +91,7 @@ func initLog() {
} }
} }
func requestHandle(w http.ResponseWriter, r *http.Request) { func handlePostForm(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm() err := r.ParseForm()
if err != nil { if err != nil {
log.Error("parse request form %+v", log.Error("parse request form %+v",
@@ -90,40 +101,85 @@ func requestHandle(w http.ResponseWriter, r *http.Request) {
return return
} }
content := r.Form.Get("content") if err = sendMessage(&Message{
if content == "" { r.Form.Get("content"),
log.Error("read content from request form nil") r.Form.Get("at"),
_, _ = io.WriteString(w, "read content from request form nil") r.Form.Get("app"),
r.Form.Get("dt"),
r.Form.Get("ds"),
}); err != nil {
http.Error(w, err.Error(), 400)
log.Error(err.Error())
return return
} }
_, _ = io.WriteString(w, "success")
return
}
func handlePostJson(w http.ResponseWriter, r *http.Request) {
if r.Body == nil {
http.Error(w, "Please sendMessage a request body", 400)
log.Error("Please sendMessage a request body")
return
}
m := &Message{}
err := json.NewDecoder(r.Body).Decode(m)
if err != nil {
http.Error(w, err.Error(), 400)
log.Error(err.Error())
return
}
if err = sendMessage(m); err != nil {
http.Error(w, err.Error(), 400)
log.Error(err.Error())
return
}
_, _ = io.WriteString(w, "success")
return
}
type Message struct {
Content string `json:"content" desc:"消息内容"`
At string `json:"at" desc:"被@人的手机号"`
App string `json:"app" desc:"发送消息应用名称(添加到消息之前)"`
Dt string `json:"dt" desc:"钉钉机器人 token"`
Ds string `json:"ds" desc:"钉钉机器人签名 secret"`
}
func sendMessage(m *Message) error {
if m.Content == "" {
return errors.New("content must not be null")
}
ats := pkg.Opts.Robot.AtMobiles ats := pkg.Opts.Robot.AtMobiles
at := r.Form.Get("at") if m.At != "" {
if at != "" { ats = append(ats, strings.Split(m.At, ",")...)
ats = append(ats, strings.Split(at, ",")...)
} }
app := r.Form.Get("app") if m.App != "" {
if app != "" { m.Content = fmt.Sprintf("%s\n%s", m.App, m.Content)
content = fmt.Sprintf("%s\n%s", app, content) }
dtRobot := dingTalk
if m.Ds != "" && m.Dt != "" {
dtRobot = dtn.NewRobot(m.Dt, m.Ds)
} }
if limiter.IsAvailable() { if limiter.IsAvailable() {
err = dingTalk.SendTextMessage(content, ats, pkg.Opts.Robot.IsAtAll) err := dtRobot.SendTextMessage(m.Content, ats, pkg.Opts.Robot.IsAtAll)
if err != nil { if err != nil {
log.Error("%+v", err) return err
_, _ = fmt.Fprintln(w, err)
return
} }
log.Info("send message <%s> success", content) log.Info("sendMessage message <%s> success", m.Content)
_, _ = io.WriteString(w, "success")
return
} else { } else {
log.Error("dingTalk 1 m allow send 20 msg. msg %v discarded.", return errors.New("dingTalk 1 m allow sendMessage 20 msg. msg discarded")
content)
_, _ = io.WriteString(w, "dingTalk 1 m allow send 20 msg. msg discarded.")
return
} }
return nil
} }

View File

@@ -9,32 +9,30 @@ type LimiterServer struct {
interval time.Duration interval time.Duration
maxCount int maxCount int
sync.Mutex sync.Mutex
reqCount int reqCount int
startTime time.Time time time.Time
} }
func NewLimiterServer(i time.Duration, c int) *LimiterServer { func NewLimiterServer(interval time.Duration, maxCount int) *LimiterServer {
return &LimiterServer{ return &LimiterServer{
interval: i, interval: interval,
maxCount: c, maxCount: maxCount,
} }
} }
func (limiter *LimiterServer) IsAvailable() bool { func (limiter *LimiterServer) IsAvailable() bool {
limiter.Lock() limiter.Lock()
defer limiter.Unlock() defer limiter.Unlock()
now := time.Now()
if limiter.startTime.IsZero() || if limiter.time.IsZero() ||
limiter.startTime.Add(limiter.interval).Before(time.Now()) { limiter.time.Add(limiter.interval).Before(now) {
limiter.reqCount = 1 limiter.reqCount = 0
limiter.startTime = time.Now()
return true
} }
if limiter.reqCount < limiter.maxCount { if limiter.reqCount < limiter.maxCount {
limiter.reqCount += 1 limiter.reqCount += 1
limiter.time = now
return true return true
} }

View File

@@ -1,6 +1,7 @@
package pkg package pkg
import ( import (
"fmt"
"testing" "testing"
"time" "time"
) )
@@ -16,3 +17,16 @@ func TestLimiter(t *testing.T) {
} }
} }
} }
func TestLimiter2(t *testing.T) {
limiter := NewLimiterServer(10*time.Second, 10)
for i := 0; i < 20; i++ {
if limiter.IsAvailable() {
fmt.Println("hello...", limiter.reqCount)
} else {
fmt.Println("limited")
}
time.Sleep(1 * time.Second)
}
}