diff --git a/gorush/notification_apns_test.go b/gorush/notification_apns_test.go index eee596b..eca561c 100644 --- a/gorush/notification_apns_test.go +++ b/gorush/notification_apns_test.go @@ -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, 0, len(logs)) } diff --git a/gorush/notification_test.go b/gorush/notification_test.go index 02541c8..d00ae27 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -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, 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, 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, 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, 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, len(logs)) } diff --git a/gorush/server_lambda.go b/gorush/server_lambda.go deleted file mode 100644 index b4bdbc2..0000000 --- a/gorush/server_lambda.go +++ /dev/null @@ -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()) -} diff --git a/gorush/worker.go b/gorush/worker.go index fd23e19..6ca0cdb 100644 --- a/gorush/worker.go +++ b/gorush/worker.go @@ -61,8 +61,8 @@ func markFailedNotification(notification *PushNotification, reason string) { notification.WaitDone() } -// queueNotification add notification to queue list. -func queueNotification(ctx context.Context, req RequestPush) (int, []logx.LogPushEntry) { +// HandleNotification add notification to queue list. +func HandleNotification(ctx context.Context, req RequestPush) (int, []logx.LogPushEntry) { var count int wg := sync.WaitGroup{} newNotification := []*PushNotification{} diff --git a/main.go b/main.go index afc0332..79ca450 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ import ( "github.com/appleboy/gorush/core" "github.com/appleboy/gorush/gorush" "github.com/appleboy/gorush/logx" + "github.com/appleboy/gorush/router" "github.com/appleboy/gorush/rpc" "github.com/appleboy/gorush/status" @@ -96,11 +97,11 @@ func main() { flag.Usage = usage flag.Parse() - gorush.SetVersion(Version) + router.SetVersion(Version) // Show version and exit if showVersion { - gorush.PrintGoRushVersion() + router.PrintGoRushVersion() os.Exit(0) } @@ -353,7 +354,7 @@ func main() { // Run httpd server g.Go(func() error { - return gorush.RunHTTPServer(ctx) + return router.RunHTTPServer(ctx, gorush.PushConf) }) // Run gRPC internal server diff --git a/gorush/server.go b/router/server.go similarity index 53% rename from gorush/server.go rename to router/server.go index ea784fc..a77e4d3 100644 --- a/gorush/server.go +++ b/router/server.go @@ -1,4 +1,4 @@ -package gorush +package router import ( "context" @@ -7,11 +7,13 @@ import ( "net/http" "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/status" api "github.com/appleboy/gin-status-api" - "github.com/appleboy/gorush/logx" "github.com/gin-contrib/logger" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" @@ -29,7 +31,7 @@ var isTerm bool func init() { // Support metrics m := metric.NewMetrics(func() int { - return len(QueueNotification) + return len(gorush.QueueNotification) }) prometheus.MustRegister(m) isTerm = isatty.IsTerminal(os.Stdout.Fd()) @@ -59,56 +61,60 @@ func versionHandler(c *gin.Context) { }) } -func pushHandler(c *gin.Context) { - var form RequestPush - var msg string +func pushHandler(cfg config.ConfYaml) gin.HandlerFunc { + return func(c *gin.Context) { + var form gorush.RequestPush + 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 - } - - if len(form.Notifications) == 0 { - msg = "Notifications field is empty." - logx.LogAccess.Debug(msg) - abortWithError(c, http.StatusBadRequest, msg) - return - } - - if int64(len(form.Notifications)) > PushConf.Core.MaxNotification { - msg = fmt.Sprintf("Number of notifications(%d) over limit(%d)", len(form.Notifications), PushConf.Core.MaxNotification) - logx.LogAccess.Debug(msg) - abortWithError(c, http.StatusBadRequest, msg) - return - } - - 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 PushConf.Core.Sync { - cancel() + if err := c.ShouldBindWith(&form, binding.JSON); err != nil { + msg = "Missing notifications field." + logx.LogAccess.Debug(err) + abortWithError(c, http.StatusBadRequest, msg) + return } - }() - counts, logs := queueNotification(ctx, form) + if len(form.Notifications) == 0 { + msg = "Notifications field is empty." + logx.LogAccess.Debug(msg) + abortWithError(c, http.StatusBadRequest, msg) + return + } - c.JSON(http.StatusOK, gin.H{ - "success": "ok", - "counts": counts, - "logs": logs, - }) + 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 + } + + 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() + } + }() + + counts, logs := gorush.HandleNotification(ctx, form) + + c.JSON(http.StatusOK, gin.H{ + "success": "ok", + "counts": counts, + "logs": logs, + }) + } } -func configHandler(c *gin.Context) { - c.YAML(http.StatusCreated, PushConf) +func configHandler(cfg config.ConfYaml) gin.HandlerFunc { + return func(c *gin.Context) { + c.YAML(http.StatusCreated, cfg) + } } func metricsHandler(c *gin.Context) { @@ -119,8 +125,8 @@ func appStatusHandler(c *gin.Context) { result := status.App{} result.Version = GetVersion() - result.QueueMax = cap(QueueNotification) - result.QueueUsage = len(QueueNotification) + result.QueueMax = cap(gorush.QueueNotification) + result.QueueUsage = len(gorush.QueueNotification) result.TotalCount = status.StatStorage.GetTotalCount() result.Ios.PushSuccess = status.StatStorage.GetIosSuccess() result.Ios.PushError = status.StatStorage.GetIosError() @@ -132,8 +138,10 @@ func appStatusHandler(c *gin.Context) { c.JSON(http.StatusOK, result) } -func sysStatsHandler(c *gin.Context) { - c.JSON(http.StatusOK, status.Stats.Data()) +func sysStatsHandler() gin.HandlerFunc { + return func(c *gin.Context) { + c.JSON(http.StatusOK, status.Stats.Data()) + } } // StatMiddleware response time, status code count, etc. @@ -145,23 +153,23 @@ func StatMiddleware() gin.HandlerFunc { } } -func autoTLSServer() *http.Server { +func autoTLSServer(cfg config.ConfYaml) *http.Server { m := autocert.Manager{ Prompt: autocert.AcceptTOS, - HostPolicy: autocert.HostWhitelist(PushConf.Core.AutoTLS.Host), - Cache: autocert.DirCache(PushConf.Core.AutoTLS.Folder), + HostPolicy: autocert.HostWhitelist(cfg.Core.AutoTLS.Host), + Cache: autocert.DirCache(cfg.Core.AutoTLS.Folder), } return &http.Server{ Addr: ":https", 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) - if PushConf.Core.Mode == "debug" { + if cfg.Core.Mode == "debug" { zerolog.SetGlobalLevel(zerolog.DebugLevel) } @@ -177,7 +185,7 @@ func routerEngine() *gin.Engine { } // set server mode - gin.SetMode(PushConf.Core.Mode) + gin.SetMode(cfg.Core.Mode) r := gin.New() @@ -185,22 +193,22 @@ func routerEngine() *gin.Engine { r.Use(logger.SetLogger( logger.WithUTC(true), logger.WithSkipPath([]string{ - PushConf.API.HealthURI, - PushConf.API.MetricURI, + cfg.API.HealthURI, + cfg.API.MetricURI, }), )) r.Use(gin.Recovery()) r.Use(VersionMiddleware()) r.Use(StatMiddleware()) - r.GET(PushConf.API.StatGoURI, api.GinHandler) - r.GET(PushConf.API.StatAppURI, appStatusHandler) - r.GET(PushConf.API.ConfigURI, configHandler) - r.GET(PushConf.API.SysStatURI, sysStatsHandler) - r.POST(PushConf.API.PushURI, pushHandler) - r.GET(PushConf.API.MetricURI, metricsHandler) - r.GET(PushConf.API.HealthURI, heartbeatHandler) - r.HEAD(PushConf.API.HealthURI, heartbeatHandler) + r.GET(cfg.API.StatGoURI, api.GinHandler) + r.GET(cfg.API.StatAppURI, appStatusHandler) + r.GET(cfg.API.ConfigURI, configHandler(cfg)) + r.GET(cfg.API.SysStatURI, sysStatsHandler()) + r.POST(cfg.API.PushURI, pushHandler(cfg)) + r.GET(cfg.API.MetricURI, metricsHandler) + r.GET(cfg.API.HealthURI, heartbeatHandler) + r.HEAD(cfg.API.HealthURI, heartbeatHandler) r.GET("/version", versionHandler) r.GET("/", rootHandler) diff --git a/router/server_lambda.go b/router/server_lambda.go new file mode 100644 index 0000000..dfd2f46 --- /dev/null +++ b/router/server_lambda.go @@ -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()) +} diff --git a/gorush/server_normal.go b/router/server_normal.go similarity index 59% rename from gorush/server_normal.go rename to router/server_normal.go index c20ca2d..00445ba 100644 --- a/gorush/server_normal.go +++ b/router/server_normal.go @@ -1,6 +1,6 @@ // +build !lambda -package gorush +package router import ( "context" @@ -10,33 +10,34 @@ import ( "net/http" "time" + "github.com/appleboy/gorush/config" "github.com/appleboy/gorush/logx" "golang.org/x/sync/errgroup" ) // 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 - if !PushConf.Core.Enabled { + if !cfg.Core.Enabled { logx.LogAccess.Info("httpd server is disabled.") return nil } if len(s) == 0 { server = &http.Server{ - Addr: PushConf.Core.Address + ":" + PushConf.Core.Port, - Handler: routerEngine(), + Addr: cfg.Core.Address + ":" + cfg.Core.Port, + Handler: routerEngine(cfg), } } else { server = s[0] } - logx.LogAccess.Info("HTTPD server is running on " + PushConf.Core.Port + " port.") - if PushConf.Core.AutoTLS.Enabled { - return startServer(ctx, autoTLSServer()) - } else if PushConf.Core.SSL { + logx.LogAccess.Info("HTTPD server is running on " + cfg.Core.Port + " port.") + if cfg.Core.AutoTLS.Enabled { + return startServer(ctx, autoTLSServer(cfg), cfg) + } else if cfg.Core.SSL { config := &tls.Config{ MinVersion: tls.VersionTLS10, } @@ -46,19 +47,19 @@ func RunHTTPServer(ctx context.Context, s ...*http.Server) (err error) { } config.Certificates = make([]tls.Certificate, 1) - if PushConf.Core.CertPath != "" && PushConf.Core.KeyPath != "" { - config.Certificates[0], err = tls.LoadX509KeyPair(PushConf.Core.CertPath, PushConf.Core.KeyPath) + if cfg.Core.CertPath != "" && cfg.Core.KeyPath != "" { + config.Certificates[0], err = tls.LoadX509KeyPair(cfg.Core.CertPath, cfg.Core.KeyPath) if err != nil { logx.LogError.Error("Failed to load https cert file: ", err) return err } - } else if PushConf.Core.CertBase64 != "" && PushConf.Core.KeyBase64 != "" { - cert, err := base64.StdEncoding.DecodeString(PushConf.Core.CertBase64) + } else if cfg.Core.CertBase64 != "" && cfg.Core.KeyBase64 != "" { + cert, err := base64.StdEncoding.DecodeString(cfg.Core.CertBase64) if err != nil { logx.LogError.Error("base64 decode error:", err.Error()) return err } - key, err := base64.StdEncoding.DecodeString(PushConf.Core.KeyBase64) + key, err := base64.StdEncoding.DecodeString(cfg.Core.KeyBase64) if err != nil { logx.LogError.Error("base64 decode error:", err.Error()) return err @@ -74,15 +75,15 @@ func RunHTTPServer(ctx context.Context, s ...*http.Server) (err error) { 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 g.Go(func() error { select { 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) defer cancel() return s.Shutdown(ctx) @@ -97,12 +98,12 @@ func listenAndServe(ctx context.Context, s *http.Server) error { 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 g.Go(func() error { select { 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) defer cancel() return s.Shutdown(ctx) @@ -117,10 +118,10 @@ func listenAndServeTLS(ctx context.Context, s *http.Server) error { 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 { - return listenAndServe(ctx, s) + return listenAndServe(ctx, s, cfg) } - return listenAndServeTLS(ctx, s) + return listenAndServeTLS(ctx, s, cfg) } diff --git a/gorush/server_test.go b/router/server_test.go similarity index 75% rename from gorush/server_test.go rename to router/server_test.go index c91e2bf..663e4a2 100644 --- a/gorush/server_test.go +++ b/router/server_test.go @@ -1,4 +1,4 @@ -package gorush +package router import ( "context" @@ -13,6 +13,9 @@ import ( "github.com/appleboy/gorush/config" "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/buger/jsonparser" @@ -22,9 +25,28 @@ import ( var goVersion = runtime.Version() -func initTest() { - PushConf, _ = config.LoadConf("") - PushConf.Core.Mode = "test" +func TestMain(m *testing.M) { + cfg := initTest() + 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 @@ -60,13 +82,13 @@ func TestPrintGoRushVersion(t *testing.T) { } func TestRunNormalServer(t *testing.T) { - initTest() + cfg := initTest() gin.SetMode(gin.TestMode) ctx, cancel := context.WithCancel(context.Background()) go func() { - assert.NoError(t, RunHTTPServer(ctx)) + assert.NoError(t, RunHTTPServer(ctx, cfg)) }() defer func() { @@ -81,16 +103,16 @@ func TestRunNormalServer(t *testing.T) { } func TestRunTLSServer(t *testing.T) { - initTest() + cfg := initTest() - PushConf.Core.SSL = true - PushConf.Core.Port = "8087" - PushConf.Core.CertPath = "../certificate/localhost.cert" - PushConf.Core.KeyPath = "../certificate/localhost.key" + cfg.Core.SSL = true + cfg.Core.Port = "8087" + cfg.Core.CertPath = "../certificate/localhost.cert" + cfg.Core.KeyPath = "../certificate/localhost.key" ctx, cancel := context.WithCancel(context.Background()) go func() { - assert.NoError(t, RunHTTPServer(ctx)) + assert.NoError(t, RunHTTPServer(ctx, cfg)) }() defer func() { @@ -107,18 +129,18 @@ func TestRunTLSServer(t *testing.T) { func TestRunTLSBase64Server(t *testing.T) { cert := `LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMrekNDQWVPZ0F3SUJBZ0lKQUxiWkVEdlVRckZLTUEwR0NTcUdTSWIzRFFFQkJRVUFNQlF4RWpBUUJnTlYKQkFNTUNXeHZZMkZzYUc5emREQWVGdzB4TmpBek1qZ3dNek13TkRGYUZ3MHlOakF6TWpZd016TXdOREZhTUJReApFakFRQmdOVkJBTU1DV3h2WTJGc2FHOXpkRENDQVNJd0RRWUpLb1pJaHZjTkFRRUJCUUFEZ2dFUEFEQ0NBUW9DCmdnRUJBTWoxK3hnNGpWTHpWbkI1ajduMXVsMzBXRUU0QkN6Y05GeGc1QU9CNUg1cSt3amUwWVlpVkZnNlBReXYKR0NpcHFJUlhWUmRWUTFoSFNldW5ZR0tlOGxxM1NiMVg4UFVKMTJ2OXVSYnBTOURLMU93cWs4cnNQRHU2c1ZUTApxS0tnSDFaOHlhenphUzBBYlh1QTVlOWdPL1J6aWpibnBFUCtxdU00ZHVlaU1QVkVKeUxxK0VvSVFZK01NOE1QCjhkWnpMNFhabDd3TDRVc0NON3JQY082VzN0bG5UMGlPM2g5Yy9ZbTJoRmh6K0tOSjlLUlJDdnRQR1pFU2lndEsKYkhzWEgwOTlXRG84di9XcDUvZXZCdy8rSkQwb3B4bUNmSElCQUxIdDl2NTNSdn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nQ0M3L0tnUmRyem9CcVo1cVM5TXYyZHNMZ1k2MzVWU2dqamZaa1ZMaUgxVlJScFNRT2JZbmZveXNnCmdhdGNIU0tNRXhkNFNMUUJ5QXVJbVhQK0w1YXlEQmNFSmZicVNwc0NnWUI3OElzMWIwdXpOTERqT2g3WTlWaHIKRDJxUHpFT1JjSW9Oc2RaY3RPb1h1WGFBbW1uZ3lJYm01UjlaTjFnV1djNDdvRndMVjNyeFdxWGdzNmZtZzhjWAo3djMwOXZGY0M5UTQvVnhhYTRCNUxOSzluM2dUQUlCUFRPdGxVbmwrMm15MXRmQnRCcVJtMFc2SUtiVEhXUzVnCnZ4akVtL0NpRUl5R1VFZ3FUTWdIQVFLQmdCS3VYZFFvdXRuZzYzUXVmd0l6RHRiS1Z6TUxRNFhpTktobWJYcGgKT2F2Q25wK2dQYkIrTDdZbDhsdEFtVFNPSmdWWjBoY1QwRHhBMzYxWngrMk11NThHQmw0T2JsbmNobXdFMXZqMQpLY1F5UHJFUXhkb1VUeWlzd0dmcXZyczhKOWltdmIrejkvVTZUMUtBQjhXaTNXVmlYelByNE1zaWFhUlhnNjQyCkZJZHhBb0dBWjcvNzM1ZGtoSmN5T2ZzK0xLc0xyNjhKU3N0b29yWE9ZdmRNdTErSkdhOWlMdWhuSEVjTVZXQzgKSXVpaHpQZmxvWnRNYkdZa1pKbjhsM0JlR2Q4aG1mRnRnVGdaR1BvVlJldGZ0MkxERkxuUHhwMnNFSDVPRkxzUQpSK0sva0FPdWw4ZVN0V3VNWE9GQTlwTXpHa0dFZ0lGSk1KT3lhSk9OM2tlZFFJOGRlQ009Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==` - initTest() + cfg := initTest() - PushConf.Core.SSL = true - PushConf.Core.Port = "8089" - PushConf.Core.CertPath = "" - PushConf.Core.KeyPath = "" - PushConf.Core.CertBase64 = cert - PushConf.Core.KeyBase64 = key + cfg.Core.SSL = true + cfg.Core.Port = "8089" + cfg.Core.CertPath = "" + cfg.Core.KeyPath = "" + cfg.Core.CertBase64 = cert + cfg.Core.KeyBase64 = key ctx, cancel := context.WithCancel(context.Background()) go func() { - assert.NoError(t, RunHTTPServer(ctx)) + assert.NoError(t, RunHTTPServer(ctx, cfg)) }() defer func() { @@ -133,11 +155,11 @@ func TestRunTLSBase64Server(t *testing.T) { } func TestRunAutoTLSServer(t *testing.T) { - initTest() - PushConf.Core.AutoTLS.Enabled = true + cfg := initTest() + cfg.Core.AutoTLS.Enabled = true ctx, cancel := context.WithCancel(context.Background()) go func() { - assert.NoError(t, RunHTTPServer(ctx)) + assert.NoError(t, RunHTTPServer(ctx, cfg)) }() defer func() { @@ -150,41 +172,41 @@ func TestRunAutoTLSServer(t *testing.T) { } func TestLoadTLSCertError(t *testing.T) { - initTest() + cfg := initTest() - PushConf.Core.SSL = true - PushConf.Core.Port = "8087" - PushConf.Core.CertPath = "../config/config.yml" - PushConf.Core.KeyPath = "../config/config.yml" + cfg.Core.SSL = true + cfg.Core.Port = "8087" + cfg.Core.CertPath = "../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) { - initTest() +func TestMissingTLSCertcfgg(t *testing.T) { + cfg := initTest() - PushConf.Core.SSL = true - PushConf.Core.Port = "8087" - PushConf.Core.CertPath = "" - PushConf.Core.KeyPath = "" - PushConf.Core.CertBase64 = "" - PushConf.Core.KeyBase64 = "" + cfg.Core.SSL = true + cfg.Core.Port = "8087" + cfg.Core.CertPath = "" + cfg.Core.KeyPath = "" + cfg.Core.CertBase64 = "" + cfg.Core.KeyBase64 = "" - err := RunHTTPServer(context.Background()) - assert.Error(t, RunHTTPServer(context.Background())) + err := RunHTTPServer(context.Background(), cfg) + assert.Error(t, RunHTTPServer(context.Background(), cfg)) assert.Equal(t, "missing https cert config", err.Error()) } func TestRootHandler(t *testing.T) { - initTest() + cfg := initTest() r := gofight.New() // log for json - PushConf.Log.Format = "json" + cfg.Log.Format = "json" 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() value, _ := jsonparser.GetString(data, "text") @@ -196,12 +218,12 @@ func TestRootHandler(t *testing.T) { } func TestAPIStatusGoHandler(t *testing.T) { - initTest() + cfg := initTest() r := gofight.New() 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() value, _ := jsonparser.GetString(data, "go_version") @@ -212,7 +234,7 @@ func TestAPIStatusGoHandler(t *testing.T) { } func TestAPIStatusAppHandler(t *testing.T) { - initTest() + cfg := initTest() r := gofight.New() @@ -220,7 +242,7 @@ func TestAPIStatusAppHandler(t *testing.T) { SetVersion(appVersion) 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() value, _ := jsonparser.GetString(data, "version") @@ -231,46 +253,46 @@ func TestAPIStatusAppHandler(t *testing.T) { } func TestAPIConfigHandler(t *testing.T) { - initTest() + cfg := initTest() r := gofight.New() 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) }) } func TestMissingNotificationsParameter(t *testing.T) { - initTest() + cfg := initTest() r := gofight.New() // missing notifications parameter. 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, "application/json; charset=utf-8", r.HeaderMap.Get("Content-Type")) }) } func TestEmptyNotifications(t *testing.T) { - initTest() + cfg := initTest() r := gofight.New() // notifications is empty. r.POST("/api/push"). 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) }) } func TestMutableContent(t *testing.T) { - initTest() + cfg := initTest() 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 assert.Equal(t, http.StatusBadRequest, r.Code) }) } func TestOutOfRangeMaxNotifications(t *testing.T) { - initTest() + cfg := initTest() - PushConf.Core.MaxNotification = int64(1) + cfg.Core.MaxNotification = int64(1) 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) }) } func TestSuccessPushHandler(t *testing.T) { t.Skip() - initTest() + cfg := initTest() - PushConf.Android.Enabled = true - PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY") + cfg.Android.Enabled = true + cfg.Android.APIKey = os.Getenv("ANDROID_API_KEY") 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) }) } func TestSysStatsHandler(t *testing.T) { - initTest() + cfg := initTest() r := gofight.New() 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) }) } func TestMetricsHandler(t *testing.T) { - initTest() + cfg := initTest() r := gofight.New() 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) }) } func TestGETHeartbeatHandler(t *testing.T) { - initTest() + cfg := initTest() r := gofight.New() 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) }) } func TestHEADHeartbeatHandler(t *testing.T) { - initTest() + cfg := initTest() r := gofight.New() 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) }) } func TestVersionHandler(t *testing.T) { SetVersion("3.0.0") - initTest() + cfg := initTest() r := gofight.New() 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) data := r.Body.Bytes() @@ -414,10 +436,10 @@ func TestVersionHandler(t *testing.T) { } func TestDisabledHTTPServer(t *testing.T) { - initTest() - PushConf.Core.Enabled = false - err := RunHTTPServer(context.Background()) - PushConf.Core.Enabled = true + cfg := initTest() + cfg.Core.Enabled = false + err := RunHTTPServer(context.Background(), cfg) + cfg.Core.Enabled = true assert.Nil(t, err) } diff --git a/gorush/version.go b/router/version.go similarity index 97% rename from gorush/version.go rename to router/version.go index f8cdcdf..6300ec3 100644 --- a/gorush/version.go +++ b/router/version.go @@ -1,4 +1,4 @@ -package gorush +package router import ( "fmt"