♻️ Replace RedirectURL global with context-based injection

This commit is contained in:
OpenWaygate committed 2026-06-16 23:27:33 +08:00
1 parent a09d6b211c
commit 85ce5706ab
3 files changed
+30 -14

No files matched your search

+2 -1
View File
@@ -65,7 +65,8 @@ func init() {
Scopes: auth.Scopes,
}
} else {
common.RedirectURL = fmt.Sprintf("http://localhost:%d", port)
redirectURL := fmt.Sprintf("http://localhost:%d", port)
cmd.SetContext(common.CtxWithRedirectURL(cmd.Context(), redirectURL))
}
return nil
+4 -1
View File
@@ -7,6 +7,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
@@ -31,7 +32,9 @@ func NewGoogleTokenVerifier(tokenInfoURL string) sdkauth.TokenVerifier {
if err != nil {
return nil, fmt.Errorf("%w: %w", sdkauth.ErrInvalidToken, err)
}
defer resp.Body.Close()
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf(
+24 -12
View File
@@ -21,19 +21,23 @@ import (
"google.golang.org/api/youtube/v3"
)
// RedirectURL is the OAuth redirect URL used by file-based auth (CLI / stdio mode).
// Set by the command layer before EnsureService is called.
var RedirectURL = "http://localhost:8216"
type redirectURLKey struct{}
// CtxWithRedirectURL returns a child context carrying the OAuth redirect URL.
func CtxWithRedirectURL(ctx context.Context, url string) context.Context {
return context.WithValue(ctx, redirectURLKey{}, url)
}
type Fields struct {
Ctx context.Context `yaml:"-" json:"-"`
Service *youtube.Service `yaml:"-" json:"-"`
Ids []string `yaml:"ids" json:"ids,omitempty"`
MaxResults int64 `yaml:"max_results" json:"max_results,omitempty"`
Hl string `yaml:"hl" json:"hl,omitempty"`
ChannelId string `yaml:"channel_id" json:"channel_id,omitempty"`
Parts []string `yaml:"parts" json:"parts,omitempty"`
Output string `yaml:"output" json:"output,omitempty"`
Ctx context.Context `yaml:"-" json:"-"`
Service *youtube.Service `yaml:"-" json:"-"`
RedirectURL string `yaml:"-" json:"-"`
Ids []string `yaml:"ids" json:"ids,omitempty"`
MaxResults int64 `yaml:"max_results" json:"max_results,omitempty"`
Hl string `yaml:"hl" json:"hl,omitempty"`
ChannelId string `yaml:"channel_id" json:"channel_id,omitempty"`
Parts []string `yaml:"parts" json:"parts,omitempty"`
Output string `yaml:"output" json:"output,omitempty"`
OnBehalfOfContentOwner string `yaml:"on_behalf_of_content_owner" json:"on_behalf_of_content_owner,omitempty"`
}
@@ -47,6 +51,11 @@ func (d *Fields) GetFields() *Fields {
func (d *Fields) SetContext(ctx context.Context) {
if d != nil {
d.Ctx = ctx
if d.RedirectURL == "" {
if url, ok := ctx.Value(redirectURLKey{}).(string); ok {
d.RedirectURL = url
}
}
}
}
@@ -72,10 +81,13 @@ func (d *Fields) EnsureService() error {
}
// File-based auth path (CLI / stdio mode).
if d.RedirectURL == "" {
d.RedirectURL = "http://localhost:8216"
}
svc, err := auth.NewY2BService(
auth.WithCredential("", pkg.Root.FS()),
auth.WithCacheToken("", pkg.Root.FS()),
auth.WithRedirectURL(RedirectURL),
auth.WithRedirectURL(d.RedirectURL),
).GetService()
if err != nil {
return fmt.Errorf("failed to create YouTube service: %w", err)