ddgo/main.go

178 lines
3.6 KiB
Go
Raw Normal View History

2019-11-14 07:35:03 +00:00
package main
import (
"bytes"
2019-11-14 09:13:45 +00:00
"context"
"encoding/base64"
2019-11-14 07:35:03 +00:00
"encoding/json"
"fmt"
"github.com/jessevdk/go-flags"
"io"
"io/ioutil"
"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-14 10:20:07 +00:00
Addr string `short:"a" long:"addr" default:":80" env:"ADDR" description:"Addr to listen on for HTTP server"`
2019-11-14 09:18:23 +00:00
WebHookUrl string `short:"u" long:"webhook-url" env:"URL" description:"Webhook url of dingding" required:"true"`
Version bool `short:"v" long:"version" description:"Show version info"`
}
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()
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
}
info := dingToInfo(content)
_, _ = w.Write(info)
2019-11-14 07:35:03 +00:00
}
func dingToInfo(msg string) []byte {
content, data := make(map[string]string), make(map[string]interface{})
content["content"] = msg
data["msgtype"] = "text"
data["text"] = content
b, _ := json.Marshal(data)
log.Info("send to %s data <%s>",
opts.WebHookUrl,
b)
resp, err := http.Post(opts.WebHookUrl,
"application/json",
bytes.NewBuffer(b))
if err != nil {
log.Error("send request to %s %+v",
opts.Addr,
err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
log.Info("send to %s data <%s> result is %s",
opts.WebHookUrl,
b,
body)
return body
}
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
}