diff --git a/TDWorker.go b/TDWorker.go index 4c2f8c1..64157b6 100644 --- a/TDWorker.go +++ b/TDWorker.go @@ -28,11 +28,11 @@ func main() { // startServer func starts an HTTP server that exposes RESTful APIs func startServer() error { - logger.AppLogger(labels.Info,labels.ServerStarting,0) - err := commands.RunServer() + logger.AppLogger(labels.Info, labels.ServerStarting, 0) + err := commands.StartServer() if err != nil { // Print the error occured while starting the server. fmt.Printf("%+v\n", err.Error()) } return err -} \ No newline at end of file +} diff --git a/middleware/request.go b/middleware/request.go index ea8b85e..44fef51 100644 --- a/middleware/request.go +++ b/middleware/request.go @@ -2,12 +2,8 @@ package middleware import ( "context" - "crypto/rand" - "encoding/base64" "fmt" "net/http" - "os" - "strings" "sync/atomic" ) @@ -25,23 +21,6 @@ var ( reqID uint64 ) -// init Initializes constant part of request ID -func init() { - hostname, err := os.Hostname() - if hostname == "" || err != nil { - hostname = "localhost" - } - var buf [12]byte - var b64 string - for len(b64) < 10 { - _, _ = rand.Read(buf[:]) - b64 = base64.StdEncoding.EncodeToString(buf[:]) - b64 = strings.NewReplacer("+", "", "/", "").Replace(b64) - } - - prefix = fmt.Sprintf("%s/%s", hostname, b64[0:10]) -} - // RequestID is a middleware that injects a request ID into the context of each // request. A request ID is a string of the form "host.example.com/random-0001", // where "random" is a base62 random string that uniquely identifies this go diff --git a/services/commands.go b/services/commands.go index 4b3f00f..129a979 100644 --- a/services/commands.go +++ b/services/commands.go @@ -1,12 +1,17 @@ package commands import ( + "context" "net/http" "time" "github.com/gorilla/mux" ) +var ( + server *http.Server +) + // getPartners func for getting TriDubai annually partners func getPartners(w http.ResponseWriter, r *http.Request) { _, _ = getData("Partners", w, r) @@ -38,7 +43,7 @@ func serveImage(w http.ResponseWriter, r *http.Request) { } // RunServer func for running HTTP server -func RunServer() error { +func configureServer() *http.Server { loadFileToMemory() r := mux.NewRouter() @@ -51,10 +56,20 @@ func RunServer() error { api.HandleFunc("/faqs", getFAQs).Methods(http.MethodGet) api.HandleFunc("/img/{name}", serveImage).Methods(http.MethodGet) - server := &http.Server{ + server = &http.Server{ Addr: ":8080", ReadHeaderTimeout: 5 * time.Second, Handler: r, } - return server.ListenAndServe() + return server +} + +func StartServer() error { + + return configureServer().ListenAndServe() +} + +func StopServer() error { + + return server.Shutdown(context.Background()) } diff --git a/services/commands_test.go b/services/commands_test.go index d3e5834..d9cfac2 100644 --- a/services/commands_test.go +++ b/services/commands_test.go @@ -1,16 +1,23 @@ package commands import ( + "TDBackend/logger" + "fmt" "net/http" "net/http/httptest" "testing" + "time" ) func Test_getSessions(t *testing.T) { - data, _ := http.NewRequest("GET", "https://apps.tridubai.org/api/v1/sessions", nil) + logger.Init("debug", time.RFC3339, "TDBackend") + + StartServer() + data, _ := http.NewRequest("GET", "http://localhost:8080/api/v1/sessions", nil) rw := httptest.NewRecorder() + fmt.Println("test") type args struct { w http.ResponseWriter r *http.Request @@ -21,12 +28,12 @@ func Test_getSessions(t *testing.T) { wantPanic bool }{ { - name: "getSessions_failure", + name: "getSessions_Success", args: args{ w: rw, r: data, }, - wantPanic: true, + wantPanic: false, }, } for _, tt := range tests { @@ -40,11 +47,15 @@ func Test_getSessions(t *testing.T) { getSessions(tt.args.w, tt.args.r) }) } + StopServer() } func Test_getDeals(t *testing.T) { - data, _ := http.NewRequest("GET", "https://apps.tridubai.org/api/v1/deals", nil) + logger.Init("debug", time.RFC3339, "TDBackend") + + _ = StartServer() + data, _ := http.NewRequest("GET", "http://localhost:8080/api/v1/deals", nil) rw := httptest.NewRecorder() type args struct { @@ -56,21 +67,13 @@ func Test_getDeals(t *testing.T) { args args wantPanic bool }{ - { - name: "getDeals_failure", - args: args{ - w: rw, - r: data, - }, - wantPanic: true, - }, { name: "getDeals_success", args: args{ w: rw, r: data, }, - wantPanic: true, + wantPanic: false, }, } for _, tt := range tests { @@ -84,11 +87,15 @@ func Test_getDeals(t *testing.T) { getDeals(tt.args.w, tt.args.r) }) } + StopServer() } func Test_getPartners(t *testing.T) { - data, _ := http.NewRequest("GET", "https://apps.tridubai.org/api/v1/partners", nil) + logger.Init("debug", time.RFC3339, "TDBackend") + + _ = StartServer() + data, _ := http.NewRequest("GET", "http://localhost:8080/api/v1/partners", nil) rw := httptest.NewRecorder() type args struct { @@ -101,12 +108,12 @@ func Test_getPartners(t *testing.T) { wantPanic bool }{ { - name: "getPartner_failure", + name: "getPartner_success", args: args{ w: rw, r: data, }, - wantPanic: true, + wantPanic: false, }, } for _, tt := range tests { @@ -120,11 +127,15 @@ func Test_getPartners(t *testing.T) { getPartners(tt.args.w, tt.args.r) }) } + StopServer() } func Test_getKit(t *testing.T) { - data, _ := http.NewRequest("GET", "https://apps.tridubai.org/api/v1/kit", nil) + logger.Init("debug", time.RFC3339, "TDBackend") + + _ = StartServer() + data, _ := http.NewRequest("GET", "http://localhost:8080/api/v1/kit", nil) rw := httptest.NewRecorder() type args struct { @@ -137,12 +148,12 @@ func Test_getKit(t *testing.T) { wantPanic bool }{ { - name: "getKit_failure", + name: "getKit_success", args: args{ w: rw, r: data, }, - wantPanic: true, + wantPanic: false, }, } for _, tt := range tests { @@ -156,11 +167,15 @@ func Test_getKit(t *testing.T) { getKit(tt.args.w, tt.args.r) }) } + StopServer() } func Test_getFAQs(t *testing.T) { - data, _ := http.NewRequest("GET", "https://apps.tridubai.org/api/v1/faqs", nil) + logger.Init("debug", time.RFC3339, "TDBackend") + + _ = StartServer() + data, _ := http.NewRequest("GET", "http://localhost:8080/api/v1/faqs", nil) rw := httptest.NewRecorder() type args struct { @@ -173,12 +188,12 @@ func Test_getFAQs(t *testing.T) { wantPanic bool }{ { - name: "getFAQs_failure", + name: "getFAQs_Success", args: args{ w: rw, r: data, }, - wantPanic: true, + wantPanic: false, }, } for _, tt := range tests { @@ -192,16 +207,19 @@ func Test_getFAQs(t *testing.T) { getFAQs(tt.args.w, tt.args.r) }) } + StopServer() } -func TestRunServer(t *testing.T) { +func TestStartServer(t *testing.T) { + logger.Init("debug", time.RFC3339, "TDBackend") + tests := []struct { name string wantPanic bool }{ { - name: "getSessions_failure", - wantPanic: true, + name: "StartServer_Success", + wantPanic: false, }, } for _, tt := range tests { @@ -212,7 +230,48 @@ func TestRunServer(t *testing.T) { t.Errorf("getSessions(), wantPanic = %v", tt.wantPanic) } }() - RunServer() + _ = StartServer() + }) + } + StopServer() +} + +func Test_serveImage(t *testing.T) { + + logger.Init("debug", time.RFC3339, "TDBackend") + + _ = StartServer() + data, _ := http.NewRequest("GET", "http://localhost:8080/api/v1/img/FFMC.jpg", nil) + rw := httptest.NewRecorder() + + type args struct { + w http.ResponseWriter + r *http.Request + } + tests := []struct { + name string + args args + wantPanic bool + }{ + { + name: "getFFMC_Success", + args: args{ + w: rw, + r: data, + }, + wantPanic: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + defer func() { + r := recover() + if (r != nil) != tt.wantPanic { + t.Errorf("serveImage(), wantPanic = %v", tt.wantPanic) + } + }() + serveImage(tt.args.w, tt.args.r) }) } + StopServer() } diff --git a/services/common.go b/services/common.go index f41cfc5..fb459b0 100644 --- a/services/common.go +++ b/services/common.go @@ -40,11 +40,12 @@ func readFile(endPoint string) (string, error) { t := time.Now() // Get file name - fileName := "static/" + endPoint + ".json" + fileName := "./static/" + endPoint + ".json" // Open our jsonFile jsonFile, err := os.Open(filepath.Clean(fileName)) + fmt.Println("File", jsonFile, err) // if we os.Open returns an error then handle it if err != nil { logger.AppLogger(labels.Error, "Error while opening "+endPoint+" file!", time.Since(t).Nanoseconds(), labels.Error+"|"+err.Error()) diff --git a/services/common_test.go b/services/common_test.go index ad6ac1a..6789cc8 100644 --- a/services/common_test.go +++ b/services/common_test.go @@ -1,16 +1,34 @@ package commands import ( - labels "TDBackend/localization" - applog "TDBackend/logger" + "TDBackend/logger" + "fmt" "net/http" "net/http/httptest" "testing" "time" ) +func Test_loadFileToMemory(t *testing.T) { + logger.Init("debug", time.RFC3339, "TDBackend") + + tests := []struct { + name string + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + loadFileToMemory() + }) + } +} + func Test_getData(t *testing.T) { + logger.Init("debug", time.RFC3339, "TDBackend") + + _ = StartServer() partner, _ := http.NewRequest("GET", "http://rehabaam.net:8080/TriDubai/v1/sessions/getPartners", nil) session, _ := http.NewRequest("POST", "http://rehabaam.net:8080/TriDubai/v1/sessions/getSessions", nil) rw := httptest.NewRecorder() @@ -54,7 +72,6 @@ func Test_getData(t *testing.T) { }, } - _ = applog.Init("debug", time.Now().Format(labels.RFC3339Milli), "TDBackend") for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { defer func() { @@ -67,3 +84,57 @@ func Test_getData(t *testing.T) { }) } } + +func Test_readFile(t *testing.T) { + logger.Init("debug", time.RFC3339, "TDBackend") + + type args struct { + endPoint string + } + tests := []struct { + name string + args args + want string + wantErr bool + }{} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := readFile(tt.args.endPoint) + fmt.Println(got) + if (err != nil) != tt.wantErr { + t.Errorf("readFile() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("readFile() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_getImage(t *testing.T) { + logger.Init("debug", time.RFC3339, "TDBackend") + + type args struct { + w http.ResponseWriter + r *http.Request + } + tests := []struct { + name string + args args + want int + wantErr bool + }{} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := getImage(tt.args.w, tt.args.r) + if (err != nil) != tt.wantErr { + t.Errorf("getImage() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("getImage() = %v, want %v", got, tt.want) + } + }) + } +}