Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into feature/correctly-s…
Browse files Browse the repository at this point in the history
…trip-trailing-slashes
  • Loading branch information
pajlada committed Jan 28, 2023
2 parents ff5acb7 + 7c59cf9 commit b347edd
Show file tree
Hide file tree
Showing 22 changed files with 112 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
- Dev: Update to Twitter's v2 API. (#414)
- Dev: Add HTTP Caching headers. (#417)
- Dev: Add custom middleware to strip trailing slashes. (#422)
- Dev: Make cache timeout durations configurable. (#419)

## 1.2.3

Expand Down
51 changes: 51 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,73 @@
# Discord token, provides rich information for Discord invite links
#discord-token: ""

# Cache duration for Discord invite links
#discord-invite-cache-duration: 6h

# Twitch Developer Application client ID and Twitch Developer Application client secret, provide rich information for Twitch Clips
#twitch-client-id: ""
#twitch-client-secret: ""

# Cache duration for Twitch Username -> Twitch ID lookups
#twitch-username-cache-duration: 1h
# Cache duration for Twitch clip links
#twitch-clip-cache-duration: 1h

# Cache duration for the Twitchemotes.com emote cache
#twitchemotes-emote-cache-duration: 30m

# YouTube API key, provides rich information for YouTube video links
#youtube-api-key: ""

# Cache duration for YouTube channel/user profile links
#youtube-channel-cache-duration: 48h
# Cache duration for YouTube video links
#youtube-video-cache-duration: 48h

# Twitter bearer token, provides rich information for tweet and Twitter user links
#twitter-bearer-token: ""

# Cache duration for Twitter post/tweet links
#twitter-tweet-cache-duration: 24h
# Cache duration for Twitter user profile links
#twitter-user-cache-duration: 24h

# Imgur client ID, provides rich information for imgur image links
#imgur-client-id: ""

# Cache duration for Imgur image and album links
#imgur-cache-duration: 1h

# oEmbed Facebook app ID and app secret, provide rich information for Facebook and Instagram links
#oembed-facebook-app-id: ""
#oembed-facebook-app-secret: ""

# Path to a json file containing supported oEmbed resolvers
#oembed-providers-path: "./data/oembed/providers.json"

# Cache duration for oEmbed links
#oembed-cache-duration: 1h

# Cache duration proxied thumbnails
#thumbnail-cache-duration: 10m

# Cache duration for links that don't have a specialized resolver
#default-link-cache-duration: 10m

# Cache duration for BetterTTV emote links
#bttv-emote-cache-duration: 1h

# Cache duration for FrankerFaceZ emote links
#ffz-emote-cache-duration: 1h

# Cache duration for 7TV emote links
#seventv-emote-cache-duration: 1h

# Cache duration for livestreamfails.com clip links
#livestreamfails-clip-cache-duration: 1h

# Cache duration for Supinic.com track links
#supinic-track-cache-duration: 1h

# Cache duration for Wikipedia article links
#wikipedia-article-cache-duration: 1h
3 changes: 1 addition & 2 deletions internal/caches/twitchusernamecache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package twitchusernamecache

import (
"context"
"time"

"github.com/Chatterino/api/internal/db"
"github.com/Chatterino/api/pkg/cache"
Expand All @@ -20,6 +19,6 @@ func New(ctx context.Context, cfg config.APIConfig, pool db.Pool, helixClient *h
}

return cache.NewPostgreSQLCache(
ctx, cfg, pool, cache.NewPrefixKeyProvider("twitch:username"), usernameLoader, 1*time.Hour,
ctx, cfg, pool, cache.NewPrefixKeyProvider("twitch:username"), usernameLoader, cfg.TwitchUsernameCacheDuration,
)
}
3 changes: 1 addition & 2 deletions internal/resolvers/betterttv/emote_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"net/http"
"net/url"
"time"

"github.com/Chatterino/api/internal/db"
"github.com/Chatterino/api/pkg/cache"
Expand Down Expand Up @@ -51,7 +50,7 @@ func NewEmoteResolver(ctx context.Context, cfg config.APIConfig, pool db.Pool, e
r := &EmoteResolver{
emoteCache: cache.NewPostgreSQLCache(
ctx, cfg, pool, cache.NewPrefixKeyProvider("betterttv:emote"),
resolver.NewResponseMarshaller(emoteLoader), 1*time.Hour),
resolver.NewResponseMarshaller(emoteLoader), cfg.BttvEmoteCacheDuration),
}

return r
Expand Down
5 changes: 2 additions & 3 deletions internal/resolvers/default/link_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/http"
"net/url"
"strings"
"time"

"github.com/Chatterino/api/internal/db"
"github.com/Chatterino/api/internal/logger"
Expand Down Expand Up @@ -266,10 +265,10 @@ func New(ctx context.Context, cfg config.APIConfig, pool db.Pool, helixClient *h

thumbnailCache := cache.NewPostgreSQLCache(
ctx, cfg, pool, cache.NewPrefixKeyProvider("default:thumbnail"), thumbnailLoader,
10*time.Minute,
cfg.ThumbnailCacheDuration,
)
linkCache := cache.NewPostgreSQLCache(
ctx, cfg, pool, cache.NewPrefixKeyProvider("default:link"), linkLoader, 10*time.Minute,
ctx, cfg, pool, cache.NewPrefixKeyProvider("default:link"), linkLoader, cfg.DefaultLinkCacheDuration,
)

r := &LinkResolver{
Expand Down
3 changes: 1 addition & 2 deletions internal/resolvers/discord/invite_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/http"
"net/url"
"strings"
"time"

"github.com/Chatterino/api/internal/db"
"github.com/Chatterino/api/pkg/cache"
Expand Down Expand Up @@ -47,7 +46,7 @@ func NewInviteResolver(ctx context.Context, cfg config.APIConfig, pool db.Pool)
r := &InviteResolver{
inviteCache: cache.NewPostgreSQLCache(
ctx, cfg, pool, cache.NewPrefixKeyProvider("discord:invite"),
resolver.NewResponseMarshaller(inviteLoader), 6*time.Hour),
resolver.NewResponseMarshaller(inviteLoader), cfg.DiscordInviteCacheDuration),
}

return r
Expand Down
3 changes: 1 addition & 2 deletions internal/resolvers/frankerfacez/emote_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"net/http"
"net/url"
"time"

"github.com/Chatterino/api/internal/db"
"github.com/Chatterino/api/pkg/cache"
Expand Down Expand Up @@ -49,7 +48,7 @@ func NewEmoteResolver(ctx context.Context, cfg config.APIConfig, pool db.Pool, e
r := &EmoteResolver{
emoteCache: cache.NewPostgreSQLCache(
ctx, cfg, pool, cache.NewPrefixKeyProvider("frankerfacez:emote"),
resolver.NewResponseMarshaller(emoteLoader), 1*time.Hour),
resolver.NewResponseMarshaller(emoteLoader), cfg.FfzEmoteCacheDuration),
}

return r
Expand Down
3 changes: 1 addition & 2 deletions internal/resolvers/imgur/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"net/http"
"net/url"
"time"

"github.com/Chatterino/api/internal/db"
"github.com/Chatterino/api/pkg/cache"
Expand Down Expand Up @@ -61,7 +60,7 @@ func NewResolver(ctx context.Context, cfg config.APIConfig, pool db.Pool, imgurC
r := &Resolver{
imgurCache: cache.NewPostgreSQLCache(
ctx, cfg, pool, cache.NewPrefixKeyProvider("imgur"),
resolver.NewResponseMarshaller(loader), 1*time.Hour),
resolver.NewResponseMarshaller(loader), cfg.ImgurCacheDuration),
}

return r
Expand Down
3 changes: 1 addition & 2 deletions internal/resolvers/livestreamfails/clip_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"net/http"
"net/url"
"time"

"github.com/Chatterino/api/internal/db"
"github.com/Chatterino/api/pkg/cache"
Expand Down Expand Up @@ -53,7 +52,7 @@ func NewClipResolver(ctx context.Context, cfg config.APIConfig, pool db.Pool, ap
r := &ClipResolver{
clipCache: cache.NewPostgreSQLCache(
ctx, cfg, pool, cache.NewPrefixKeyProvider("livestreamfails:clip"),
resolver.NewResponseMarshaller(clipLoader), 1*time.Hour),
resolver.NewResponseMarshaller(clipLoader), cfg.LivestreamfailsClipCacheDuration),
}

return r
Expand Down
3 changes: 1 addition & 2 deletions internal/resolvers/oembed/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"log"
"net/http"
"net/url"
"time"

"github.com/Chatterino/api/internal/db"
"github.com/Chatterino/api/pkg/cache"
Expand Down Expand Up @@ -57,7 +56,7 @@ func NewResolver(ctx context.Context, cfg config.APIConfig, pool db.Pool, data [
r := &Resolver{
oEmbedCache: cache.NewPostgreSQLCache(
ctx, cfg, pool, cache.NewPrefixKeyProvider("oembed"),
resolver.NewResponseMarshaller(loader), 1*time.Hour,
resolver.NewResponseMarshaller(loader), cfg.OembedCacheDuration,
),
oEmbed: oEmbed,
}
Expand Down
3 changes: 1 addition & 2 deletions internal/resolvers/seventv/emote_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"net/http"
"net/url"
"time"

"github.com/Chatterino/api/internal/db"
"github.com/Chatterino/api/pkg/cache"
Expand Down Expand Up @@ -45,7 +44,7 @@ func NewEmoteResolver(ctx context.Context, cfg config.APIConfig, pool db.Pool, a
r := &EmoteResolver{
emoteCache: cache.NewPostgreSQLCache(
ctx, cfg, pool, cache.NewPrefixKeyProvider("seventv:emote"),
resolver.NewResponseMarshaller(emoteLoader), 1*time.Hour),
resolver.NewResponseMarshaller(emoteLoader), cfg.SeventvEmoteCacheDuration),
}

return r
Expand Down
3 changes: 1 addition & 2 deletions internal/resolvers/supinic/track_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"net/http"
"net/url"
"time"

"github.com/Chatterino/api/internal/db"
"github.com/Chatterino/api/pkg/cache"
Expand Down Expand Up @@ -50,7 +49,7 @@ func NewTrackResolver(ctx context.Context, cfg config.APIConfig, pool db.Pool) *
r := &TrackResolver{
trackCache: cache.NewPostgreSQLCache(
ctx, cfg, pool, cache.NewPrefixKeyProvider("supinic:track"),
resolver.NewResponseMarshaller(trackLoader), 1*time.Hour),
resolver.NewResponseMarshaller(trackLoader), cfg.SupinicTrackCacheDuration),
}

return r
Expand Down
3 changes: 1 addition & 2 deletions internal/resolvers/twitch/clip_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
"net/url"
"regexp"
"time"

"github.com/Chatterino/api/internal/db"
"github.com/Chatterino/api/pkg/cache"
Expand Down Expand Up @@ -76,7 +75,7 @@ func NewClipResolver(ctx context.Context, cfg config.APIConfig, pool db.Pool, he
r := &ClipResolver{
clipCache: cache.NewPostgreSQLCache(
ctx, cfg, pool, cache.NewPrefixKeyProvider("twitch:clip"),
resolver.NewResponseMarshaller(clipLoader), 1*time.Hour,
resolver.NewResponseMarshaller(clipLoader), cfg.TwitchClipCacheDuration,
),
}

Expand Down
5 changes: 2 additions & 3 deletions internal/resolvers/twitter/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
"net/url"
"strings"
"time"

"github.com/Chatterino/api/internal/db"
"github.com/Chatterino/api/pkg/cache"
Expand Down Expand Up @@ -97,13 +96,13 @@ func NewTwitterResolver(

tweetCache := cache.NewPostgreSQLCache(
ctx, cfg, pool, tweetCacheKeyProvider, resolver.NewResponseMarshaller(tweetLoader),
24*time.Hour,
cfg.TwitterTweetCacheDuration,
)
tweetCache.RegisterDependent(ctx, collageCache)

userCache := cache.NewPostgreSQLCache(
ctx, cfg, pool, userCacheKeyProvider, resolver.NewResponseMarshaller(userLoader),
24*time.Hour,
cfg.TwitterUserCacheDuration,
)

r := &TwitterResolver{
Expand Down
3 changes: 1 addition & 2 deletions internal/resolvers/wikipedia/article_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
"net/url"
"strings"
"time"

"github.com/Chatterino/api/internal/db"
"github.com/Chatterino/api/pkg/cache"
Expand Down Expand Up @@ -77,7 +76,7 @@ func NewArticleResolver(ctx context.Context, cfg config.APIConfig, pool db.Pool,
r := &ArticleResolver{
articleCache: cache.NewPostgreSQLCache(
ctx, cfg, pool, cache.NewPrefixKeyProvider("wikipedia:article"),
resolver.NewResponseMarshaller(articleLoader), 1*time.Hour,
resolver.NewResponseMarshaller(articleLoader), cfg.WikipediaArticleCacheDuration,
),
}

Expand Down
3 changes: 1 addition & 2 deletions internal/resolvers/youtube/channel_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
"net/url"
"regexp"
"time"

"github.com/Chatterino/api/internal/db"
"github.com/Chatterino/api/internal/logger"
Expand Down Expand Up @@ -61,7 +60,7 @@ func NewYouTubeChannelResolver(ctx context.Context, cfg config.APIConfig, pool d

r := &YouTubeChannelResolver{
channelCache: cache.NewPostgreSQLCache(
ctx, cfg, pool, cache.NewPrefixKeyProvider("youtube:channel"), loader, 48*time.Hour,
ctx, cfg, pool, cache.NewPrefixKeyProvider("youtube:channel"), loader, cfg.YoutubeChannelCacheDuration,
),
}

Expand Down
3 changes: 1 addition & 2 deletions internal/resolvers/youtube/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package youtube
import (
"context"
"html/template"
"time"

"github.com/Chatterino/api/internal/db"
"github.com/Chatterino/api/internal/logger"
Expand Down Expand Up @@ -44,7 +43,7 @@ var (
func NewYouTubeVideoResolvers(ctx context.Context, cfg config.APIConfig, pool db.Pool, youtubeClient *youtubeAPI.Service) (resolver.Resolver, resolver.Resolver) {
videoLoader := NewVideoLoader(youtubeClient)
videoCache := cache.NewPostgreSQLCache(
ctx, cfg, pool, cache.NewPrefixKeyProvider("youtube:video"), videoLoader, 48*time.Hour,
ctx, cfg, pool, cache.NewPrefixKeyProvider("youtube:video"), videoLoader, cfg.YoutubeVideoCacheDuration,
)

videoResolver := NewYouTubeVideoResolver(videoCache)
Expand Down
3 changes: 1 addition & 2 deletions internal/resolvers/youtube/video_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http/httptest"
"net/url"
"testing"
"time"

"github.com/Chatterino/api/internal/logger"
"github.com/Chatterino/api/pkg/cache"
Expand Down Expand Up @@ -56,7 +55,7 @@ func TestVideoResolver(t *testing.T) {

loader := NewVideoLoader(youtubeClient)
videoCache := cache.NewPostgreSQLCache(
ctx, cfg, pool, cache.NewPrefixKeyProvider("youtube:video"), loader, 24*time.Hour,
ctx, cfg, pool, cache.NewPrefixKeyProvider("youtube:video"), loader, cfg.YoutubeVideoCacheDuration,
)

resolver := NewYouTubeVideoResolver(videoCache)
Expand Down
3 changes: 1 addition & 2 deletions internal/resolvers/youtube/video_shorturl_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http/httptest"
"net/url"
"testing"
"time"

"github.com/Chatterino/api/internal/logger"
"github.com/Chatterino/api/pkg/cache"
Expand Down Expand Up @@ -56,7 +55,7 @@ func TestVideoShortURLResolver(t *testing.T) {

loader := NewVideoLoader(youtubeClient)
videoCache := cache.NewPostgreSQLCache(
ctx, cfg, pool, cache.NewPrefixKeyProvider("youtube:video"), loader, 24*time.Hour,
ctx, cfg, pool, cache.NewPrefixKeyProvider("youtube:video"), loader, cfg.YoutubeVideoCacheDuration,
)

resolver := NewYouTubeVideoShortURLResolver(videoCache)
Expand Down
3 changes: 1 addition & 2 deletions internal/routes/twitchemotes/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"log"
"net/http"
"time"

"github.com/Chatterino/api/internal/db"
"github.com/Chatterino/api/pkg/cache"
Expand Down Expand Up @@ -60,7 +59,7 @@ func Initialize(ctx context.Context, cfg config.APIConfig, pool db.Pool, router
}
twitchemotesCache := cache.NewPostgreSQLCache(
ctx, cfg, pool, cache.NewPrefixKeyProvider("twitchemotes"), loader,
time.Duration(30)*time.Minute,
cfg.TwitchemotesEmoteCacheDuration,
)

router.Get("/twitchemotes/set/{setID}", func(w http.ResponseWriter, r *http.Request) {
Expand Down
Loading

0 comments on commit b347edd

Please sign in to comment.