This commit is contained in:
ehlxr 2019-11-14 17:18:23 +08:00
parent 1e93285245
commit 06337d2aba

144
main.go
View File

@ -30,9 +30,50 @@ GoVersion: %s
`
bannerBase64 = "DQogX19fXyAgX19fXyAgICBfX18gIF9fX19fIA0KKCAgXyBcKCAgXyBcICAvIF9fKSggIF8gICkNCiApKF8pICkpKF8pICkoIChfLS4gKShfKSggDQooX19fXy8oX19fXy8gIFxfX18vKF9fX19fKQ0K"
opts struct {
Addr string `short:"a" long:"addr" default:"0.0.0.0:10141" env:"ADDR" description:"Addr to listen on for HTTP server"`
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"`
}
)
func init() {
initLog()
}
func main() {
parseArg()
mux := http.NewServeMux()
mux.HandleFunc("/", requestHandle)
server := &http.Server{
Addr: ":4000",
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() {
err := log.NewConsole()
if err != nil {
panic("unable to create new logger: " + err.Error())
@ -51,10 +92,39 @@ func init() {
}
}
var opts struct {
Addr string `short:"a" long:"addr" default:"0.0.0.0:10141" env:"ADDR" description:"Addr to listen on for HTTP server"`
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"`
func parseArg() {
if _, err := flags.NewParser(&opts, flags.Default).Parse(); err != nil {
if opts.Version {
printVersion()
os.Exit(0)
}
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
os.Exit(1)
}
}
}
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)
}
func dingToInfo(msg string) []byte {
@ -88,74 +158,8 @@ func dingToInfo(msg string) []byte {
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)
}
// printVersion Print out version information
func printVersion() {
banner, _ := base64.StdEncoding.DecodeString(bannerBase64)
fmt.Printf(versionTpl, banner, Version, BuildTime, GitCommit, GoVersion)
}
func parseArg() {
if _, err := flags.NewParser(&opts, flags.Default).Parse(); err != nil {
if opts.Version {
printVersion()
os.Exit(0)
}
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
os.Exit(1)
}
}
}
func main() {
parseArg()
mux := http.NewServeMux()
mux.HandleFunc("/", send)
server := &http.Server{
Addr: ":4000",
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")
}
}
}