Skip to content

Commit

Permalink
feat: Add optional caching to rest server
Browse files Browse the repository at this point in the history
  • Loading branch information
mehmetumit committed Nov 5, 2023
1 parent 92549ad commit 724cc2a
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion internal/adapters/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"syscall"
"time"

"github.com/mehmetumit/dexus/internal/adapters/rest/middleware"
"github.com/mehmetumit/dexus/internal/core/ports"

"github.com/go-chi/chi/v5"
Expand All @@ -30,8 +31,14 @@ type Server struct {
app ports.AppRunner
}

type Option func(http.Handler) http.Handler

func NewServer(logger ports.Logger, config *ServerConfig, app ports.AppRunner) {
func WithCacher(c ports.Cacher, l ports.Logger, ttl time.Duration) Option {
cacheInterceptor := middleware.NewCacheInterceptor(c, l, ttl)
return cacheInterceptor.InterceptHandler
}

func NewServer(logger ports.Logger, config *ServerConfig, app ports.AppRunner, options ...Option) {
router := chi.NewRouter()
router.Use(chiMiddleware.Logger, chiMiddleware.Recoverer)
router.Use(cors.Handler(cors.Options{
Expand All @@ -44,6 +51,10 @@ func NewServer(logger ports.Logger, config *ServerConfig, app ports.AppRunner) {
AllowCredentials: true,
MaxAge: 300, // Maximum value not ignored by any of major browsers
}))
// Load optional middlewares
for _, op := range options {
router.Use(op)
}
s := &http.Server{
Addr: config.Addr,
Handler: router,
Expand Down

0 comments on commit 724cc2a

Please sign in to comment.