perf: caching certain routes

This commit is contained in:
Reinaldy Rafli 2021-08-04 16:03:53 +07:00
parent c0f817722d
commit e3e178087c
8 changed files with 24 additions and 10 deletions

View File

@ -49,7 +49,7 @@ func setup() error {
defer a.Close()
defer j.Close()
return nil
}

View File

@ -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)

View File

@ -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")
}
}

View File

@ -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
}

View File

@ -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)

View File

@ -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)

View File

@ -45,4 +45,4 @@ func TestIsToday_ErrorIfInvalid(t *testing.T) {
if today != false {
t.Error("it should be false:", today)
}
}
}

View File

@ -23,4 +23,4 @@ func TestRandomString_Invalid(t *testing.T) {
if len(random) != 20 {
t.Error("result is not within the length of 10")
}
}
}