-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apply cache headers to some most used routes
- Loading branch information
1 parent
d4cf781
commit 4e7f1a4
Showing
21 changed files
with
696 additions
and
579 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package profile_cache | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/rs/xid" | ||
|
||
"github.com/Southclaws/storyden/app/resources/cachecontrol" | ||
"github.com/Southclaws/storyden/internal/ent" | ||
"github.com/Southclaws/storyden/internal/ent/account" | ||
"github.com/Southclaws/storyden/internal/ent/post" | ||
) | ||
|
||
type Cache struct { | ||
db *ent.Client | ||
} | ||
|
||
func New(db *ent.Client) *Cache { | ||
return &Cache{ | ||
db: db, | ||
} | ||
} | ||
|
||
func (c *Cache) IsNotModified(ctx context.Context, cq cachecontrol.Query, id xid.ID) bool { | ||
r, err := c.db.Account.Query().Select(post.FieldUpdatedAt).Where(account.ID(id)).Only(ctx) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
notModified := cq.NotModified(r.UpdatedAt) | ||
|
||
return notModified | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package reqinfo | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/mileusna/useragent" | ||
|
||
"github.com/Southclaws/opt" | ||
"github.com/Southclaws/storyden/app/resources/cachecontrol" | ||
) | ||
|
||
type Info struct { | ||
UserAgent useragent.UserAgent | ||
CacheQuery cachecontrol.Query | ||
} | ||
|
||
type infoKey struct{} | ||
|
||
func WithRequestInfo(ctx context.Context, r *http.Request) context.Context { | ||
ua := useragent.Parse(r.Header.Get("User-Agent")) | ||
|
||
ifNoneMatch := opt.NewIf(r.Header.Get("If-None-Match"), notEmpty) | ||
|
||
ifModifiedSince, err := opt.MapErr( | ||
opt.NewIf(r.Header.Get("If-Modified-Since"), notEmpty), | ||
parseConditionalRequestTime, | ||
) | ||
if err != nil { | ||
ifModifiedSince = opt.NewEmpty[time.Time]() | ||
} | ||
|
||
info := Info{ | ||
UserAgent: ua, | ||
CacheQuery: cachecontrol.NewQuery(ifNoneMatch, ifModifiedSince), | ||
} | ||
|
||
return context.WithValue(ctx, infoKey{}, info) | ||
} | ||
|
||
func GetDeviceName(ctx context.Context) string { | ||
v := ctx.Value(infoKey{}) | ||
i, ok := v.(Info) | ||
if !ok { | ||
return "Unknown" | ||
} | ||
|
||
ua := i.UserAgent | ||
|
||
return fmt.Sprintf("%s (%s)", ua.Name, ua.OS) | ||
} | ||
|
||
func GetCacheQuery(ctx context.Context) cachecontrol.Query { | ||
v := ctx.Value(infoKey{}) | ||
i, ok := v.(Info) | ||
if !ok { | ||
return cachecontrol.Query{} | ||
} | ||
|
||
return i.CacheQuery | ||
} | ||
|
||
func notEmpty(s string) bool { | ||
return s != "" | ||
} | ||
|
||
func parseConditionalRequestTime(in string) (time.Time, error) { | ||
return time.Parse(time.RFC1123, in) | ||
} |
Oops, something went wrong.