chore: create router package (#586)

This commit is contained in:
Bo-Yi Wu 2021-07-14 02:16:09 +08:00 committed by GitHub
parent 2cd46ad690
commit ca7dcbd8dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 231 additions and 199 deletions

View File

@ -597,7 +597,7 @@ func TestDisabledIosNotifications(t *testing.T) {
}, },
} }
count, logs := queueNotification(ctx, req) count, logs := HandleNotification(ctx, req)
assert.Equal(t, 2, count) assert.Equal(t, 2, count)
assert.Equal(t, 0, len(logs)) assert.Equal(t, 0, len(logs))
} }

View File

@ -55,7 +55,7 @@ func TestSenMultipleNotifications(t *testing.T) {
}, },
} }
count, logs := queueNotification(ctx, req) count, logs := HandleNotification(ctx, req)
assert.Equal(t, 3, count) assert.Equal(t, 3, count)
assert.Equal(t, 0, len(logs)) assert.Equal(t, 0, len(logs))
} }
@ -91,7 +91,7 @@ func TestDisabledAndroidNotifications(t *testing.T) {
}, },
} }
count, logs := queueNotification(ctx, req) count, logs := HandleNotification(ctx, req)
assert.Equal(t, 1, count) assert.Equal(t, 1, count)
assert.Equal(t, 0, len(logs)) assert.Equal(t, 0, len(logs))
} }
@ -130,7 +130,7 @@ func TestSyncModeForNotifications(t *testing.T) {
}, },
} }
count, logs := queueNotification(ctx, req) count, logs := HandleNotification(ctx, req)
assert.Equal(t, 3, count) assert.Equal(t, 3, count)
assert.Equal(t, 2, len(logs)) assert.Equal(t, 2, len(logs))
} }
@ -173,7 +173,7 @@ func TestSyncModeForTopicNotification(t *testing.T) {
}, },
} }
count, logs := queueNotification(ctx, req) count, logs := HandleNotification(ctx, req)
assert.Equal(t, 2, count) assert.Equal(t, 2, count)
assert.Equal(t, 1, len(logs)) assert.Equal(t, 1, len(logs))
} }
@ -200,7 +200,7 @@ func TestSyncModeForDeviceGroupNotification(t *testing.T) {
}, },
} }
count, logs := queueNotification(ctx, req) count, logs := HandleNotification(ctx, req)
assert.Equal(t, 1, count) assert.Equal(t, 1, count)
assert.Equal(t, 1, len(logs)) assert.Equal(t, 1, len(logs))
} }

View File

@ -1,21 +0,0 @@
// +build lambda
package gorush
import (
"context"
"github.com/apex/gateway"
)
// RunHTTPServer provide run http or https protocol.
func RunHTTPServer(ctx context.Context) error {
if !PushConf.Core.Enabled {
LogAccess.Debug("httpd server is disabled.")
return nil
}
LogAccess.Info("HTTPD server is running on " + PushConf.Core.Port + " port.")
return gateway.ListenAndServe(PushConf.Core.Address+":"+PushConf.Core.Port, routerEngine())
}

View File

