2021-07-13 18:16:09 +00:00
|
|
|
package router
|
2016-03-24 09:01:43 +00:00
|
|
|
|
|
|
|
import (
|
2019-10-17 15:19:25 +00:00
|
|
|
"context"
|
2017-04-05 04:03:43 +00:00
|
|
|
"crypto/tls"
|
2021-07-16 04:10:34 +00:00
|
|
|
"errors"
|
2016-04-10 08:00:17 +00:00
|
|
|
"fmt"
|
2017-01-19 08:56:30 +00:00
|
|
|
"net/http"
|
2020-02-05 08:57:55 +00:00
|
|
|
"os"
|
2021-07-16 04:10:34 +00:00
|
|
|
"sync"
|
2017-01-19 08:56:30 +00:00
|
|
|
|
2021-07-13 18:16:09 +00:00
|
|
|
"github.com/appleboy/gorush/config"
|
2021-07-16 04:10:34 +00:00
|
|
|
"github.com/appleboy/gorush/core"
|
2021-07-13 18:16:09 +00:00
|
|
|
"github.com/appleboy/gorush/logx"
|
2021-07-13 15:58:47 +00:00
|
|
|
"github.com/appleboy/gorush/metric"
|
2021-07-23 17:56:33 +00:00
|
|
|
"github.com/appleboy/gorush/notify"
|
2021-07-13 15:58:47 +00:00
|
|
|
"github.com/appleboy/gorush/status"
|
|
|
|
|
2019-06-10 18:07:01 +00:00
|
|
|
api "github.com/appleboy/gin-status-api"
|
2020-02-05 08:57:55 +00:00
|
|
|
"github.com/gin-contrib/logger"
|
2016-03-24 16:53:45 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2017-08-06 01:53:38 +00:00
|
|
|
"github.com/gin-gonic/gin/binding"
|
2021-08-15 08:12:08 +00:00
|
|
|
"github.com/golang-queue/queue"
|
2021-07-13 08:32:39 +00:00
|
|
|
"github.com/mattn/go-isatty"
|
2017-01-19 08:56:30 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2020-02-05 08:57:55 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2021-07-13 15:58:47 +00:00
|
|
|
"github.com/thoas/stats"
|
2017-04-05 04:03:43 +00:00
|
|
|
"golang.org/x/crypto/acme/autocert"
|
2016-03-24 09:01:43 +00:00
|
|
|
)
|
|
|
|
|
2021-08-04 01:00:04 +00:00
|
|
|
var doOnce sync.Once
|
2017-01-19 08:56:30 +00:00
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
func abortWithError(c *gin.Context, code int, message string) {
|
2017-03-29 01:21:31 +00:00
|
|
|
c.AbortWithStatusJSON(code, gin.H{
|
2016-03-24 16:46:37 +00:00
|
|
|
"code": code,
|
|
|
|
"message": message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func rootHandler(c *gin.Context) {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2016-03-27 16:04:01 +00:00
|
|
|
"text": "Welcome to notification server.",
|
2016-03-24 16:46:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-26 03:10:51 +00:00
|
|
|
func heartbeatHandler(c *gin.Context) {
|
2017-10-06 02:45:56 +00:00
|
|
|
c.AbortWithStatus(http.StatusOK)
|
2017-09-26 03:10:51 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 03:25:41 +00:00
|
|
|
func versionHandler(c *gin.Context) {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"source": "https://github.com/appleboy/gorush",
|
|
|
|
"version": GetVersion(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-02 06:07:30 +00:00
|
|
|
func pushHandler(cfg *config.ConfYaml, q *queue.Queue) gin.HandlerFunc {
|
2021-07-13 18:16:09 +00:00
|
|
|
return func(c *gin.Context) {
|
2021-07-23 17:56:33 +00:00
|
|
|
var form notify.RequestPush
|
2021-07-13 18:16:09 +00:00
|
|
|
var msg string
|
|
|
|
|
|
|
|
if err := c.ShouldBindWith(&form, binding.JSON); err != nil {
|
|
|
|
msg = "Missing notifications field."
|
|
|
|
logx.LogAccess.Debug(err)
|
|
|
|
abortWithError(c, http.StatusBadRequest, msg)
|
|
|
|
return
|
|
|
|
}
|
2016-03-24 16:46:37 +00:00
|
|
|
|
2021-07-13 18:16:09 +00:00
|
|
|
if len(form.Notifications) == 0 {
|
|
|
|
msg = "Notifications field is empty."
|
|
|
|
logx.LogAccess.Debug(msg)
|
|
|
|
abortWithError(c, http.StatusBadRequest, msg)
|
|
|
|
return
|
2019-10-17 15:19:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 18:16:09 +00:00
|
|
|
if int64(len(form.Notifications)) > cfg.Core.MaxNotification {
|
|
|
|
msg = fmt.Sprintf("Number of notifications(%d) over limit(%d)", len(form.Notifications), cfg.Core.MaxNotification)
|
|
|
|
logx.LogAccess.Debug(msg)
|
|
|
|
abortWithError(c, http.StatusBadRequest, msg)
|
|
|
|
return
|
|
|
|
}
|
2016-03-24 16:46:37 +00:00
|
|
|
|
2021-07-13 18:16:09 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
go func() {
|
|
|
|
// Deprecated: the CloseNotifier interface predates Go's context package.
|
|
|
|
// New code should use Request.Context instead.
|
|
|
|
// Change to context package
|
|
|
|
<-c.Request.Context().Done()
|
|
|
|
// Don't send notification after client timeout or disconnected.
|
|
|
|
// See the following issue for detail information.
|
|
|
|
// https://github.com/appleboy/gorush/issues/422
|
|
|
|
if cfg.Core.Sync {
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
counts, logs := handleNotification(ctx, cfg, form, q)
|
2021-07-13 18:16:09 +00:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": "ok",
|
|
|
|
"counts": counts,
|
|
|
|
"logs": logs,
|
|
|
|
})
|
|
|
|
}
|
2016-03-24 09:01:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 06:07:30 +00:00
|
|
|
func configHandler(cfg *config.ConfYaml) gin.HandlerFunc {
|
2021-07-13 18:16:09 +00:00
|
|
|
return func(c *gin.Context) {
|
|
|
|
c.YAML(http.StatusCreated, cfg)
|
|
|
|
}
|
2016-04-15 07:05:23 +00:00
|
|
|
}
|
|
|
|
|
2017-01-19 08:56:30 +00:00
|
|
|
func metricsHandler(c *gin.Context) {
|
|
|
|
promhttp.Handler().ServeHTTP(c.Writer, c.Request)
|
|
|
|
}
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
func appStatusHandler(q *queue.Queue) gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
result := status.App{}
|
|
|
|
|
|
|
|
result.Version = GetVersion()
|
|
|
|
result.QueueMax = q.Capacity()
|
|
|
|
result.QueueUsage = q.Usage()
|
|
|
|
result.TotalCount = status.StatStorage.GetTotalCount()
|
|
|
|
result.Ios.PushSuccess = status.StatStorage.GetIosSuccess()
|
|
|
|
result.Ios.PushError = status.StatStorage.GetIosError()
|
|
|
|
result.Android.PushSuccess = status.StatStorage.GetAndroidSuccess()
|
|
|
|
result.Android.PushError = status.StatStorage.GetAndroidError()
|
|
|
|
result.Huawei.PushSuccess = status.StatStorage.GetHuaweiSuccess()
|
|
|
|
result.Huawei.PushError = status.StatStorage.GetHuaweiError()
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
|
|
}
|
2021-07-13 15:58:47 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 18:16:09 +00:00
|
|
|
func sysStatsHandler() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
c.JSON(http.StatusOK, status.Stats.Data())
|
|
|
|
}
|
2021-07-13 15:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StatMiddleware response time, status code count, etc.
|
|
|
|
func StatMiddleware() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
beginning, recorder := status.Stats.Begin(c.Writer)
|
|
|
|
c.Next()
|
|
|
|
status.Stats.End(beginning, stats.WithRecorder(recorder))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 06:07:30 +00:00
|
|
|
func autoTLSServer(cfg *config.ConfYaml, q *queue.Queue) *http.Server {
|
2017-04-05 04:03:43 +00:00
|
|
|
m := autocert.Manager{
|
|
|
|
Prompt: autocert.AcceptTOS,
|
2021-07-13 18:16:09 +00:00
|
|
|
HostPolicy: autocert.HostWhitelist(cfg.Core.AutoTLS.Host),
|
|
|
|
Cache: autocert.DirCache(cfg.Core.AutoTLS.Folder),
|
2017-04-05 04:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &http.Server{
|
|
|
|
Addr: ":https",
|
|
|
|
TLSConfig: &tls.Config{GetCertificate: m.GetCertificate},
|
2021-07-16 04:10:34 +00:00
|
|
|
Handler: routerEngine(cfg, q),
|
2017-04-05 04:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 06:07:30 +00:00
|
|
|
func routerEngine(cfg *config.ConfYaml, q *queue.Queue) *gin.Engine {
|
2020-02-05 08:57:55 +00:00
|
|
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
2021-07-13 18:16:09 +00:00
|
|
|
if cfg.Core.Mode == "debug" {
|
2020-02-05 08:57:55 +00:00
|
|
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
|
|
|
|
|
2021-08-04 01:00:04 +00:00
|
|
|
isTerm := isatty.IsTerminal(os.Stdout.Fd())
|
2020-02-05 08:57:55 +00:00
|
|
|
if isTerm {
|
|
|
|
log.Logger = log.Output(
|
|
|
|
zerolog.ConsoleWriter{
|
|
|
|
Out: os.Stdout,
|
|
|
|
NoColor: false,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
// Support metrics
|
|
|
|
doOnce.Do(func() {
|
|
|
|
m := metric.NewMetrics(func() int {
|
|
|
|
return q.Usage()
|
|
|
|
})
|
|
|
|
prometheus.MustRegister(m)
|
|
|
|
})
|
|
|
|
|
2016-04-01 01:53:10 +00:00
|
|
|
// set server mode
|
2021-07-13 18:16:09 +00:00
|
|
|
gin.SetMode(cfg.Core.Mode)
|
2016-03-27 13:12:11 +00:00
|
|
|
|
2016-03-24 09:01:43 +00:00
|
|
|
r := gin.New()
|
|
|
|
|
|
|
|
// Global middleware
|
2021-07-11 15:01:19 +00:00
|
|
|
r.Use(logger.SetLogger(
|
|
|
|
logger.WithUTC(true),
|
|
|
|
logger.WithSkipPath([]string{
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg.API.HealthURI,
|
|
|
|
cfg.API.MetricURI,
|
2021-07-11 15:01:19 +00:00
|
|
|
}),
|
|
|
|
))
|
2016-03-24 09:01:43 +00:00
|
|
|
r.Use(gin.Recovery())
|
2016-03-25 01:23:46 +00:00
|
|
|
r.Use(VersionMiddleware())
|
2016-06-26 04:21:32 +00:00
|
|
|
r.Use(StatMiddleware())
|
2016-03-24 09:01:43 +00:00
|
|
|
|
2021-07-13 18:16:09 +00:00
|
|
|
r.GET(cfg.API.StatGoURI, api.GinHandler)
|
2021-07-16 04:10:34 +00:00
|
|
|
r.GET(cfg.API.StatAppURI, appStatusHandler(q))
|
2021-07-13 18:16:09 +00:00
|
|
|
r.GET(cfg.API.ConfigURI, configHandler(cfg))
|
|
|
|
r.GET(cfg.API.SysStatURI, sysStatsHandler())
|
2021-07-16 04:10:34 +00:00
|
|
|
r.POST(cfg.API.PushURI, pushHandler(cfg, q))
|
2021-07-13 18:16:09 +00:00
|
|
|
r.GET(cfg.API.MetricURI, metricsHandler)
|
|
|
|
r.GET(cfg.API.HealthURI, heartbeatHandler)
|
|
|
|
r.HEAD(cfg.API.HealthURI, heartbeatHandler)
|
2017-09-26 03:25:41 +00:00
|
|
|
r.GET("/version", versionHandler)
|
2016-03-24 09:01:43 +00:00
|
|
|
r.GET("/", rootHandler)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
2021-07-16 04:10:34 +00:00
|
|
|
|
|
|
|
// markFailedNotification adds failure logs for all tokens in push notification
|
2021-08-02 06:07:30 +00:00
|
|
|
func markFailedNotification(cfg *config.ConfYaml, notification *notify.PushNotification, reason string) []logx.LogPushEntry {
|
2021-07-16 04:10:34 +00:00
|
|
|
logx.LogError.Error(reason)
|
2021-08-01 09:12:16 +00:00
|
|
|
logs := make([]logx.LogPushEntry, 0)
|
2021-07-16 04:10:34 +00:00
|
|
|
for _, token := range notification.Tokens {
|
2021-08-01 09:12:16 +00:00
|
|
|
logs = append(logs, logx.GetLogPushEntry(&logx.InputLog{
|
2021-07-16 04:10:34 +00:00
|
|
|
ID: notification.ID,
|
|
|
|
Status: core.FailedPush,
|
|
|
|
Token: token,
|
|
|
|
Message: notification.Message,
|
|
|
|
Platform: notification.Platform,
|
|
|
|
Error: errors.New(reason),
|
|
|
|
HideToken: cfg.Log.HideToken,
|
|
|
|
Format: cfg.Log.Format,
|
|
|
|
}))
|
|
|
|
}
|
2021-08-01 09:12:16 +00:00
|
|
|
|
|
|
|
return logs
|
2021-07-16 04:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleNotification add notification to queue list.
|
2021-08-02 06:07:30 +00:00
|
|
|
func handleNotification(ctx context.Context, cfg *config.ConfYaml, req notify.RequestPush, q *queue.Queue) (int, []logx.LogPushEntry) {
|
2021-07-16 04:10:34 +00:00
|
|
|
var count int
|
|
|
|
wg := sync.WaitGroup{}
|
2021-07-23 17:56:33 +00:00
|
|
|
newNotification := []*notify.PushNotification{}
|
2021-07-17 12:14:19 +00:00
|
|
|
|
|
|
|
if cfg.Core.Sync && !core.IsLocalQueue(core.Queue(cfg.Queue.Engine)) {
|
|
|
|
cfg.Core.Sync = false
|
|
|
|
}
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
for i := range req.Notifications {
|
|
|
|
notification := &req.Notifications[i]
|
|
|
|
switch notification.Platform {
|
|
|
|
case core.PlatFormIos:
|
|
|
|
if !cfg.Ios.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case core.PlatFormAndroid:
|
|
|
|
if !cfg.Android.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case core.PlatFormHuawei:
|
|
|
|
if !cfg.Huawei.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newNotification = append(newNotification, notification)
|
|
|
|
}
|
|
|
|
|
2021-08-01 09:12:16 +00:00
|
|
|
logs := make([]logx.LogPushEntry, 0, count)
|
2021-07-16 04:10:34 +00:00
|
|
|
for _, notification := range newNotification {
|
|
|
|
if cfg.Core.Sync {
|
2021-08-01 09:12:16 +00:00
|
|
|
wg.Add(1)
|
2021-07-16 04:10:34 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 09:12:16 +00:00
|
|
|
if core.IsLocalQueue(core.Queue(cfg.Queue.Engine)) && cfg.Core.Sync {
|
2021-08-02 06:07:30 +00:00
|
|
|
func(msg *notify.PushNotification, cfg *config.ConfYaml) {
|
2021-08-03 06:44:00 +00:00
|
|
|
if err := q.QueueTask(func(ctx context.Context) error {
|
2021-08-01 09:12:16 +00:00
|
|
|
defer wg.Done()
|
2021-08-01 11:48:57 +00:00
|
|
|
resp, err := notify.SendNotification(msg, cfg)
|
2021-08-01 09:12:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// add log
|
2021-08-03 06:44:00 +00:00
|
|
|
logs = append(logs, resp.Logs...)
|
2021-08-01 09:12:16 +00:00
|
|
|
|
|
|
|
return nil
|
2021-08-03 06:44:00 +00:00
|
|
|
}); err != nil {
|
|
|
|
logx.LogError.Error(err)
|
|
|
|
}
|
2021-08-01 11:48:57 +00:00
|
|
|
}(notification, cfg)
|
2021-08-01 09:12:16 +00:00
|
|
|
} else if err := q.Queue(notification); err != nil {
|
|
|
|
resp := markFailedNotification(cfg, notification, "max capacity reached")
|
|
|
|
// add log
|
2021-08-03 06:44:00 +00:00
|
|
|
logs = append(logs, resp...)
|
2021-08-01 09:12:16 +00:00
|
|
|
wg.Done()
|
2021-07-16 04:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
count += len(notification.Tokens)
|
|
|
|
// Count topic message
|
|
|
|
if notification.To != "" {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Core.Sync {
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
status.StatStorage.AddTotalCount(int64(count))
|
|
|
|
|
2021-08-01 09:12:16 +00:00
|
|
|
return count, logs
|
2021-07-16 04:10:34 +00:00
|
|
|
}
|