Compare commits

...

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))))
DIST_DIR := $(ROOT_DIR)/dist/
#VERSION_PATH := $(shell cat `go env GOMOD` | awk '/^module/{print $$2}')/
VERSION_PATH := main
VERSION_PATH := $(shell cat `go env GOMOD` | awk '/^module/{print $$2}')/pkg
#VERSION_PATH := main
LD_APP_NAMW := -X '$(VERSION_PATH).AppName=$(shell basename `pwd`)'
LD_GIT_COMMIT := -X '$(VERSION_PATH).GitCommit=$(COMMIT_SHA1)'
LD_BUILD_TIME := -X '$(VERSION_PATH).BuildTime=$(BUILD_TIME)'

View File

@ -1,5 +1,10 @@
# 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
Usage:
ddgo [OPTIONS]

110
main.go
View File

@ -2,8 +2,10 @@ package main
import (
"context"
"encoding/json"
"errors"
"fmt"
dt "github.com/JetBlink/dingtalk-notify-go-sdk"
dtn "github.com/JetBlink/dingtalk-notify-go-sdk"
"github.com/ehlxr/ddgo/pkg"
"io"
"net/http"
@ -15,7 +17,7 @@ import (
)
var (
dingTalk *dt.Robot
dingTalk *dtn.Robot
limiter *pkg.LimiterServer
)
@ -26,7 +28,9 @@ func init() {
func main() {
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)
start()
@ -34,7 +38,14 @@ func main() {
func start() {
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{
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()
if err != nil {
log.Error("parse request form %+v",
@ -90,40 +101,85 @@ func requestHandle(w http.ResponseWriter, r *http.Request) {
return
}
content := r.Form.Get("content")
if content == "" {
log.Error("read content from request form nil")
_, _ = io.WriteString(w, "read content from request form nil")
if err = sendMessage(&Message{
r.Form.Get("content"),
r.Form.Get("at"),
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
}
_, _ = 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
at := r.Form.Get("at")
if at != "" {
ats = append(ats, strings.Split(at, ",")...)
if m.At != "" {
ats = append(ats, strings.Split(m.At, ",")...)
}
app := r.Form.Get("app")
if app != "" {
content = fmt.Sprintf("%s\n%s", app, content)
if m.App != "" {
m.Content = fmt.Sprintf("%s\n%s", m.App, m.Content)
}
dtRobot := dingTalk
if m.Ds != "" && m.Dt != "" {
dtRobot = dtn.NewRobot(m.Dt, m.Ds)
}
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 {
log.Error("%+v", err)
_, _ = fmt.Fprintln(w, err)
return
return err
}
log.Info("send message <%s> success", content)
_, _ = io.WriteString(w, "success")
return
log.Info("sendMessage message <%s> success", m.Content)
} else {
log.Error("dingTalk 1 m allow send 20 msg. msg %v discarded.",
content)
_, _ = io.WriteString(w, "dingTalk 1 m allow send 20 msg. msg discarded.")
return
return errors.New("dingTalk 1 m allow sendMessage 20 msg. msg discarded")
}
return nil
}

View File

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

View File

@ -1,6 +1,7 @@
package pkg
import (
"fmt"
"testing"
"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)
}
}