perf: caching certain routes
This commit is contained in:
parent
c0f817722d
commit
e3e178087c
|
@ -49,7 +49,7 @@ func setup() error {
|
||||||
|
|
||||||
defer a.Close()
|
defer a.Close()
|
||||||
defer j.Close()
|
defer j.Close()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ func TestDeleteJoke_200(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
reqBody := strings.NewReader("{\"key\":\"very secure\",\"token\":\"password\"}")
|
reqBody := strings.NewReader("{\"key\":\"very secure\",\"token\":\"password\"}")
|
||||||
req, _ := http.NewRequest("DELETE", "/id/1", reqBody)
|
req, _ := http.NewRequest("DELETE", "/id/1", reqBody)
|
||||||
res, err := app.Test(req, -1)
|
res, err := app.Test(req, -1)
|
||||||
|
|
|
@ -32,7 +32,7 @@ func setup() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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...)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -76,4 +76,4 @@ func TestGetSubmission_Params(t *testing.T) {
|
||||||
body, err := ioutil.ReadAll(res.Body)
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
assert.Nilf(t, err, "get submission")
|
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")
|
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")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,15 @@ package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"jokes-bapak2-api/app/v1/handler/health"
|
"jokes-bapak2-api/app/v1/handler/health"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/gofiber/fiber/v2/middleware/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Health(app *fiber.App) *fiber.App {
|
func Health(app *fiber.App) *fiber.App {
|
||||||
// Health check
|
// Health check
|
||||||
app.Get("/health", health.Health)
|
app.Get("/health", cache.New(cache.Config{Expiration: 30 * time.Minute}), health.Health)
|
||||||
|
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,8 +3,10 @@ package routes
|
||||||
import (
|
import (
|
||||||
"jokes-bapak2-api/app/v1/handler/joke"
|
"jokes-bapak2-api/app/v1/handler/joke"
|
||||||
"jokes-bapak2-api/app/v1/middleware"
|
"jokes-bapak2-api/app/v1/middleware"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/gofiber/fiber/v2/middleware/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Joke(app *fiber.App) *fiber.App {
|
func Joke(app *fiber.App) *fiber.App {
|
||||||
|
@ -12,13 +14,13 @@ func Joke(app *fiber.App) *fiber.App {
|
||||||
app.Get("/", joke.SingleJoke)
|
app.Get("/", joke.SingleJoke)
|
||||||
|
|
||||||
// Today's joke
|
// Today's joke
|
||||||
app.Get("/today", joke.TodayJoke)
|
app.Get("/today", cache.New(cache.Config{Expiration: 6 * time.Hour}), joke.TodayJoke)
|
||||||
|
|
||||||
// Joke by ID
|
// Joke by ID
|
||||||
app.Get("/id/:id", middleware.OnlyIntegerAsID(), joke.JokeByID)
|
app.Get("/id/:id", middleware.OnlyIntegerAsID(), joke.JokeByID)
|
||||||
|
|
||||||
// Count total jokes
|
// Count total jokes
|
||||||
app.Get("/total", joke.TotalJokes)
|
app.Get("/total", cache.New(cache.Config{Expiration: 15 * time.Minute}), joke.TotalJokes)
|
||||||
|
|
||||||
// Add new joke
|
// Add new joke
|
||||||
app.Put("/", middleware.RequireAuth(), joke.AddNewJoke)
|
app.Put("/", middleware.RequireAuth(), joke.AddNewJoke)
|
||||||
|
|
|
@ -2,13 +2,23 @@ package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"jokes-bapak2-api/app/v1/handler/submit"
|
"jokes-bapak2-api/app/v1/handler/submit"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/gofiber/fiber/v2/middleware/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Submit(app *fiber.App) *fiber.App {
|
func Submit(app *fiber.App) *fiber.App {
|
||||||
// Get pending submitted joke
|
// 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
|
// Add a joke
|
||||||
app.Post("/submit", submit.SubmitJoke)
|
app.Post("/submit", submit.SubmitJoke)
|
||||||
|
|
|
@ -45,4 +45,4 @@ func TestIsToday_ErrorIfInvalid(t *testing.T) {
|
||||||
if today != false {
|
if today != false {
|
||||||
t.Error("it should be false:", today)
|
t.Error("it should be false:", today)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,4 +23,4 @@ func TestRandomString_Invalid(t *testing.T) {
|
||||||
if len(random) != 20 {
|
if len(random) != 20 {
|
||||||
t.Error("result is not within the length of 10")
|
t.Error("result is not within the length of 10")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue