Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass http.Request all the way to custom resolver load functions #167

Merged
merged 2 commits into from
Jun 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Pass http.Request all the way down the pipeline to custom resolvers. (#167)

## 1.1.0

- Made Reddit Score field in Livestreamfails tooltip use humanized value. (#164)
Expand Down
5 changes: 3 additions & 2 deletions internal/resolvers/betterttv/run_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package betterttv

import (
"encoding/json"
"net/http"
"net/url"
)

func run(url *url.URL) ([]byte, error) {
func run(url *url.URL, r *http.Request) ([]byte, error) {
matches := emotePathRegex.FindStringSubmatch(url.Path)
if len(matches) != 2 {
return nil, errInvalidBTTVEmotePath
}

emoteHash := matches[1]

apiResponse := emoteCache.Get(emoteHash, nil)
apiResponse := emoteCache.Get(emoteHash, r)
return json.Marshal(apiResponse)
}
4 changes: 2 additions & 2 deletions internal/resolvers/default/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (dr *R) load(urlString string, r *http.Request) (interface{}, time.Duration

for _, m := range dr.customResolvers {
if m.Check(requestUrl) {
data, err := m.Run(requestUrl)
data, err := m.Run(requestUrl, r)

if errors.Is(err, resolver.ErrDontHandle) {
break
Expand Down Expand Up @@ -57,7 +57,7 @@ func (dr *R) load(urlString string, r *http.Request) (interface{}, time.Duration
if requestUrl.String() != resp.Request.URL.String() {
for _, m := range dr.customResolvers {
if m.Check(resp.Request.URL) {
data, err := m.Run(resp.Request.URL)
data, err := m.Run(resp.Request.URL, r)

if errors.Is(err, resolver.ErrDontHandle) {
break
Expand Down
5 changes: 3 additions & 2 deletions internal/resolvers/discord/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ package discord
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
)

func run(url *url.URL) ([]byte, error) {
func run(url *url.URL, r *http.Request) ([]byte, error) {
matches := discordInviteURLRegex.FindStringSubmatch(fmt.Sprintf("%s%s", strings.ToLower(url.Host), url.Path))
if len(matches) != 4 {
return nil, errInvalidDiscordInvite
}

inviteCode := matches[3]

apiResponse := inviteCache.Get(inviteCode, nil)
apiResponse := inviteCache.Get(inviteCode, r)
return json.Marshal(apiResponse)
}
5 changes: 3 additions & 2 deletions internal/resolvers/frankerfacez/run_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package frankerfacez

import (
"encoding/json"
"net/http"
"net/url"
)

func run(url *url.URL) ([]byte, error) {
func run(url *url.URL, r *http.Request) ([]byte, error) {
matches := emotePathRegex.FindStringSubmatch(url.Path)
if len(matches) != 4 {
return nil, errInvalidFrankerFaceZEmotePath
}

emoteHash := matches[1]

apiResponse := emoteCache.Get(emoteHash, nil)
apiResponse := emoteCache.Get(emoteHash, r)
return json.Marshal(apiResponse)

}
5 changes: 3 additions & 2 deletions internal/resolvers/imgur/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package imgur
import (
"encoding/json"
"errors"
"net/http"
"net/url"
)

func run(url *url.URL) ([]byte, error) {
imgurResponse, ok := imgurCache.Get(url.String(), nil).(response)
func run(url *url.URL, r *http.Request) ([]byte, error) {
imgurResponse, ok := imgurCache.Get(url.String(), r).(response)
if !ok {
return nil, errors.New("imgur cache load function is broken")
}
Expand Down
5 changes: 3 additions & 2 deletions internal/resolvers/livestreamfails/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"html/template"
"net/http"
"net/url"
"regexp"
"strings"
Expand Down Expand Up @@ -53,11 +54,11 @@ func New() (resolvers []resolver.CustomURLManager) {

return true
},
Run: func(url *url.URL) ([]byte, error) {
Run: func(url *url.URL, r *http.Request) ([]byte, error) {
pathParts := strings.Split(strings.TrimPrefix(url.Path, "/"), "/")
clipId := pathParts[1]

apiResponse := clipCache.Get(clipId, nil)
apiResponse := clipCache.Get(clipId, r)
return json.Marshal(apiResponse)
},
})
Expand Down
5 changes: 3 additions & 2 deletions internal/resolvers/oembed/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"html/template"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"

Expand Down Expand Up @@ -61,8 +62,8 @@ func New() (resolvers []resolver.CustomURLManager) {
Check: func(url *url.URL) bool {
return oEmbed.FindItem(url.String()) != nil
},
Run: func(url *url.URL) ([]byte, error) {
apiResponse := oEmbedCache.Get(url.String(), nil)
Run: func(url *url.URL, r *http.Request) ([]byte, error) {
apiResponse := oEmbedCache.Get(url.String(), r)
return json.Marshal(apiResponse)
},
})
Expand Down
5 changes: 3 additions & 2 deletions internal/resolvers/supinic/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package supinic

import (
"encoding/json"
"net/http"
"net/url"
)

func run(url *url.URL) ([]byte, error) {
func run(url *url.URL, r *http.Request) ([]byte, error) {
matches := trackPathRegex.FindStringSubmatch(url.Path)
if len(matches) != 2 {
return nil, errInvalidTrackPath
}

trackID := matches[1]

apiResponse := trackListCache.Get(trackID, nil)
apiResponse := trackListCache.Get(trackID, r)
return json.Marshal(apiResponse)
}
9 changes: 5 additions & 4 deletions internal/resolvers/twitch/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"html/template"
"log"
"net/http"
"net/url"
"strings"
"time"
Expand Down Expand Up @@ -75,11 +76,11 @@ func New() (resolvers []resolver.CustomURLManager) {
Check: func(url *url.URL) bool {
return utils.IsDomain(url, "clips.twitch.tv")
},
Run: func(url *url.URL) ([]byte, error) {
Run: func(url *url.URL, r *http.Request) ([]byte, error) {
pathParts := strings.Split(strings.TrimPrefix(url.Path, "/"), "/")
clipSlug := pathParts[0]

apiResponse := clipCache.Get(clipSlug, nil)
apiResponse := clipCache.Get(clipSlug, r)
return json.Marshal(apiResponse)
},
})
Expand All @@ -95,11 +96,11 @@ func New() (resolvers []resolver.CustomURLManager) {

return len(pathParts) >= 4 && pathParts[2] == "clip"
},
Run: func(url *url.URL) ([]byte, error) {
Run: func(url *url.URL, r *http.Request) ([]byte, error) {
pathParts := strings.Split(strings.TrimPrefix(url.Path, "/"), "/")
clipSlug := pathParts[2]

apiResponse := clipCache.Get(clipSlug, nil)
apiResponse := clipCache.Get(clipSlug, r)
return json.Marshal(apiResponse)
},
})
Expand Down
7 changes: 4 additions & 3 deletions internal/resolvers/twitter/run_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ package twitter

import (
"encoding/json"
"net/http"
"net/url"
"strings"

"github.com/Chatterino/api/pkg/resolver"
)

func run(url *url.URL) ([]byte, error) {
func run(url *url.URL, r *http.Request) ([]byte, error) {
if tweetRegexp.MatchString(url.String()) {
tweetID := getTweetIDFromURL(url)
if tweetID == "" {
return resolver.NoLinkInfoFound, nil
}

apiResponse := tweetCache.Get(tweetID, nil)
apiResponse := tweetCache.Get(tweetID, r)
return json.Marshal(apiResponse)
}

Expand All @@ -27,7 +28,7 @@ func run(url *url.URL) ([]byte, error) {
return resolver.NoLinkInfoFound, nil
}

apiResponse := twitterUserCache.Get(userName, nil)
apiResponse := twitterUserCache.Get(userName, r)
return json.Marshal(apiResponse)
}

Expand Down
5 changes: 3 additions & 2 deletions internal/resolvers/wikipedia/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package wikipedia
import (
"encoding/json"
"errors"
"net/http"
"net/url"
)

func run(url *url.URL) ([]byte, error) {
wikiResponse, ok := wikipediaCache.Get(url.String(), nil).(response)
func run(url *url.URL, r *http.Request) ([]byte, error) {
wikiResponse, ok := wikipediaCache.Get(url.String(), r).(response)
if !ok {
return nil, errors.New("wikipedia cache load function is broken")
}
Expand Down
9 changes: 5 additions & 4 deletions internal/resolvers/youtube/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"html/template"
"log"
"net/http"
"net/url"
"os"
"time"
Expand Down Expand Up @@ -55,14 +56,14 @@ func New() (resolvers []resolver.CustomURLManager) {
Check: func(url *url.URL) bool {
return utils.IsSubdomainOf(url, "youtube.com")
},
Run: func(url *url.URL) ([]byte, error) {
Run: func(url *url.URL, r *http.Request) ([]byte, error) {
videoID := getYoutubeVideoIDFromURL(url)

if videoID == "" {
return resolver.NoLinkInfoFound, nil
}

apiResponse := videoCache.Get(videoID, nil)
apiResponse := videoCache.Get(videoID, r)
return json.Marshal(apiResponse)
},
})
Expand All @@ -71,14 +72,14 @@ func New() (resolvers []resolver.CustomURLManager) {
Check: func(url *url.URL) bool {
return url.Host == "youtu.be"
},
Run: func(url *url.URL) ([]byte, error) {
Run: func(url *url.URL, r *http.Request) ([]byte, error) {
videoID := getYoutubeVideoIDFromURL2(url)

if videoID == "" {
return resolver.NoLinkInfoFound, nil
}

apiResponse := videoCache.Get(videoID, nil)
apiResponse := videoCache.Get(videoID, r)
return json.Marshal(apiResponse)
},
})
Expand Down
3 changes: 2 additions & 1 deletion pkg/resolver/pkg.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resolver

import (
"net/http"
"net/url"
"time"
)
Expand All @@ -23,7 +24,7 @@ type Response struct {

type CustomURLManager struct {
Check func(url *url.URL) bool
Run func(url *url.URL) ([]byte, error)
Run func(url *url.URL, r *http.Request) ([]byte, error)
}

var NoSpecialDur time.Duration