ddgo/main.go

159 lines
3.6 KiB
Go
Raw Normal View History

2019-11-14 07:35:03 +00:00
package main
import (
2019-11-14 09:13:45 +00:00
"context"
"encoding/base64"
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-14 07:35:03 +00:00
"github.com/jessevdk/go-flags"
"io"
"net/http"
2019-11-14 09:13:45 +00:00
"os"
"os/signal"
2019-11-14 07:35:03 +00:00
log "unknwon.dev/clog/v2"
)
2019-11-14 09:13:45 +00:00
var (
2019-11-14 10:20:07 +00:00
AppName string
2019-11-14 09:13:45 +00:00
Version string
BuildTime string
GitCommit string
GoVersion string
versionTpl = `%s
2019-11-14 10:20:07 +00:00
Name: %s
2019-11-14 09:13:45 +00:00
Version: %s
BuildTime: %s
GitCommit: %s
GoVersion: %s
`
bannerBase64 = "DQogX19fXyAgX19fXyAgICBfX18gIF9fX19fIA0KKCAgXyBcKCAgXyBcICAvIF9fKSggIF8gICkNCiApKF8pICkpKF8pICkoIChfLS4gKShfKSggDQooX19fXy8oX19fXy8gIFxfX18vKF9fX19fKQ0K"
2019-11-14 09:18:23 +00:00
opts struct {
2019-11-20 06:42:59 +00:00
Addr string `short:"a" long:"addr" default:":80" env:"ADDR" description:"Addr to listen on for HTTP server"`
2019-11-20 08:14:16 +00:00
Token string `short:"t" long:"token" env:"TOKEN" description:"DingTalk robot access token" required:"true"`
Secret string `short:"s" long:"secret" env:"SECRET" description:"DingTalk robot secret"`
2019-11-20 06:42:59 +00:00
AtMobiles []string `short:"m" long:"at-mobiles" env:"AT_MOBILES" env-delim:"," description:"The mobile of the person will be @"`
IsAtAll bool `short:"e" long:"at-all" env:"AT_ALL" description:"Whether @ everyone"`
Version bool `short:"v" long:"version" description:"Show version info"`
2019-11-14 09:18:23 +00:00
}
2019-11-20 06:42:59 +00:00
robot *dt.Robot
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() {
parseArg()
2019-11-20 06:42:59 +00:00
robot = dt.NewRobot(opts.Token, opts.Secret)
2019-11-14 09:18:23 +00:00
mux := http.NewServeMux()
mux.HandleFunc("/", requestHandle)
server := &http.Server{
2019-11-14 10:20:07 +00:00
Addr: 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)
}
}()
log.Info("Starting HTTP server on http://%s", opts.Addr)
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 parseArg() {
2019-11-15 02:16:19 +00:00
parser := flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash)
2019-11-15 02:23:47 +00:00
if AppName != "" {
parser.Name = AppName
}
2019-11-14 10:20:07 +00:00
if _, err := parser.Parse(); err != nil {
2019-11-14 09:18:23 +00:00
if opts.Version {
printVersion()
os.Exit(0)
}
2019-11-14 10:20:07 +00:00
2019-11-14 09:18:23 +00:00
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
2019-11-15 02:16:19 +00:00
_, _ = fmt.Fprintln(os.Stdout, err)
2019-11-14 09:18:23 +00:00
os.Exit(0)
}
2019-11-15 02:16:19 +00:00
_, _ = fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr)
os.Exit(1)
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-20 06:42:59 +00:00
err = robot.SendTextMessage(content, opts.AtMobiles, opts.IsAtAll)
2019-11-14 07:35:03 +00:00
if err != nil {
2019-11-20 06:42:59 +00:00
log.Error("%+v", err)
_, _ = fmt.Fprintln(w, err)
return
2019-11-14 07:35:03 +00:00
}
2019-11-20 06:42:59 +00:00
log.Info("send message <%s> success", content)
_, _ = io.WriteString(w, "send message success")
2019-11-14 07:35:03 +00:00
}
2019-11-14 09:13:45 +00:00
// printVersion Print out version information
func printVersion() {
banner, _ := base64.StdEncoding.DecodeString(bannerBase64)
2019-11-14 10:20:07 +00:00
fmt.Printf(versionTpl, banner, AppName, Version, BuildTime, GitCommit, GoVersion)
2019-11-14 09:13:45 +00:00
}