feat: cache and limiter configurations
This commit is contained in:
parent
98bff40964
commit
f8e7eb0cea
|
@ -66,7 +66,10 @@ type Config struct {
|
||||||
}
|
}
|
||||||
Routes struct {
|
Routes struct {
|
||||||
Calendar struct {
|
Calendar struct {
|
||||||
CacheExpiration time.Duration `env:"ROUTES_CACHE_EXPIRATION,default=24h"`
|
CacheExpiration time.Duration `env:"ROUTES_CACHE_EXPIRATION,default=24h"`
|
||||||
|
CacheControl bool `env:"ROUTES_CACHE_CONTROL,default=true"`
|
||||||
|
LimiterMaxRequest int `env:"ROUTES_CALENDAR_LIMITER_MAX_REQUESTS,default=2"`
|
||||||
|
LimiterExpiration time.Duration `env:"ROUTES_CALENDAR_LIMITER_DURATION,default=1s"`
|
||||||
}
|
}
|
||||||
Static struct {
|
Static struct {
|
||||||
Path string `env:"ROUTES_STATIC_PATH,default=/static"`
|
Path string `env:"ROUTES_STATIC_PATH,default=/static"`
|
||||||
|
|
|
@ -3,7 +3,7 @@ package routes
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"strings"
|
||||||
|
|
||||||
"github.com/emersion/go-ical"
|
"github.com/emersion/go-ical"
|
||||||
"github.com/fmartingr/notion2ical/internal/config"
|
"github.com/fmartingr/notion2ical/internal/config"
|
||||||
|
@ -21,28 +21,18 @@ type CalendarRoutes struct {
|
||||||
notion *notionClient.NotionClient
|
notion *notionClient.NotionClient
|
||||||
publicHostname string
|
publicHostname string
|
||||||
thanksMessage string
|
thanksMessage string
|
||||||
|
|
||||||
|
limiterHandler fiber.Handler
|
||||||
|
cacheHandler fiber.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *CalendarRoutes) Setup() *CalendarRoutes {
|
func (r *CalendarRoutes) Setup() *CalendarRoutes {
|
||||||
r.router.
|
r.router.
|
||||||
Use(limiter.New(limiter.Config{
|
Use(r.limiterHandler).
|
||||||
Max: 2,
|
|
||||||
Expiration: time.Second,
|
|
||||||
// LimitReached: func(c *fiber.Ctx) error {
|
|
||||||
// return c.SendFile("./toofast.html")
|
|
||||||
// },
|
|
||||||
})).
|
|
||||||
Post("/wizard", r.wizardHandler).
|
Post("/wizard", r.wizardHandler).
|
||||||
Post("/download", r.downloadHandler).
|
Post("/download", r.downloadHandler).
|
||||||
Get("/", r.indexHandler).
|
Get("/", r.indexHandler).
|
||||||
Use(cache.New(cache.Config{
|
Use(r.cacheHandler).
|
||||||
Expiration: 24 * time.Hour,
|
|
||||||
CacheControl: true,
|
|
||||||
CacheHeader: "X-Cache",
|
|
||||||
KeyGenerator: func(c *fiber.Ctx) string {
|
|
||||||
return utils.CopyString(c.Path() + string(c.Context().QueryArgs().QueryString()))
|
|
||||||
},
|
|
||||||
})).
|
|
||||||
Get("/calendar.ics", r.calendarIcsHandler)
|
Get("/calendar.ics", r.calendarIcsHandler)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
@ -157,7 +147,7 @@ func (r *CalendarRoutes) calendarIcsHandler(c *fiber.Ctx) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// c.Set("Content-Type", "text/calendar")
|
c.Set("Content-Type", "text/calendar")
|
||||||
return c.Send(buf.Bytes())
|
return c.Send(buf.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,6 +158,28 @@ func NewCalendarRoutes(logger *zap.Logger, cfg *config.Config) *CalendarRoutes {
|
||||||
router: fiber.New(),
|
router: fiber.New(),
|
||||||
publicHostname: cfg.Http.PublicHostname,
|
publicHostname: cfg.Http.PublicHostname,
|
||||||
thanksMessage: cfg.Branding.ThanksMessage,
|
thanksMessage: cfg.Branding.ThanksMessage,
|
||||||
|
limiterHandler: limiter.New(limiter.Config{
|
||||||
|
Max: cfg.Routes.Calendar.LimiterMaxRequest,
|
||||||
|
Expiration: cfg.Routes.Calendar.LimiterExpiration,
|
||||||
|
}),
|
||||||
|
cacheHandler: cache.New(cache.Config{
|
||||||
|
Expiration: cfg.Routes.Calendar.CacheExpiration,
|
||||||
|
CacheControl: cfg.Routes.Calendar.CacheControl,
|
||||||
|
KeyGenerator: func(c *fiber.Ctx) string {
|
||||||
|
args := c.Context().QueryArgs()
|
||||||
|
params := []string{
|
||||||
|
"database_id",
|
||||||
|
string(args.Peek("database_id")),
|
||||||
|
"all_day_events",
|
||||||
|
string(args.Peek("all_day_events")),
|
||||||
|
"date_property",
|
||||||
|
string(args.Peek("date_property")),
|
||||||
|
"name_propety",
|
||||||
|
string(args.Peek("name_propety")),
|
||||||
|
}
|
||||||
|
return utils.CopyString(c.Path() + strings.Join(params, "-"))
|
||||||
|
},
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
routes.Setup()
|
routes.Setup()
|
||||||
return &routes
|
return &routes
|
||||||
|
|
Loading…
Reference in New Issue