diff --git a/TDWorker.go b/TDWorker.go index 64157b6..e8e4a29 100644 --- a/TDWorker.go +++ b/TDWorker.go @@ -29,7 +29,7 @@ func main() { // startServer func starts an HTTP server that exposes RESTful APIs func startServer() error { logger.AppLogger(labels.Info, labels.ServerStarting, 0) - err := commands.StartServer() + err := commands.NewServer().Start() if err != nil { // Print the error occured while starting the server. fmt.Printf("%+v\n", err.Error()) diff --git a/logger/logger.go b/logger/logger.go index d3d2ba0..b1a5134 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -19,9 +19,6 @@ var ( // ZLevel is global logger ZLevel *zap.AtomicLevel - // timeFormat is custom Time format - customTimeFormat string - // onceInit guarantee initialize logger only once onceInit sync.Once @@ -86,12 +83,9 @@ func Init(lvl string, timeFormat string, msName string) error { consoleErrors := zapcore.Lock(os.Stderr) // Configure console output. - var useCustomTimeFormat bool ecfg := zap.NewProductionEncoderConfig() if len(timeFormat) > 0 { - customTimeFormat = timeFormat ecfg.EncodeTime = customTimeEncoder - useCustomTimeFormat = true } consoleEncoder := zapcore.NewJSONEncoder(ecfg) @@ -105,11 +99,6 @@ func Init(lvl string, timeFormat string, msName string) error { // From a zapcore.Core, it's easy to construct a Logger. Log = zap.New(core) zap.RedirectStdLog(Log) - - // If timeformat is not provided, log a warning on console to use a zap default or custom one - if !useCustomTimeFormat { - Log.Warn("time format for logger is not provided - use zap default") - } }) return err @@ -141,8 +130,6 @@ func AppLogger(level string, description string, time int64, items ...string) { Log.Debug(description, fields...) case "error": Log.Error(description, fields...) - case "fatal": - Log.Fatal(description, fields...) case "warn": Log.Warn(description, fields...) } diff --git a/logger/logger_test.go b/logger/logger_test.go index 32d169e..2591af6 100644 --- a/logger/logger_test.go +++ b/logger/logger_test.go @@ -187,6 +187,24 @@ func TestAppLogger(t *testing.T) { items: []string{"description | error"}, }, }, + { + name: "givenWarnArguments_returnNothing_success", + args: args{ + level: "warn", + description: "description", + time: 1593332975979, + items: []string{"description | warn"}, + }, + }, + { + name: "givenPipeArguments_returnNothing_Error", + args: args{ + level: "debug", + description: "description", + time: 1593332975979, + items: []string{"description fatal"}, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/services/commands.go b/services/commands.go index d8b9083..2df7c72 100644 --- a/services/commands.go +++ b/services/commands.go @@ -1,18 +1,15 @@ package commands import ( - "context" "net/http" - "os" - "os/signal" "time" "github.com/gorilla/mux" ) -var ( - server *http.Server -) +type Server struct { + *http.Server +} // getPartners func for getting TriDubai annually partners func getPartners(w http.ResponseWriter, r *http.Request) { @@ -44,8 +41,8 @@ func serveImage(w http.ResponseWriter, r *http.Request) { _, _ = getImage(w, r) } -// RunServer func for running HTTP server -func StartServer() error { +// NewServer func for HTTP server +func NewServer() Server { r := mux.NewRouter() api := r.PathPrefix("/api/v1").Subrouter() @@ -58,33 +55,18 @@ func StartServer() error { loadFileToMemory() - server = &http.Server{ - Addr: ":8080", - ReadHeaderTimeout: 5 * time.Second, - Handler: r, + return Server{ + &http.Server{ + Addr: ":8080", + ReadHeaderTimeout: 5 * time.Second, + Handler: r, + }, } - - ctx := context.Background() - StopServer(ctx, server) - - return server.ListenAndServe() } -// Graceful shutdown for HTTP server if Interrupt signal captured from OS -func StopServer(ctx context.Context, srv *http.Server) { - - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt) - go func() { - select { - case <-c: - break - case <-ctx.Done(): - break - } - - _, cancel := context.WithTimeout(ctx, 5*time.Second) - defer cancel() - _ = srv.Shutdown(ctx) - }() +func (s Server) Start() error { + if err := s.ListenAndServe(); err != http.ErrServerClosed { + return err + } + return nil } diff --git a/services/commands_test.go b/services/commands_test.go index f087358..ee25315 100644 --- a/services/commands_test.go +++ b/services/commands_test.go @@ -229,42 +229,14 @@ func Test_serveImage(t *testing.T) { } } -func TestStopServer(t *testing.T) { - type args struct { - ctx context.Context - srv *http.Server - } - tests := []struct { - name string - args args - }{ - { - name: "StopServer_Success", - args: args{ - ctx: context.Background(), - srv: &http.Server{}, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - StopServer(tt.args.ctx, tt.args.srv) - }) - } -} - func TestStartServer(t *testing.T) { - tests := []struct { - name string - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := StartServer(); (err != nil) != tt.wantErr { - t.Errorf("StartServer() error = %v, wantErr %v", err, tt.wantErr) - } - }) + srv := NewServer() + go func() { + time.Sleep(1 * time.Second) + srv.Shutdown(context.Background()) + }() + err := srv.Start() + if err != nil { + t.Error("unexpected error:", err) } } diff --git a/services/common_test.go b/services/common_test.go index 8b212ae..8742b73 100644 --- a/services/common_test.go +++ b/services/common_test.go @@ -111,6 +111,11 @@ func Test_loadFileToMemory(t *testing.T) { loadFileToMemory() }) } + + fNames = []string{"Sessions2"} + + loadFileToMemory() + } func jsonMarshaller(str string) string {