This commit is contained in:
ehlxr 2019-11-14 15:35:03 +08:00
commit a93853e69f
4 changed files with 137 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/ddgo.log
/ddgo

9
go.mod Normal file
View File

@ -0,0 +1,9 @@
module github.com/ehlxr/ddgo
go 1.13
require (
github.com/jessevdk/go-flags v1.4.1-0.20181221193153-c0795c8afcf4
golang.org/x/sys v0.0.0-20191113165036-4c7a9d0fe056 // indirect
unknwon.dev/clog/v2 v2.0.0-beta.5
)

22
go.sum Normal file
View File

@ -0,0 +1,22 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/jessevdk/go-flags v1.4.1-0.20181221193153-c0795c8afcf4 h1:xKkUL6QBojwguhKKetf1SocCAKqc6W7S/mGm9xEGllo=
github.com/jessevdk/go-flags v1.4.1-0.20181221193153-c0795c8afcf4/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191008105621-543471e840be h1:QAcqgptGM8IQBC9K/RC4o+O9YmqEm0diQn9QmZw/0mU=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191113165036-4c7a9d0fe056 h1:dHtDnRWQtSx0Hjq9kvKFpBh9uPPKfQN70NZZmvssGwk=
golang.org/x/sys v0.0.0-20191113165036-4c7a9d0fe056/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
unknwon.dev/clog/v2 v2.0.0-beta.5 h1:A0F7S8765S965KXyba4l1tQBW9908/lcFfkCkHmYRb4=
unknwon.dev/clog/v2 v2.0.0-beta.5/go.mod h1:zvUlyibDHI4mykYdWyWje2G9nF/nBzfDOqRo2my4mWc=

104
main.go Normal file
View File

@ -0,0 +1,104 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/jessevdk/go-flags"
"io"
"io/ioutil"
"net/http"
log "unknwon.dev/clog/v2"
)
func init() {
err := log.NewConsole()
if err != nil {
panic("unable to create new logger: " + err.Error())
}
err = log.NewFile(log.FileConfig{
Level: log.LevelInfo,
Filename: "ddgo.log",
FileRotationConfig: log.FileRotationConfig{
Rotate: true,
Daily: true,
},
})
if err != nil {
panic("unable to create new logger: " + err.Error())
}
}
var opts struct {
Addr string `short:"a" long:"addr" default:"0.0.0.0:10141" env:"ADDR" description:"Addr to listen on for http requests"`
WebHookUrl string `short:"u" long:"webhook-url" env:"URL" description:"Webhook url of dingding" required:"true"`
}
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
}
func send(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)
}
func main() {
_, err := flags.Parse(&opts)
if err != nil {
log.Fatal("parse arg %+v",
err)
}
http.HandleFunc("/", send)
log.Info("server on http://%s", opts.Addr)
if err := http.ListenAndServe(opts.Addr, nil); err != nil {
log.Fatal("ListenAndServe %+v",
err)
}
}