119 lines
2.6 KiB
Go
119 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/rs/zerolog"
|
|
"github.com/rs/zerolog/log"
|
|
"gopkg.in/gomail.v2"
|
|
"kumoly.io/kumoly/app/email"
|
|
"kumoly.io/lib/guard"
|
|
)
|
|
|
|
var (
|
|
fName string
|
|
fLink string
|
|
fLogo string
|
|
fTroubleText string
|
|
|
|
fHost string
|
|
fPort int
|
|
fUsr string
|
|
fPwd string
|
|
|
|
flagAddr string
|
|
fSAllow string
|
|
|
|
flagLogLevel int
|
|
flagLogPretty bool
|
|
)
|
|
|
|
func init() {
|
|
flag.StringVar(&fName, "name", "", "name of the app")
|
|
flag.StringVar(&fLink, "link", "", "link url of the app")
|
|
flag.StringVar(&fLogo, "logo", "", "logo url of the app")
|
|
flag.StringVar(&fTroubleText, "no-link-text", "", "TroubleText of the app")
|
|
|
|
flag.StringVar(&fHost, "s-host", "mail.kumoly.io", "SMTP host")
|
|
flag.IntVar(&fPort, "s-port", 587, "SMTP port")
|
|
flag.StringVar(&fUsr, "s-user", "test@kumoly.io", "SMTP user")
|
|
flag.StringVar(&fPwd, "s-passwd", "test", "SMTP password")
|
|
|
|
flag.StringVar(&flagAddr, "addr", ":8000", "address to listen")
|
|
flag.StringVar(&fSAllow, "allow", "", "allowed ipnet")
|
|
|
|
flag.IntVar(&flagLogLevel, "level", 1, "log level [-1(trace):5(panic)] 7 to disable")
|
|
flag.BoolVar(&flagLogPretty, "pretty", false, "log message in human readable format (the original log is json)")
|
|
}
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
|
|
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
|
|
zerolog.SetGlobalLevel(zerolog.Level(flagLogLevel))
|
|
if flagLogPretty {
|
|
log.Logger = log.Output(zerolog.ConsoleWriter{
|
|
Out: os.Stdout,
|
|
TimeFormat: "2006/01/02 15:04:05",
|
|
})
|
|
}
|
|
|
|
email.SetDefault(email.App{
|
|
Name: fName,
|
|
Link: fLink,
|
|
Logo: fLogo,
|
|
Copyright: fmt.Sprintf("Copyright © %v %s. All rights reserved.",
|
|
time.Now().Year(), fName),
|
|
TroubleText: fTroubleText,
|
|
})
|
|
email.SetDailer(gomail.NewDialer(fHost, fPort, fUsr, fPwd))
|
|
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
data, err := ioutil.ReadAll(r.Body)
|
|
r.Body.Close()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
var e Email
|
|
if err := json.Unmarshal(data, &e); err != nil {
|
|
panic(err)
|
|
}
|
|
if err := email.Send(e.From, e.To, e.Title, &e.Body); err != nil {
|
|
panic(err)
|
|
}
|
|
w.Write([]byte("ok"))
|
|
})
|
|
|
|
g := guard.New()
|
|
g.Skip = guard.SkipStatic
|
|
|
|
server := &http.Server{
|
|
Addr: flagAddr,
|
|
Handler: g.Guard(mux),
|
|
}
|
|
|
|
log.Info().Msgf("%s started at %s", fName, flagAddr)
|
|
err := server.ListenAndServe()
|
|
if err != nil {
|
|
log.Error().Err(err)
|
|
}
|
|
}
|
|
|
|
type Email struct {
|
|
From string
|
|
To []string
|
|
Title string
|
|
Body email.Body
|
|
}
|