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

Use Helix for loading Emote Set info #175

Merged
merged 11 commits into from
Jul 3, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Added link preview support for 7tv emote links. (#155)
- Skip lilliput if image is below maxThumbnailSize. (#184)
- Dev: Change Emote Set backend from `twitchemotes.com` to the Twitch Helix API. (#175)

## 1.2.0

Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ Go web service that serves as a cache to APIs that each Chatterino client could
Emote data is served cached from [twitchemotes.com](https://twitchemotes.com/).

## Routes
`/twitchemotes/set/:setID/`

`/twitchemotes/set/:setID`
Returns information about a given twitch emote set. Example response:

```
{
"channel_name": "forsen", // twitch user name
Expand All @@ -21,6 +23,7 @@ Returns information about a given twitch emote set. Example response:

`link_resolver/:url`
Resolves a url into a preview tooltip. Example response:

```
{
"status": 200, // status code returned from the page
Expand All @@ -33,23 +36,27 @@ Resolves a url into a preview tooltip. Example response:

`health/uptime`
Returns API service's uptime. Example response:

```
928h2m53.795354922s
```

`health/memory`
Returns information about memory usage. Example response:

```
Alloc=505 MiB, TotalAlloc=17418866 MiB, Sys=3070 MiB, NumGC=111245
```

`health/combined`
Returns both uptime and information about memory usage. Example response:

```
Uptime: 928h5m7.937821282s - Memory: Alloc=510 MiB, TotalAlloc=17419213 MiB, Sys=3070 MiB, NumGC=111246
```

## Using your self-hosted version

If you host your own version of this API, you can modify which url Chatterino2 uses to resolve links and to resolve twitch emote sets.
[Change link resolver](https://wiki.chatterino.com/Environment%20Variables/#chatterino2_link_resolver_url)
[Change Twitch emote resolver](https://wiki.chatterino.com/Environment%20Variables/#chatterino2_twitch_emote_set_resolver_url)
Expand Down
4 changes: 2 additions & 2 deletions cmd/api/link_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func TestResolveTwitchClip(t *testing.T) {
router := chi.NewRouter()
cfg := config.New()
defaultresolver.Initialize(router, cfg)
defaultresolver.Initialize(router, cfg, nil)
ts := httptest.NewServer(router)
defer ts.Close()
fmt.Println(ts.URL)
Expand Down Expand Up @@ -45,7 +45,7 @@ func TestResolveTwitchClip(t *testing.T) {
func TestResolveTwitchClip2(t *testing.T) {
router := chi.NewRouter()
cfg := config.New()
defaultresolver.Initialize(router, cfg)
defaultresolver.Initialize(router, cfg, nil)
ts := httptest.NewServer(router)
defer ts.Close()
const url = `https%3A%2F%2Ftwitch.tv%2Fpajlada%2Fclip%2FGorgeousAntsyPizzaSaltBae`
Expand Down
18 changes: 16 additions & 2 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import (
"time"

defaultresolver "github.com/Chatterino/api/internal/resolvers/default"
"github.com/Chatterino/api/internal/routes/twitchemotes"
"github.com/Chatterino/api/internal/twitchapiclient"
"github.com/Chatterino/api/pkg/cache"
"github.com/Chatterino/api/pkg/config"
"github.com/Chatterino/api/pkg/resolver"
"github.com/Chatterino/api/pkg/thumbnail"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)

var (
Expand Down Expand Up @@ -65,9 +69,19 @@ func main() {

router := chi.NewRouter()

handleTwitchEmotes(router)
// Strip trailing slashes from API requests
router.Use(middleware.StripSlashes)

var helixUsernameCache *cache.Cache

helixClient, helixUsernameCache, err := twitchapiclient.New(cfg)
if err != nil {
log.Printf("[Twitch] %s\n", err.Error())
}

twitchemotes.Initialize(router, helixClient, helixUsernameCache)
handleHealth(router)
defaultresolver.Initialize(router, cfg)
defaultresolver.Initialize(router, cfg, helixClient)

listen(cfg.BindAddress, mountRouter(router))
}
163 changes: 0 additions & 163 deletions cmd/api/twitchemotes.go

This file was deleted.

9 changes: 5 additions & 4 deletions internal/resolvers/default/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/Chatterino/api/pkg/thumbnail"
"github.com/Chatterino/api/pkg/utils"
"github.com/go-chi/chi/v5"
"github.com/nicklaw5/helix"
)

const (
Expand Down Expand Up @@ -87,7 +88,7 @@ func (dr *R) HandleThumbnailRequest(w http.ResponseWriter, r *http.Request) {
}
}

func New(cfg config.APIConfig) *R {
func New(cfg config.APIConfig, helixClient *helix.Client) *R {
r := &R{
cfg: cfg,
}
Expand All @@ -103,7 +104,7 @@ func New(cfg config.APIConfig) *R {
r.customResolvers = append(r.customResolvers, livestreamfails.New(cfg)...)
r.customResolvers = append(r.customResolvers, oembed.New(cfg)...)
r.customResolvers = append(r.customResolvers, supinic.New(cfg)...)
r.customResolvers = append(r.customResolvers, twitch.New(cfg)...)
r.customResolvers = append(r.customResolvers, twitch.New(cfg, helixClient)...)
r.customResolvers = append(r.customResolvers, twitter.New(cfg)...)
r.customResolvers = append(r.customResolvers, wikipedia.New(cfg)...)
r.customResolvers = append(r.customResolvers, youtube.New(cfg)...)
Expand All @@ -112,8 +113,8 @@ func New(cfg config.APIConfig) *R {
return r
}

func Initialize(router *chi.Mux, cfg config.APIConfig) {
defaultLinkResolver := New(cfg)
func Initialize(router *chi.Mux, cfg config.APIConfig, helixClient *helix.Client) {
defaultLinkResolver := New(cfg, helixClient)

router.Get("/link_resolver/{url}", defaultLinkResolver.HandleRequest)
router.Get("/thumbnail/{url}", defaultLinkResolver.HandleThumbnailRequest)
Expand Down
28 changes: 4 additions & 24 deletions internal/resolvers/twitch/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,13 @@ var (
helixAPI TwitchAPIClient
)

func New(cfg config.APIConfig) (resolvers []resolver.CustomURLManager) {
if cfg.TwitchClientID == "" {
log.Println("[Config] twitch_client_id is missing, won't do special responses for Twitch clips")
func New(cfg config.APIConfig, helixClient *helix.Client) (resolvers []resolver.CustomURLManager) {
if helixClient == nil {
log.Println("[Config] No Helix Client passed to New - won't do special responses for Twitch clips")
zneix marked this conversation as resolved.
Show resolved Hide resolved
return
}

if cfg.TwitchClientSecret == "" {
log.Println("[Config] twitch_client_secret is missing, won't do special responses for Twitch clips")
return
}

var err error

helixAPI, err = helix.NewClient(&helix.Options{
ClientID: cfg.TwitchClientID,
ClientSecret: cfg.TwitchClientSecret,
})

if err != nil {
log.Fatalf("[Helix] Error initializing API client: %s", err.Error())
}

waitForFirstAppAccessToken := make(chan struct{})

// Initialize methods responsible for refreshing oauth
go initAppAccessToken(helixAPI.(*helix.Client), waitForFirstAppAccessToken)
<-waitForFirstAppAccessToken
helixAPI = helixClient

resolvers = append(resolvers, resolver.CustomURLManager{
Check: check,
Expand Down
Loading