Skip to content

Commit

Permalink
fix: reorganize service structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse0Michael committed Sep 7, 2023
1 parent 50ba2c6 commit f1f8cc0
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 65 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
.PHONY: build
COVERAGEDIR = .coverage

# Source a local .env
ifneq (,$(wildcard ./.env))
include .env
export
endif

gen:
go generate ./...

Expand Down
31 changes: 7 additions & 24 deletions cmd/fetcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import (
"os/signal"
"syscall"

"github.com/Davincible/goinsta"
"github.com/dghubble/go-twitter/twitter"
"github.com/jesse0michael/fetcher/internal"
"github.com/jesse0michael/fetcher/internal/server"
"github.com/jesse0michael/fetcher/internal/service"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)

type Config struct {
Server server.Config
Service service.Config
}

func main() {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelInfo})))

Expand All @@ -32,30 +32,13 @@ func main() {
}()

_ = godotenv.Load()
var cfg internal.Config
var cfg Config
if err := envconfig.Process("", &cfg); err != nil {
slog.Error("failed to process config")
cancel()
}

// Twitter client
// oauth2 configures a client that uses app credentials to keep a fresh token
config := &clientcredentials.Config{
ClientID: cfg.Twitter.ClientID,
ClientSecret: cfg.Twitter.ClientSecret,
TokenURL: cfg.Twitter.TokenURL,
}
// http.Client will automatically authorize Requests
httpClient := config.Client(oauth2.NoContext)
twitterClient := twitter.NewClient(httpClient)

// Instagram client
insta := goinsta.New(cfg.Instagram.Username, cfg.Instagram.Password)
if err := insta.Login(); err != nil {
slog.With("error", err).Error("failed to log into instagram")
}

fetcher := service.NewFetcher(cfg.Fetcher, twitterClient, insta)
fetcher := service.NewFetcher(cfg.Service)
srvr := server.New(cfg.Server, fetcher)
go func() {
if err := srvr.ListenAndServe(); err != nil {
Expand Down
24 changes: 0 additions & 24 deletions internal/config.go

This file was deleted.

31 changes: 20 additions & 11 deletions internal/service/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import (
"log/slog"
"sort"
"sync"

"github.com/Davincible/goinsta"
"github.com/dghubble/go-twitter/twitter"
)

type Config struct {
Twitter TwitterConfig
Instagram InstagramConfig
Count int `envconfig:"FETCHER_COUNT" default:"50"`
ProxyURL string `envconfig:"FETCHER_PROXY_URL" default:"https://fetcher-ho4joes5va-uw.a.run.app/proxy"`
}

type Feeder interface {
Feed(ctx context.Context, id string) ([]FeedItem, error)
}
Expand All @@ -23,11 +27,6 @@ type FetcherRequest struct {
DeviantArtID string `query:"deviantartID"`
}

type Config struct {
Count int `envconfig:"FETCHER_COUNT" default:"50"`
ProxyURL string `envconfig:"FETCHER_PROXY_URL" default:"https://fetcher-ho4joes5va-uw.a.run.app/proxy"`
}

// Fetcher can retrieve feed items from various sources and compound the results into one feed.
type Fetcher struct {
cfg Config
Expand All @@ -41,12 +40,22 @@ type Fetcher struct {
}

// NewFetcher creates a Fetcher service.
func NewFetcher(cfg Config, twitterClient *twitter.Client, insta *goinsta.Instagram) *Fetcher {
func NewFetcher(cfg Config) *Fetcher {
twitter, err := NewTwitter(cfg.Twitter)
if err != nil {
slog.With("error", err).Error("failed to create twitter feeder")
}

instagram, err := NewInstagram(cfg.Instagram, cfg.ProxyURL)
if err != nil {
slog.With("error", err).Error("failed to create instagram feeder")
}

return &Fetcher{
cfg: cfg,
blogger: NewBlogger(),
twitter: NewTwitter(cfg.Count, twitterClient),
instagram: NewInstagram(cfg.ProxyURL, insta),
twitter: twitter,
instagram: instagram,
soundCloud: NewSoundCloud(),
swarm: NewSwarm(),
deviantArt: NewDeviantArt(),
Expand Down
13 changes: 10 additions & 3 deletions internal/service/instagram.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@ import (
"github.com/Davincible/goinsta"
)

type InstagramConfig struct {
Username string `envconfig:"INSTAGRAM_USERNAME"`
Password string `envconfig:"INSTAGRAM_PASSWORD"`
}

type Instagram struct {
proxyURL string
client *goinsta.Instagram
}

func NewInstagram(proxyURL string, client *goinsta.Instagram) *Instagram {
func NewInstagram(cfg InstagramConfig, proxyURL string) (*Instagram, error) {
insta := goinsta.New(cfg.Username, cfg.Password)
err := insta.Login()
return &Instagram{
proxyURL: proxyURL,
client: client,
}
client: insta,
}, err
}

func (i *Instagram) Feed(_ context.Context, id string) ([]FeedItem, error) {
Expand Down
24 changes: 21 additions & 3 deletions internal/service/twitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,36 @@ import (
"time"

"github.com/dghubble/go-twitter/twitter"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)

type TwitterConfig struct {
Count int `envconfig:"TWITTER_COUNT" default:"20"`
ClientID string `envconfig:"TWITTER_CLIENT_ID"`
ClientSecret string `envconfig:"TWITTER_CLIENT_SECRET"`
TokenURL string `envconfig:"TWITTER_TOKEN_URL" default:"https://api.twitter.com/oauth2/token"`
}

type Twitter struct {
count int
client *twitter.Client
}

func NewTwitter(count int, client *twitter.Client) *Twitter {
func NewTwitter(cfg TwitterConfig) (*Twitter, error) {
// oauth2 configures a client that uses app credentials to keep a fresh token
config := &clientcredentials.Config{
ClientID: cfg.ClientID,
ClientSecret: cfg.ClientSecret,
TokenURL: cfg.TokenURL,
}
// http.Client will automatically authorize Requests
httpClient := config.Client(oauth2.NoContext)
client := twitter.NewClient(httpClient)
return &Twitter{
count: count,
count: cfg.Count,
client: client,
}
}, nil
}

func (t *Twitter) Feed(_ context.Context, id string) ([]FeedItem, error) {
Expand Down

0 comments on commit f1f8cc0

Please sign in to comment.