2019-11-14 07:35:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-11-14 09:13:45 +00:00
|
|
|
"context"
|
2019-11-14 07:35:03 +00:00
|
|
|
"fmt"
|
2019-11-20 06:42:59 +00:00
|
|
|
dt "github.com/JetBlink/dingtalk-notify-go-sdk"
|
2019-11-25 09:23:04 +00:00
|
|
|
"github.com/ehlxr/ddgo/pkg"
|
2019-11-14 07:35:03 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2019-11-14 09:13:45 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2019-11-20 09:04:11 +00:00
|
|
|
"strings"
|
2019-11-25 09:23:04 +00:00
|
|
|
"time"
|
2019-11-14 07:35:03 +00:00
|
|
|
log "unknwon.dev/clog/v2"
|
|
|
|
)
|
|
|
|
|
2019-11-14 09:13:45 +00:00
|
|
|
var (
|
2019-11-20 08:33:37 +00:00
|
|
|
dingTalk *dt.Robot
|
2019-11-25 09:23:04 +00:00
|
|
|
limiter *pkg.LimiterServer
|
2019-11-14 09:13:45 +00:00
|
|
|
)
|
|
|
|
|
2019-11-14 07:35:03 +00:00
|
|
|
func init() {
|
2019-11-14 09:18:23 +00:00
|
|
|
initLog()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2019-11-25 09:23:04 +00:00
|
|
|
pkg.ParseArg()
|
2019-11-14 09:18:23 +00:00
|
|
|
|
2019-11-25 09:23:04 +00:00
|
|
|
dingTalk = dt.NewRobot(pkg.Opts.Robot.Token, pkg.Opts.Robot.Secret)
|
|
|
|
limiter = pkg.NewLimiterServer(1*time.Minute, 20)
|
2019-11-20 06:42:59 +00:00
|
|
|
|
2019-11-25 09:23:04 +00:00
|
|
|
start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func start() {
|
2019-11-14 09:18:23 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", requestHandle)
|
|
|
|
|
|
|
|
server := &http.Server{
|
2019-11-25 09:23:04 +00:00
|
|
|
Addr: pkg.Opts.Addr,
|
2019-11-14 09:18:23 +00:00
|
|
|
Handler: mux,
|
|
|
|
}
|
|
|
|
|
|
|
|
quit := make(chan os.Signal)
|
|
|
|
signal.Notify(quit, os.Interrupt)
|
|
|
|
go func() {
|
|
|
|
<-quit
|
|
|
|
|
|
|
|
if err := server.Shutdown(context.Background()); err != nil {
|
|
|
|
log.Fatal("Shutdown server:", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-11-25 09:23:04 +00:00
|
|
|
log.Info("Starting HTTP server on http://%s", pkg.Opts.Addr)
|
2019-11-14 09:18:23 +00:00
|
|
|
if err := server.ListenAndServe(); err != nil {
|
|
|
|
if err == http.ErrServerClosed {
|
|
|
|
log.Info("Server closed under request")
|
|
|
|
} else {
|
|
|
|
log.Fatal("Server closed unexpected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initLog() {
|
2019-11-14 07:35:03 +00:00
|
|
|
err := log.NewConsole()
|
|
|
|
if err != nil {
|
|
|
|
panic("unable to create new logger: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
err = log.NewFile(log.FileConfig{
|
|
|
|
Level: log.LevelInfo,
|
2019-11-15 02:23:47 +00:00
|
|
|
Filename: "./logs/ddgo.log",
|
2019-11-14 07:35:03 +00:00
|
|
|
FileRotationConfig: log.FileRotationConfig{
|
|
|
|
Rotate: true,
|
|
|
|
Daily: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic("unable to create new logger: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 09:18:23 +00:00
|
|
|
func requestHandle(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("parse request form %+v",
|
|
|
|
err)
|
|
|
|
_, _ = io.WriteString(w,
|
|
|
|
fmt.Sprintf("parse request form %+v", err))
|
|
|
|
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")
|
|
|
|
return
|
|
|
|
}
|
2019-11-14 07:35:03 +00:00
|
|
|
|
2019-11-25 09:23:04 +00:00
|
|
|
ats := pkg.Opts.Robot.AtMobiles
|
2019-11-20 09:04:11 +00:00
|
|
|
at := r.Form.Get("at")
|
|
|
|
if at != "" {
|
|
|
|
ats = append(ats, strings.Split(at, ",")...)
|
|
|
|
}
|
|
|
|
|
2019-11-25 09:23:04 +00:00
|
|
|
app := r.Form.Get("app")
|
|
|
|
if app != "" {
|
|
|
|
content = fmt.Sprintf("%s\n%s", app, content)
|
2019-11-14 07:35:03 +00:00
|
|
|
}
|
2019-11-20 06:42:59 +00:00
|
|
|
|
2019-11-25 09:23:04 +00:00
|
|
|
if limiter.IsAvailable() {
|
|
|
|
err = dingTalk.SendTextMessage(content, ats, pkg.Opts.Robot.IsAtAll)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("%+v", err)
|
|
|
|
_, _ = fmt.Fprintln(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("send message <%s> success", content)
|
|
|
|
_, _ = io.WriteString(w, "success")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
log.Error("dingTalk 1 m allow send 20 msg. msg %v discarded.",
|
|
|
|
content)
|
2019-11-14 07:35:03 +00:00
|
|
|
|
2019-11-25 09:23:04 +00:00
|
|
|
_, _ = io.WriteString(w, "dingTalk 1 m allow send 20 msg. msg discarded.")
|
|
|
|
return
|
|
|
|
}
|
2019-11-14 09:13:45 +00:00
|
|
|
}
|