feat: add YouTube engine with config file and env support
YouTube Data API v3 engine: - Add YouTubeConfig to EnginesConfig with api_key field - Add YOUTUBE_API_KEY env override - Thread *config.Config through search service to factory - Factory falls back to env vars if config fields are empty - Update config.example.toml with youtube section Also update default local_ported to include google and youtube.
This commit is contained in:
parent
1689cab9bd
commit
a7f594b7fa
5 changed files with 47 additions and 12 deletions
|
|
@ -4,23 +4,42 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/metamorphosis-dev/kafka/internal/config"
|
||||
)
|
||||
|
||||
// NewDefaultPortedEngines returns the starter set of Go-native engines.
|
||||
// The service can swap/extend this registry later as more engines are ported.
|
||||
func NewDefaultPortedEngines(client *http.Client) map[string]Engine {
|
||||
// If cfg is nil, falls back to reading API keys from environment variables.
|
||||
func NewDefaultPortedEngines(client *http.Client, cfg *config.Config) map[string]Engine {
|
||||
if client == nil {
|
||||
client = &http.Client{Timeout: 10 * time.Second}
|
||||
}
|
||||
|
||||
var braveAPIKey, braveAccessToken, youtubeAPIKey string
|
||||
if cfg != nil {
|
||||
braveAPIKey = cfg.Engines.Brave.APIKey
|
||||
braveAccessToken = cfg.Engines.Brave.AccessToken
|
||||
youtubeAPIKey = cfg.Engines.YouTube.APIKey
|
||||
}
|
||||
if braveAPIKey == "" {
|
||||
braveAPIKey = os.Getenv("BRAVE_API_KEY")
|
||||
}
|
||||
if braveAccessToken == "" {
|
||||
braveAccessToken = os.Getenv("BRAVE_ACCESS_TOKEN")
|
||||
}
|
||||
if youtubeAPIKey == "" {
|
||||
youtubeAPIKey = os.Getenv("YOUTUBE_API_KEY")
|
||||
}
|
||||
|
||||
return map[string]Engine{
|
||||
"wikipedia": &WikipediaEngine{client: client},
|
||||
"arxiv": &ArxivEngine{client: client},
|
||||
"crossref": &CrossrefEngine{client: client},
|
||||
"braveapi": &BraveEngine{
|
||||
client: client,
|
||||
apiKey: os.Getenv("BRAVE_API_KEY"),
|
||||
accessGateToken: os.Getenv("BRAVE_ACCESS_TOKEN"),
|
||||
apiKey: braveAPIKey,
|
||||
accessGateToken: braveAccessToken,
|
||||
resultsPerPage: 20,
|
||||
},
|
||||
"qwant": &QwantEngine{
|
||||
|
|
@ -35,6 +54,7 @@ func NewDefaultPortedEngines(client *http.Client) map[string]Engine {
|
|||
"google": &GoogleEngine{client: client},
|
||||
"youtube": &YouTubeEngine{
|
||||
client: client,
|
||||
apiKey: youtubeAPIKey,
|
||||
baseURL: "https://www.googleapis.com",
|
||||
},
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue