Skip to content

Commit

Permalink
add 1 hour expiration to flightnumber endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
its-felix committed Oct 8, 2024
1 parent 24841ca commit a6dec31
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
7 changes: 3 additions & 4 deletions go/api/web/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ import (
"github.com/explore-flights/monorepo/go/common/xtime"
"github.com/labstack/echo/v4"
"net/http"
"time"
)

func noCache(c echo.Context) {
c.Response().Header().Set(echo.HeaderCacheControl, "private, no-cache, no-store, max-age=0, must-revalidate")
}

func NewAirportsEndpoint(dh *data.Handler) echo.HandlerFunc {
return func(c echo.Context) error {
airports, err := dh.Airports(c.Request().Context())
Expand All @@ -30,6 +27,8 @@ func NewAircraftEndpoint(dh *data.Handler) echo.HandlerFunc {

func NewFlightNumberEndpoint(dh *data.Handler) echo.HandlerFunc {
return func(c echo.Context) error {
addExpirationHeaders(c, time.Now(), time.Hour)

fnRaw := c.Param("fn")
airport := c.Param("airport")
dateRaw := c.Param("date")
Expand Down
10 changes: 2 additions & 8 deletions go/api/web/sitemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package web
import (
"context"
"encoding/xml"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
Expand Down Expand Up @@ -56,19 +55,14 @@ func (l *s3ListObjectsIterator) Err() error {
}

func NewSitemapHandler(s3c adapt.S3Lister, bucket string) echo.HandlerFunc {
ttl := time.Hour * 3
cacheControlHeaderValue := fmt.Sprintf("public, max-age=%d, must-revalidate", int(ttl.Seconds()))
const ttl = time.Hour * 3

return func(c echo.Context) error {
now := time.Now().UTC() // http.TimeFormat requires UTC time
expiresAt := now.Add(ttl)
baseURL := baseUrl(c)

res := c.Response()
res.Header().Set(echo.HeaderContentType, echo.MIMEApplicationXMLCharsetUTF8)
res.Header().Set("Date", now.Format(http.TimeFormat))
res.Header().Set("Expires", expiresAt.Format(http.TimeFormat))
res.Header().Set(echo.HeaderCacheControl, cacheControlHeaderValue)
addExpirationHeaders(c, time.Now(), ttl)

_, err := res.Write([]byte(xml.Header))
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions go/api/web/util.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package web

import (
"fmt"
"github.com/labstack/echo/v4"
"net/http"
"strings"
"time"
)

func baseUrl(c echo.Context) string {
Expand Down Expand Up @@ -37,3 +40,19 @@ func contextSchemeAndHost(c echo.Context) (string, string) {

return c.Scheme(), req.Host
}

func addExpirationHeaders(c echo.Context, now time.Time, expiration time.Duration) {
now = now.UTC()
expiresAt := now.Add(expiration)

res := c.Response()
res.Header().Set("Date", now.Format(http.TimeFormat))
res.Header().Set("Expires", expiresAt.Format(http.TimeFormat))
res.Header().Set(echo.HeaderCacheControl, fmt.Sprintf("public, max-age=%d, must-revalidate", int(expiration.Seconds())))
}

func noCache(c echo.Context) {
res := c.Response()
res.Header().Del("Expires")
res.Header().Set(echo.HeaderCacheControl, "private, no-cache, no-store, max-age=0, must-revalidate")
}

0 comments on commit a6dec31

Please sign in to comment.