diff --git a/api/app/v1/handler/health/health_test.go b/api/app/v1/handler/health/health_test.go index 9cd990a..cf55b70 100644 --- a/api/app/v1/handler/health/health_test.go +++ b/api/app/v1/handler/health/health_test.go @@ -49,7 +49,7 @@ func setup() error { defer a.Close() defer j.Close() - + return nil } diff --git a/api/app/v1/handler/joke/joke_delete_test.go b/api/app/v1/handler/joke/joke_delete_test.go index ff81e02..2685a22 100644 --- a/api/app/v1/handler/joke/joke_delete_test.go +++ b/api/app/v1/handler/joke/joke_delete_test.go @@ -18,7 +18,7 @@ func TestDeleteJoke_200(t *testing.T) { } defer cleanup() - + reqBody := strings.NewReader("{\"key\":\"very secure\",\"token\":\"password\"}") req, _ := http.NewRequest("DELETE", "/id/1", reqBody) res, err := app.Test(req, -1) diff --git a/api/app/v1/handler/submit/submit_get_test.go b/api/app/v1/handler/submit/submit_get_test.go index 3a9dd18..2d6eb47 100644 --- a/api/app/v1/handler/submit/submit_get_test.go +++ b/api/app/v1/handler/submit/submit_get_test.go @@ -32,7 +32,7 @@ func setup() error { if err != nil { return err } - + s, err := db.Query(context.Background(), "INSERT INTO \"submission\" (id, link, created_at, author, status) VALUES ($1, $2, $3, $4, $5), ($6, $7, $8, $9, $10);", submissionData...) if err != nil { return err @@ -76,4 +76,4 @@ func TestGetSubmission_Params(t *testing.T) { body, err := ioutil.ReadAll(res.Body) assert.Nilf(t, err, "get submission") assert.Equalf(t, "{\"count\":1,\"jokes\":[{\"id\":2,\"link\":\"https://via.placeholder.com/300/02f/fff.png\",\"created_at\":\"2021-08-04T18:20:38Z\",\"author\":\"Test \\u003ctest@example.com\\u003e\",\"status\":1}]}", string(body), "get submission") -} \ No newline at end of file +} diff --git a/api/app/v1/routes/health.go b/api/app/v1/routes/health.go index 969cb88..c1dc52b 100644 --- a/api/app/v1/routes/health.go +++ b/api/app/v1/routes/health.go @@ -2,13 +2,15 @@ package routes import ( "jokes-bapak2-api/app/v1/handler/health" + "time" "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/cache" ) func Health(app *fiber.App) *fiber.App { // Health check - app.Get("/health", health.Health) + app.Get("/health", cache.New(cache.Config{Expiration: 30 * time.Minute}), health.Health) return app } diff --git a/api/app/v1/routes/joke.go b/api/app/v1/routes/joke.go index 54cc5c7..1bddc1d 100644 --- a/api/app/v1/routes/joke.go +++ b/api/app/v1/routes/joke.go @@ -3,8 +3,10 @@ package routes import ( "jokes-bapak2-api/app/v1/handler/joke" "jokes-bapak2-api/app/v1/middleware" + "time" "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/cache" ) func Joke(app *fiber.App) *fiber.App { @@ -12,13 +14,13 @@ func Joke(app *fiber.App) *fiber.App { app.Get("/", joke.SingleJoke) // Today's joke - app.Get("/today", joke.TodayJoke) + app.Get("/today", cache.New(cache.Config{Expiration: 6 * time.Hour}), joke.TodayJoke) // Joke by ID app.Get("/id/:id", middleware.OnlyIntegerAsID(), joke.JokeByID) // Count total jokes - app.Get("/total", joke.TotalJokes) + app.Get("/total", cache.New(cache.Config{Expiration: 15 * time.Minute}), joke.TotalJokes) // Add new joke app.Put("/", middleware.RequireAuth(), joke.AddNewJoke) diff --git a/api/app/v1/routes/submit.go b/api/app/v1/routes/submit.go index 6084980..24b9274 100644 --- a/api/app/v1/routes/submit.go +++ b/api/app/v1/routes/submit.go @@ -2,13 +2,23 @@ package routes import ( "jokes-bapak2-api/app/v1/handler/submit" + "time" "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/cache" ) func Submit(app *fiber.App) *fiber.App { // Get pending submitted joke - app.Get("/submit", submit.GetSubmission) + app.Get( + "/submit", + cache.New(cache.Config{ + Expiration: 5 * time.Minute, + KeyGenerator: func(c *fiber.Ctx) string { + return c.OriginalURL() + }, + }), + submit.GetSubmission) // Add a joke app.Post("/submit", submit.SubmitJoke) diff --git a/api/app/v1/utils/date_test.go b/api/app/v1/utils/date_test.go index 2e328b8..f96f077 100644 --- a/api/app/v1/utils/date_test.go +++ b/api/app/v1/utils/date_test.go @@ -45,4 +45,4 @@ func TestIsToday_ErrorIfInvalid(t *testing.T) { if today != false { t.Error("it should be false:", today) } -} \ No newline at end of file +} diff --git a/api/app/v1/utils/random_test.go b/api/app/v1/utils/random_test.go index d549507..65a2be1 100644 --- a/api/app/v1/utils/random_test.go +++ b/api/app/v1/utils/random_test.go @@ -23,4 +23,4 @@ func TestRandomString_Invalid(t *testing.T) { if len(random) != 20 { t.Error("result is not within the length of 10") } -} \ No newline at end of file +}