Skip to content

Commit

Permalink
perf(youtube): Create YouTube client during startup
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jun 24, 2024
1 parent 3060b95 commit b8605fc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
12 changes: 1 addition & 11 deletions internal/youtube/channel/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,11 @@ import (
"github.com/gabe565/transsmute/internal/youtube/middleware"
"github.com/gabe565/transsmute/internal/youtube/playlist"
"github.com/go-chi/chi/v5"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)

func Handler(apiKey string) http.HandlerFunc {
func Handler(service *youtube.Service) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
service, err := youtube.NewService(
r.Context(),
option.WithAPIKey(apiKey),
option.WithTelemetryDisabled(),
)
if err != nil {
panic(err)
}

identifier := chi.URLParam(r, "id")
ch := New(service, identifier)

Expand Down
12 changes: 1 addition & 11 deletions internal/youtube/playlist/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,11 @@ import (
"github.com/gabe565/transsmute/internal/feed"
"github.com/gabe565/transsmute/internal/youtube/middleware"
"github.com/go-chi/chi/v5"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)

func Handler(apiKey string) http.HandlerFunc {
func Handler(service *youtube.Service) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
service, err := youtube.NewService(
r.Context(),
option.WithAPIKey(apiKey),
option.WithTelemetryDisabled(),
)
if err != nil {
panic(err)
}

identifier := chi.URLParam(r, "id")
plist := New(service, identifier)

Expand Down
17 changes: 15 additions & 2 deletions internal/youtube/routes.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
package youtube

import (
"context"

"github.com/gabe565/transsmute/internal/config"
"github.com/gabe565/transsmute/internal/youtube/channel"
"github.com/gabe565/transsmute/internal/youtube/middleware"
"github.com/gabe565/transsmute/internal/youtube/playlist"
"github.com/go-chi/chi/v5"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)

func Routes(r chi.Router, conf config.YouTube) error {
if conf.Enabled {
service, err := youtube.NewService(
context.Background(),
option.WithAPIKey(conf.APIKey),
option.WithTelemetryDisabled(),
)
if err != nil {
return err
}

r.Group(func(r chi.Router) {
r.Use(middleware.NoIframe)
r.Get("/youtube/channel/{id}", channel.Handler(conf.APIKey))
r.Get("/youtube/playlist/{id}", playlist.Handler(conf.APIKey))
r.Get("/youtube/channel/{id}", channel.Handler(service))
r.Get("/youtube/playlist/{id}", playlist.Handler(service))
})
}
return nil
Expand Down

0 comments on commit b8605fc

Please sign in to comment.