@ -61,8 +61,8 @@ func markFailedNotification(notification *PushNotification, reason string) {
notification.WaitDone() notification.WaitDone()
} }
// queueNotification add notification to queue list. // HandleNotification add notification to queue list.
func queueNotification(ctx context.Context, req RequestPush) (int, []logx.LogPushEntry) { func HandleNotification(ctx context.Context, req RequestPush) (int, []logx.LogPushEntry) {
var count int var count int
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
newNotification := []*PushNotification{} newNotification := []*PushNotification{}

View File

@ -19,6 +19,7 @@ import (
"github.com/appleboy/gorush/core" "github.com/appleboy/gorush/core"
"github.com/appleboy/gorush/gorush" "github.com/appleboy/gorush/gorush"
"github.com/appleboy/gorush/logx" "github.com/appleboy/gorush/logx"
"github.com/appleboy/gorush/router"
"github.com/appleboy/gorush/rpc" "github.com/appleboy/gorush/rpc"
"github.com/appleboy/gorush/status" "github.com/appleboy/gorush/status"
@ -96,11 +97,11 @@ func main() {
flag.Usage = usage flag.Usage = usage
flag.Parse() flag.Parse()
gorush.SetVersion(Version) router.SetVersion(Version)
// Show version and exit // Show version and exit
if showVersion { if showVersion {
gorush.PrintGoRushVersion() router.PrintGoRushVersion()
os.Exit(0) os.Exit(0)
} }
@ -353,7 +354,7 @@ func main() {
// Run httpd server // Run httpd server
g.Go(func() error { g.Go(func() error {
return gorush.RunHTTPServer(ctx) return router.RunHTTPServer(ctx, gorush.PushConf)
}) })
// Run gRPC internal server // Run gRPC internal server

View File

@ -1,4 +1,4 @@
package gorush package router
import ( import (
"context" "context"
@ -7,11 +7,13 @@ import (
"net/http" "net/http"
"os" "os"
"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/gorush"
"github.com/appleboy/gorush/logx"
"github.com/appleboy/gorush/metric" "github.com/appleboy/gorush/metric"
"github.com/appleboy/gorush/status" "github.com/appleboy/gorush/status"
api "github.com/appleboy/gin-status-api" api "github.com/appleboy/gin-status-api"
"github.com/appleboy/gorush/logx"
"github.com/gin-contrib/logger" "github.com/gin-contrib/logger"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding" "github.com/gin-gonic/gin/binding"
@ -29,7 +31,7 @@ var isTerm bool
func init() { func init() {
// Support metrics // Support metrics
m := metric.NewMetrics(func() int { m := metric.NewMetrics(func() int {
return len(QueueNotification) return len(gorush.QueueNotification)
}) })
prometheus.MustRegister(m) prometheus.MustRegister(m)
isTerm = isatty.IsTerminal(os.Stdout.Fd()) isTerm = isatty.IsTerminal(os.Stdout.Fd())
@ -59,8 +61,9 @@ func versionHandler(c *gin.Context) {
}) })
} }
func pushHandler(c *gin.Context) { func pushHandler(cfg config.ConfYaml) gin.HandlerFunc {
var form RequestPush return func(c *gin.Context) {
var form gorush.RequestPush
var msg string var msg string
if err := c.ShouldBindWith(&form, binding.JSON); err != nil { if err := c.ShouldBindWith(&form, binding.JSON); err != nil {
@ -77,8 +80,8 @@ func pushHandler(c *gin.Context) {
return return
} }
if int64(len(form.Notifications)) > PushConf.Core.MaxNotification { if int64(len(form.Notifications)) > cfg.Core.MaxNotification {
msg = fmt.Sprintf("Number of notifications(%d) over limit(%d)", len(form.Notifications), PushConf.Core.MaxNotification) msg = fmt.Sprintf("Number of notifications(%d) over limit(%d)", len(form.Notifications), cfg.Core.MaxNotification)
logx.LogAccess.Debug(msg) logx.LogAccess.Debug(msg)
abortWithError(c, http.StatusBadRequest, msg) abortWithError(c, http.StatusBadRequest, msg)
return return
@ -93,12 +96,12 @@ func pushHandler(c *gin.Context) {
// Don't send notification after client timeout or disconnected. // Don't send notification after client timeout or disconnected.
// See the following issue for detail information. // See the following issue for detail information.
// https://github.com/appleboy/gorush/issues/422 // https://github.com/appleboy/gorush/issues/422
if PushConf.Core.Sync { if cfg.Core.Sync {
cancel() cancel()
} }
}() }()
counts, logs := queueNotification(ctx, form) counts, logs := gorush.HandleNotification(ctx, form)
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"success": "ok", "success": "ok",
@ -106,9 +109,12 @@ func pushHandler(c *gin.Context) {
"logs": logs, "logs": logs,
}) })
} }
}
func configHandler(c *gin.Context) { func configHandler(cfg config.ConfYaml) gin.HandlerFunc {
c.YAML(http.StatusCreated, PushConf) return func(c *gin.Context) {
c.YAML(http.StatusCreated, cfg)
}
} }
func metricsHandler(c *gin.Context) { func metricsHandler(c *gin.Context) {
@ -119,8 +125,8 @@ func appStatusHandler(c *gin.Context) {
result := status.App{} result := status.App{}
result.Version = GetVersion() result.Version = GetVersion()
result.QueueMax = cap(QueueNotification) result.QueueMax = cap(gorush.QueueNotification)
result.QueueUsage = len(QueueNotification) result.QueueUsage = len(gorush.QueueNotification)
result.TotalCount = status.StatStorage.GetTotalCount() result.TotalCount = status.StatStorage.GetTotalCount()
result.Ios.PushSuccess = status.StatStorage.GetIosSuccess() result.Ios.PushSuccess = status.StatStorage.GetIosSuccess()
result.Ios.PushError = status.StatStorage.GetIosError() result.Ios.PushError = status.StatStorage.GetIosError()
@ -132,9 +138,11 @@ func appStatusHandler(c *gin.Context) {
c.JSON(http.StatusOK, result) c.JSON(http.StatusOK, result)
} }
func sysStatsHandler(c *gin.Context) { func sysStatsHandler() gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(http.StatusOK, status.Stats.Data()) c.JSON(http.StatusOK, status.Stats.Data())
} }
}
// StatMiddleware response time, status code count, etc. // StatMiddleware response time, status code count, etc.
func StatMiddleware() gin.HandlerFunc { func StatMiddleware() gin.HandlerFunc {
@ -145,23 +153,23 @@ func StatMiddleware() gin.HandlerFunc {
} }
} }
func autoTLSServer() *http.Server { func autoTLSServer(cfg config.ConfYaml) *http.Server {
m := autocert.Manager{ m := autocert.Manager{
Prompt: autocert.AcceptTOS, Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(PushConf.Core.AutoTLS.Host), HostPolicy: autocert.HostWhitelist(cfg.Core.AutoTLS.Host),
Cache: autocert.DirCache(PushConf.Core.AutoTLS.Folder), Cache: autocert.DirCache(cfg.Core.AutoTLS.Folder),
} }
return &http.Server{ return &http.Server{
Addr: ":https", Addr: ":https",
TLSConfig: &tls.Config{GetCertificate: m.GetCertificate}, TLSConfig: &tls.Config{GetCertificate: m.GetCertificate},
Handler: routerEngine(), Handler: routerEngine(cfg),
} }
} }
func routerEngine() *gin.Engine { func routerEngine(cfg config.ConfYaml) *gin.Engine {
zerolog.SetGlobalLevel(zerolog.InfoLevel) zerolog.SetGlobalLevel(zerolog.InfoLevel)
if PushConf.Core.Mode == "debug" { if cfg.Core.Mode == "debug" {
zerolog.SetGlobalLevel(zerolog.DebugLevel) zerolog.SetGlobalLevel(zerolog.DebugLevel)
} }
@ -177,7 +185,7 @@ func routerEngine() *gin.Engine {
} }
// set server mode // set server mode
gin.SetMode(PushConf.Core.Mode) gin.SetMode(cfg.Core.Mode)
r := gin.New() r := gin.New()
@ -185,22 +193,22 @@ func routerEngine() *gin.Engine {
r.Use(logger.SetLogger( r.Use(logger.SetLogger(
logger.WithUTC(true), logger.WithUTC(true),
logger.WithSkipPath([]string{ logger.WithSkipPath([]string{
PushConf.API.HealthURI, cfg.API.HealthURI,
PushConf.API.MetricURI, cfg.API.MetricURI,
}), }),
)) ))
r.Use(gin.Recovery()) r.Use(gin.Recovery())
r.Use(VersionMiddleware()) r.Use(VersionMiddleware())
r.Use(StatMiddleware()) r.Use(StatMiddleware())
r.GET(PushConf.API.StatGoURI, api.GinHandler) r.GET(cfg.API.StatGoURI, api.GinHandler)
r.GET(PushConf.API.StatAppURI, appStatusHandler) r.GET(cfg.API.StatAppURI, appStatusHandler)
r.GET(PushConf.API.ConfigURI, configHandler) r.GET(cfg.API.ConfigURI, configHandler(cfg))
r.GET(PushConf.API.SysStatURI, sysStatsHandler) r.GET(cfg.API.SysStatURI, sysStatsHandler())
r.POST(PushConf.API.PushURI, pushHandler) r.POST(cfg.API.PushURI, pushHandler(cfg))
r.GET(PushConf.API.MetricURI, metricsHandler) r.GET(cfg.API.MetricURI, metricsHandler)
r.GET(PushConf.API.HealthURI, heartbeatHandler) r.GET(cfg.API.HealthURI, heartbeatHandler)
r.HEAD(PushConf.API.HealthURI, heartbeatHandler) r.HEAD(cfg.API.HealthURI, heartbeatHandler)
r.GET("/version", versionHandler) r.GET("/version", versionHandler)
r.GET("/", rootHandler) r.GET("/", rootHandler)

21
router/server_lambda.go Normal file
View File

@ -0,0 +1,21 @@
// +build lambda
package router
import (
"context"
"github.com/apex/gateway"
)
// RunHTTPServer provide run http or https protocol.
func RunHTTPServer(ctx context.Context, cfg config.ConfYaml, s ...*http.Server) (err error) {
if !cfg.Core.Enabled {
LogAccess.Debug("httpd server is disabled.")
return nil
}
LogAccess.Info("HTTPD server is running on " + cfg.Core.Port + " port.")
return gateway.ListenAndServe(cfg.Core.Address+":"+cfg.Core.Port, routerEngine())
}

View File

@ -1,6 +1,6 @@
// +build !lambda // +build !lambda
package gorush package router
import ( import (
"context" "context"
@ -10,33 +10,34 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/logx" "github.com/appleboy/gorush/logx"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
// RunHTTPServer provide run http or https protocol. // RunHTTPServer provide run http or https protocol.
func RunHTTPServer(ctx context.Context, s ...*http.Server) (err error) { func RunHTTPServer(ctx context.Context, cfg config.ConfYaml, s ...*http.Server) (err error) {
var server *http.Server var server *http.Server
if !PushConf.Core.Enabled { if !cfg.Core.Enabled {
logx.LogAccess.Info("httpd server is disabled.") logx.LogAccess.Info("httpd server is disabled.")
return nil return nil
} }
if len(s) == 0 { if len(s) == 0 {
server = &http.Server{ server = &http.Server{
Addr: PushConf.Core.Address + ":" + PushConf.Core.Port, Addr: cfg.Core.Address + ":" + cfg.Core.Port,
Handler: routerEngine(), Handler: routerEngine(cfg),
} }
} else { } else {
server = s[0] server = s[0]
} }
logx.LogAccess.Info("HTTPD server is running on " + PushConf.Core.Port + " port.") logx.LogAccess.Info("HTTPD server is running on " + cfg.Core.Port + " port.")
if PushConf.Core.AutoTLS.Enabled { if cfg.Core.AutoTLS.Enabled {
return startServer(ctx, autoTLSServer()) return startServer(ctx, autoTLSServer(cfg), cfg)
} else if PushConf.Core.SSL { } else if cfg.Core.SSL {
config := &tls.Config{ config := &tls.Config{
MinVersion: tls.VersionTLS10, MinVersion: tls.VersionTLS10,
} }
@ -46,19 +47,19 @@ func RunHTTPServer(ctx context.Context, s ...*http.Server) (err error) {
} }
config.Certificates = make([]tls.Certificate, 1) config.Certificates = make([]tls.Certificate, 1)
if PushConf.Core.CertPath != "" && PushConf.Core.KeyPath != "" { if cfg.Core.CertPath != "" && cfg.Core.KeyPath != "" {
config.Certificates[0], err = tls.LoadX509KeyPair(PushConf.Core.CertPath, PushConf.Core.KeyPath) config.Certificates[0], err = tls.LoadX509KeyPair(cfg.Core.CertPath, cfg.Core.KeyPath)
if err != nil { if err != nil {
logx.LogError.Error("Failed to load https cert file: ", err) logx.LogError.Error("Failed to load https cert file: ", err)
return err return err
} }
} else if PushConf.Core.CertBase64 != "" && PushConf.Core.KeyBase64 != "" { } else if cfg.Core.CertBase64 != "" && cfg.Core.KeyBase64 != "" {
cert, err := base64.StdEncoding.DecodeString(PushConf.Core.CertBase64) cert, err := base64.StdEncoding.DecodeString(cfg.Core.CertBase64)
if err != nil { if err != nil {
logx.LogError.Error("base64 decode error:", err.Error()) logx.LogError.Error("base64 decode error:", err.Error())
return err return err
} }
key, err := base64.StdEncoding.DecodeString(PushConf.Core.KeyBase64) key, err := base64.StdEncoding.DecodeString(cfg.Core.KeyBase64)
if err != nil { if err != nil {
logx.LogError.Error("base64 decode error:", err.Error()) logx.LogError.Error("base64 decode error:", err.Error())
return err return err
@ -74,15 +75,15 @@ func RunHTTPServer(ctx context.Context, s ...*http.Server) (err error) {
server.TLSConfig = config server.TLSConfig = config
} }
return startServer(ctx, server) return startServer(ctx, server, cfg)
} }
func listenAndServe(ctx context.Context, s *http.Server) error { func listenAndServe(ctx context.Context, s *http.Server, cfg config.ConfYaml) error {
var g errgroup.Group var g errgroup.Group
g.Go(func() error { g.Go(func() error {
select { select {
case <-ctx.Done(): case <-ctx.Done():
timeout := time.Duration(PushConf.Core.ShutdownTimeout) * time.Second timeout := time.Duration(cfg.Core.ShutdownTimeout) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout) ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel() defer cancel()
return s.Shutdown(ctx) return s.Shutdown(ctx)
@ -97,12 +98,12 @@ func listenAndServe(ctx context.Context, s *http.Server) error {
return g.Wait() return g.Wait()
} }
func listenAndServeTLS(ctx context.Context, s *http.Server) error { func listenAndServeTLS(ctx context.Context, s *http.Server, cfg config.ConfYaml) error {
var g errgroup.Group var g errgroup.Group
g.Go(func() error { g.Go(func() error {
select { select {
case <-ctx.Done(): case <-ctx.Done():
timeout := time.Duration(PushConf.Core.ShutdownTimeout) * time.Second timeout := time.Duration(cfg.Core.ShutdownTimeout) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout) ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel() defer cancel()
return s.Shutdown(ctx) return s.Shutdown(ctx)
@ -117,10 +118,10 @@ func listenAndServeTLS(ctx context.Context, s *http.Server) error {
return g.Wait() return g.Wait()
} }
func startServer(ctx context.Context, s *http.Server) error { func startServer(ctx context.Context, s *http.Server, cfg config.ConfYaml) error {
if s.TLSConfig == nil { if s.TLSConfig == nil {
return listenAndServe(ctx, s) return listenAndServe(ctx, s, cfg)
} }
return listenAndServeTLS(ctx, s) return listenAndServeTLS(ctx, s, cfg)
} }

View File

@ -1,4 +1,4 @@
package gorush package router
import ( import (
"context" "context"
@ -13,6 +13,9 @@ import (
"github.com/appleboy/gorush/config" "github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/core" "github.com/appleboy/gorush/core"
"github.com/appleboy/gorush/gorush"
"github.com/appleboy/gorush/logx"
"github.com/appleboy/gorush/status"
"github.com/appleboy/gofight/v2" "github.com/appleboy/gofight/v2"
"github.com/buger/jsonparser" "github.com/buger/jsonparser"
@ -22,9 +25,28 @@ import (
var goVersion = runtime.Version() var goVersion = runtime.Version()
func initTest() { func TestMain(m *testing.M) {
PushConf, _ = config.LoadConf("") cfg := initTest()
PushConf.Core.Mode = "test" if err := logx.InitLog(
cfg.Log.AccessLevel,
cfg.Log.AccessLog,
cfg.Log.ErrorLevel,
cfg.Log.ErrorLog,
); err != nil {
log.Fatal(err)
}
if err := status.InitAppStatus(cfg); err != nil {
log.Fatal(err)
}
m.Run()
}
func initTest() config.ConfYaml {
cfg, _ := config.LoadConf("")
cfg.Core.Mode = "test"
return cfg
} }
// testRequest is testing url string if server is running // testRequest is testing url string if server is running
@ -60,13 +82,13 @@ func TestPrintGoRushVersion(t *testing.T) {
} }
func TestRunNormalServer(t *testing.T) { func TestRunNormalServer(t *testing.T) {
initTest() cfg := initTest()
gin.SetMode(gin.TestMode) gin.SetMode(gin.TestMode)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
go func() { go func() {
assert.NoError(t, RunHTTPServer(ctx)) assert.NoError(t, RunHTTPServer(ctx, cfg))
}() }()
defer func() { defer func() {
@ -81,16 +103,16 @@ func TestRunNormalServer(t *testing.T) {
} }
func TestRunTLSServer(t *testing.T) { func TestRunTLSServer(t *testing.T) {
initTest() cfg := initTest()
PushConf.Core.SSL = true cfg.Core.SSL = true
PushConf.Core.Port = "8087" cfg.Core.Port = "8087"
PushConf.Core.CertPath = "../certificate/localhost.cert" cfg.Core.CertPath = "../certificate/localhost.cert"
PushConf.Core.KeyPath = "../certificate/localhost.key" cfg.Core.KeyPath = "../certificate/localhost.key"
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
go func() { go func() {
assert.NoError(t, RunHTTPServer(ctx)) assert.NoError(t, RunHTTPServer(ctx, cfg))
}() }()
defer func() { defer func() {
@ -107,18 +129,18 @@ func TestRunTLSServer(t *testing.T) {
func TestRunTLSBase64Server(t *testing.T) { func TestRunTLSBase64Server(t *testing.T) {
cert := `LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMrekNDQWVPZ0F3SUJBZ0lKQUxiWkVEdlVRckZLTUEwR0NTcUdTSWIzRFFFQkJRVUFNQlF4RWpBUUJnTlYKQkFNTUNXeHZZMkZzYUc5emREQWVGdzB4TmpBek1qZ3dNek13TkRGYUZ3MHlOakF6TWpZd016TXdOREZhTUJReApFakFRQmdOVkJBTU1DV3h2WTJGc2FHOXpkRENDQVNJd0RRWUpLb1pJaHZjTkFRRUJCUUFEZ2dFUEFEQ0NBUW9DCmdnRUJBTWoxK3hnNGpWTHpWbkI1ajduMXVsMzBXRUU0QkN6Y05GeGc1QU9CNUg1cSt3amUwWVlpVkZnNlBReXYKR0NpcHFJUlhWUmRWUTFoSFNldW5ZR0tlOGxxM1NiMVg4UFVKMTJ2OXVSYnBTOURLMU93cWs4cnNQRHU2c1ZUTApxS0tnSDFaOHlhenphUzBBYlh1QTVlOWdPL1J6aWpibnBFUCtxdU00ZHVlaU1QVkVKeUxxK0VvSVFZK01NOE1QCjhkWnpMNFhabDd3TDRVc0NON3JQY082VzN0bG5UMGlPM2g5Yy9ZbTJoRmh6K0tOSjlLUlJDdnRQR1pFU2lndEsKYkhzWEgwOTlXRG84di9XcDUvZXZCdy8rSkQwb3B4bUNmSElCQUxIdDl2NTNSdnZzRFoxdDMzUnB1NUM4em5FWQpZMkF5N05neGhxanFvV0pxQTQ4bEplQTBjbHNDQXdFQUFhTlFNRTR3SFFZRFZSME9CQllFRkMwYlRVMVhvZ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dBWjcvNzM1ZGtoSmN5T2ZzK0xLc0xyNjhKU3N0b29yWE9ZdmRNdTErSkdhOWlMdWhuSEVjTVZXQzgKSXVpaHpQZmxvWnRNYkdZa1pKbjhsM0JlR2Q4aG1mRnRnVGdaR1BvVlJldGZ0MkxERkxuUHhwMnNFSDVPRkxzUQpSK0sva0FPdWw4ZVN0V3VNWE9GQTlwTXpHa0dFZ0lGSk1KT3lhSk9OM2tlZFFJOGRlQ009Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==`
initTest() cfg := initTest()
PushConf.Core.SSL = true cfg.Core.SSL = true
PushConf.Core.Port = "8089" cfg.Core.Port = "8089"
PushConf.Core.CertPath = "" cfg.Core.CertPath = ""
PushConf.Core.KeyPath = "" cfg.Core.KeyPath = ""
PushConf.Core.CertBase64 = cert cfg.Core.CertBase64 = cert
PushConf.Core.KeyBase64 = key cfg.Core.KeyBase64 = key
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
go func() { go func() {
assert.NoError(t, RunHTTPServer(ctx)) assert.NoError(t, RunHTTPServer(ctx, cfg))
}() }()
defer func() { defer func() {
@ -133,11 +155,11 @@ func TestRunTLSBase64Server(t *testing.T) {
} }
func TestRunAutoTLSServer(t *testing.T) { func TestRunAutoTLSServer(t *testing.T) {
initTest() cfg := initTest()
PushConf.Core.AutoTLS.Enabled = true cfg.Core.AutoTLS.Enabled = true
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
go func() { go func() {
assert.NoError(t, RunHTTPServer(ctx)) assert.NoError(t, RunHTTPServer(ctx, cfg))
}() }()
defer func() { defer func() {
@ -150,41 +172,41 @@ func TestRunAutoTLSServer(t *testing.T) {
} }
func TestLoadTLSCertError(t *testing.T) { func TestLoadTLSCertError(t *testing.T) {
initTest() cfg := initTest()
PushConf.Core.SSL = true cfg.Core.SSL = true
PushConf.Core.Port = "8087" cfg.Core.Port = "8087"
PushConf.Core.CertPath = "../config/config.yml" cfg.Core.CertPath = "../config/config.yml"
PushConf.Core.KeyPath = "../config/config.yml" cfg.Core.KeyPath = "../config/config.yml"
assert.Error(t, RunHTTPServer(context.Background())) assert.Error(t, RunHTTPServer(context.Background(), cfg))
} }
func TestMissingTLSCertConfg(t *testing.T) { func TestMissingTLSCertcfgg(t *testing.T) {
initTest() cfg := initTest()
PushConf.Core.SSL = true cfg.Core.SSL = true
PushConf.Core.Port = "8087" cfg.Core.Port = "8087"
PushConf.Core.CertPath = "" cfg.Core.CertPath = ""
PushConf.Core.KeyPath = "" cfg.Core.KeyPath = ""
PushConf.Core.CertBase64 = "" cfg.Core.CertBase64 = ""
PushConf.Core.KeyBase64 = "" cfg.Core.KeyBase64 = ""
err := RunHTTPServer(context.Background()) err := RunHTTPServer(context.Background(), cfg)
assert.Error(t, RunHTTPServer(context.Background())) assert.Error(t, RunHTTPServer(context.Background(), cfg))
assert.Equal(t, "missing https cert config", err.Error()) assert.Equal(t, "missing https cert config", err.Error())
} }
func TestRootHandler(t *testing.T) { func TestRootHandler(t *testing.T) {
initTest() cfg := initTest()
r := gofight.New() r := gofight.New()
// log for json // log for json
PushConf.Log.Format = "json" cfg.Log.Format = "json"
r.GET("/"). r.GET("/").
Run(routerEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { Run(routerEngine(cfg), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
data := r.Body.Bytes() data := r.Body.Bytes()
value, _ := jsonparser.GetString(data, "text") value, _ := jsonparser.GetString(data, "text")
@ -196,12 +218,12 @@ func TestRootHandler(t *testing.T) {
} }
func TestAPIStatusGoHandler(t *testing.T) { func TestAPIStatusGoHandler(t *testing.T) {
initTest() cfg := initTest()
r := gofight.New() r := gofight.New()
r.GET("/api/stat/go"). r.GET("/api/stat/go").
Run(routerEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { Run(routerEngine(cfg), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
data := r.Body.Bytes() data := r.Body.Bytes()
value, _ := jsonparser.GetString(data, "go_version") value, _ := jsonparser.GetString(data, "go_version")
@ -212,7 +234,7 @@ func TestAPIStatusGoHandler(t *testing.T) {
} }
func TestAPIStatusAppHandler(t *testing.T) { func TestAPIStatusAppHandler(t *testing.T) {
initTest() cfg := initTest()
r := gofight.New() r := gofight.New()
@ -220,7 +242,7 @@ func TestAPIStatusAppHandler(t *testing.T) {
SetVersion(appVersion) SetVersion(appVersion)
r.GET("/api/stat/app"). r.GET("/api/stat/app").
Run(routerEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { Run(routerEngine(cfg), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
data := r.Body.Bytes() data := r.Body.Bytes()
value, _ := jsonparser.GetString(data, "version") value, _ := jsonparser.GetString(data, "version")
@ -231,46 +253,46 @@ func TestAPIStatusAppHandler(t *testing.T) {
} }
func TestAPIConfigHandler(t *testing.T) { func TestAPIConfigHandler(t *testing.T) {
initTest() cfg := initTest()
r := gofight.New() r := gofight.New()
r.GET("/api/config"). r.GET("/api/config").
Run(routerEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { Run(routerEngine(cfg), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusCreated, r.Code) assert.Equal(t, http.StatusCreated, r.Code)
}) })
} }
func TestMissingNotificationsParameter(t *testing.T) { func TestMissingNotificationsParameter(t *testing.T) {
initTest() cfg := initTest()
r := gofight.New() r := gofight.New()
// missing notifications parameter. // missing notifications parameter.
r.POST("/api/push"). r.POST("/api/push").
Run(routerEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { Run(routerEngine(cfg), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusBadRequest, r.Code) assert.Equal(t, http.StatusBadRequest, r.Code)
assert.Equal(t, "application/json; charset=utf-8", r.HeaderMap.Get("Content-Type")) assert.Equal(t, "application/json; charset=utf-8", r.HeaderMap.Get("Content-Type"))
}) })
} }
func TestEmptyNotifications(t *testing.T) { func TestEmptyNotifications(t *testing.T) {
initTest() cfg := initTest()
r := gofight.New() r := gofight.New()
// notifications is empty. // notifications is empty.
r.POST("/api/push"). r.POST("/api/push").
SetJSON(gofight.D{ SetJSON(gofight.D{
"notifications": []PushNotification{}, "notifications": []gorush.PushNotification{},
}). }).
Run(routerEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { Run(routerEngine(cfg), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusBadRequest, r.Code) assert.Equal(t, http.StatusBadRequest, r.Code)
}) })
} }
func TestMutableContent(t *testing.T) { func TestMutableContent(t *testing.T) {
initTest() cfg := initTest()
r := gofight.New() r := gofight.New()
@ -292,16 +314,16 @@ func TestMutableContent(t *testing.T) {
}, },
}, },
}). }).
Run(routerEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { Run(routerEngine(cfg), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
// json: cannot unmarshal number into Go struct field PushNotification.mutable_content of type bool // json: cannot unmarshal number into Go struct field PushNotification.mutable_content of type bool
assert.Equal(t, http.StatusBadRequest, r.Code) assert.Equal(t, http.StatusBadRequest, r.Code)
}) })
} }
func TestOutOfRangeMaxNotifications(t *testing.T) { func TestOutOfRangeMaxNotifications(t *testing.T) {
initTest() cfg := initTest()
PushConf.Core.MaxNotification = int64(1) cfg.Core.MaxNotification = int64(1)
r := gofight.New() r := gofight.New()
@ -321,17 +343,17 @@ func TestOutOfRangeMaxNotifications(t *testing.T) {
}, },
}, },
}). }).
Run(routerEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { Run(routerEngine(cfg), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusBadRequest, r.Code) assert.Equal(t, http.StatusBadRequest, r.Code)
}) })
} }
func TestSuccessPushHandler(t *testing.T) { func TestSuccessPushHandler(t *testing.T) {
t.Skip() t.Skip()
initTest() cfg := initTest()
PushConf.Android.Enabled = true cfg.Android.Enabled = true
PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY") cfg.Android.APIKey = os.Getenv("ANDROID_API_KEY")
androidToken := os.Getenv("ANDROID_TEST_TOKEN") androidToken := os.Getenv("ANDROID_TEST_TOKEN")
@ -347,63 +369,63 @@ func TestSuccessPushHandler(t *testing.T) {
}, },
}, },
}). }).
Run(routerEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { Run(routerEngine(cfg), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code) assert.Equal(t, http.StatusOK, r.Code)
}) })
} }
func TestSysStatsHandler(t *testing.T) { func TestSysStatsHandler(t *testing.T) {
initTest() cfg := initTest()
r := gofight.New() r := gofight.New()
r.GET("/sys/stats"). r.GET("/sys/stats").
Run(routerEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { Run(routerEngine(cfg), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code) assert.Equal(t, http.StatusOK, r.Code)
}) })
} }
func TestMetricsHandler(t *testing.T) { func TestMetricsHandler(t *testing.T) {
initTest() cfg := initTest()
r := gofight.New() r := gofight.New()
r.GET("/metrics"). r.GET("/metrics").
Run(routerEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { Run(routerEngine(cfg), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code) assert.Equal(t, http.StatusOK, r.Code)
}) })
} }
func TestGETHeartbeatHandler(t *testing.T) { func TestGETHeartbeatHandler(t *testing.T) {
initTest() cfg := initTest()
r := gofight.New() r := gofight.New()
r.GET("/healthz"). r.GET("/healthz").
Run(routerEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { Run(routerEngine(cfg), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code) assert.Equal(t, http.StatusOK, r.Code)
}) })
} }
func TestHEADHeartbeatHandler(t *testing.T) { func TestHEADHeartbeatHandler(t *testing.T) {
initTest() cfg := initTest()
r := gofight.New() r := gofight.New()
r.HEAD("/healthz"). r.HEAD("/healthz").
Run(routerEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { Run(routerEngine(cfg), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code) assert.Equal(t, http.StatusOK, r.Code)
}) })
} }
func TestVersionHandler(t *testing.T) { func TestVersionHandler(t *testing.T) {
SetVersion("3.0.0") SetVersion("3.0.0")
initTest() cfg := initTest()
r := gofight.New() r := gofight.New()
r.GET("/version"). r.GET("/version").
Run(routerEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { Run(routerEngine(cfg), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code) assert.Equal(t, http.StatusOK, r.Code)
data := r.Body.Bytes() data := r.Body.Bytes()
@ -414,10 +436,10 @@ func TestVersionHandler(t *testing.T) {
} }
func TestDisabledHTTPServer(t *testing.T) { func TestDisabledHTTPServer(t *testing.T) {
initTest() cfg := initTest()
PushConf.Core.Enabled = false cfg.Core.Enabled = false
err := RunHTTPServer(context.Background()) err := RunHTTPServer(context.Background(), cfg)
PushConf.Core.Enabled = true cfg.Core.Enabled = true
assert.Nil(t, err) assert.Nil(t, err)
} }

View File

@ -1,4 +1,4 @@
package gorush package router
import ( import (
"fmt" "fmt"