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

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