cache-control header

This commit is contained in:
Egor Aristov 2025-03-11 18:33:23 +03:00
parent e19d616401
commit 03558563df
Signed by: egor3f
GPG Key ID: 40482A264AAEC85F

View File

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"fmt"
wizard_vue "github.com/egor3f/rssalchemy/frontend/wizard-vue" wizard_vue "github.com/egor3f/rssalchemy/frontend/wizard-vue"
"github.com/egor3f/rssalchemy/internal/adapters/natsadapter" "github.com/egor3f/rssalchemy/internal/adapters/natsadapter"
httpApi "github.com/egor3f/rssalchemy/internal/api/http" httpApi "github.com/egor3f/rssalchemy/internal/api/http"
@ -56,7 +57,11 @@ func main() {
setIPExtractor(e, cfg) setIPExtractor(e, cfg)
e.StaticFS("/", echo.MustSubFS(wizard_vue.EmbedFS, wizard_vue.FSPrefix)) cacheGroup := e.Group("", addCacheControlHeader(1*time.Hour))
cacheGroup.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: wizard_vue.FSPrefix,
Filesystem: http.FS(wizard_vue.EmbedFS),
}))
apiHandler := httpApi.New( apiHandler := httpApi.New(
na, na,
@ -80,6 +85,18 @@ func main() {
} }
} }
func addCacheControlHeader(ttl time.Duration) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Response().Header().Set(
echo.HeaderCacheControl,
fmt.Sprintf("public, max-age=%d", int(ttl.Seconds())),
)
return next(c)
}
}
}
func setIPExtractor(e *echo.Echo, cfg config.Config) { func setIPExtractor(e *echo.Echo, cfg config.Config) {
if len(cfg.RealIpHeader) > 0 { if len(cfg.RealIpHeader) > 0 {
// Real ip header // Real ip header