Skip to content

Commit

Permalink
Move init to request handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrel-b committed Apr 29, 2024
1 parent 96c2af6 commit 42e1fb8
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 178 deletions.
3 changes: 2 additions & 1 deletion publicapi/publicapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ type PublicAPI struct {
}

func New(ctx context.Context, disableDataloaderCaching bool, repos *postgres.Repositories, queries *db.Queries, httpClient *http.Client, ethClient *ethclient.Client, ipfsClient *shell.Shell,
arweaveClient *goar.Client, storageClient *storage.Client, multichainProvider *multichain.Provider, taskClient *task.Client, throttler *throttle.Locker, secrets *secretmanager.Client, apq *apq.APQCache, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache *redis.Cache, magicClient *magicclient.API, neynar *farcaster.NeynarAPI, mintLimiter *limiters.KeyRateLimiter) *PublicAPI {
arweaveClient *goar.Client, storageClient *storage.Client, taskClient *task.Client, throttler *throttle.Locker, secrets *secretmanager.Client, apq *apq.APQCache, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache *redis.Cache, magicClient *magicclient.API, neynar *farcaster.NeynarAPI, mintLimiter *limiters.KeyRateLimiter) *PublicAPI {
loaders := dataloader.NewLoaders(ctx, queries, disableDataloaderCaching, tracing.DataloaderPreFetchHook, tracing.DataloaderPostFetchHook)
validator := validate.WithCustomValidators()
tokenManager := tokenmanage.New(ctx, taskClient, tokenManageCache, nil)
privyClient := privy.NewPrivyClient(httpClient)
highlightProvider := highlight.NewProvider(httpClient)
multichainProvider := multichain.NewMultichainProvider(ctx, repos, queries)

return &PublicAPI{
repos: repos,
Expand Down
13 changes: 6 additions & 7 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"github.com/mikeydub/go-gallery/service/auth"
"github.com/mikeydub/go-gallery/service/limiters"
"github.com/mikeydub/go-gallery/service/mediamapper"
"github.com/mikeydub/go-gallery/service/multichain"
"github.com/mikeydub/go-gallery/service/notifications"
"github.com/mikeydub/go-gallery/service/persist/postgres"
"github.com/mikeydub/go-gallery/service/recommend"
Expand All @@ -48,24 +47,24 @@ import (
"github.com/mikeydub/go-gallery/util"
)

func handlersInit(router *gin.Engine, repos *postgres.Repositories, queries *db.Queries, httpClient *http.Client, ethClient *ethclient.Client, ipfsClient *shell.Shell, arweaveClient *goar.Client, stg *storage.Client, mcProvider *multichain.Provider, throttler *throttle.Locker, taskClient *task.Client, pub *pubsub.Client, lock *redislock.Client, secrets *secretmanager.Client, graphqlAPQCache, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache *redis.Cache, magicClient *magicclient.API, recommender *recommend.Recommender, p *userpref.Personalization, neynar *farcaster.NeynarAPI, mintLimiter *limiters.KeyRateLimiter) *gin.Engine {
func handlersInit(router *gin.Engine, repos *postgres.Repositories, queries *db.Queries, httpClient *http.Client, ethClient *ethclient.Client, ipfsClient *shell.Shell, arweaveClient *goar.Client, stg *storage.Client, throttler *throttle.Locker, taskClient *task.Client, pub *pubsub.Client, lock *redislock.Client, secrets *secretmanager.Client, graphqlAPQCache, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache *redis.Cache, magicClient *magicclient.API, recommender *recommend.Recommender, p *userpref.Personalization, neynar *farcaster.NeynarAPI, mintLimiter *limiters.KeyRateLimiter) *gin.Engine {

graphqlGroup := router.Group("/glry/graphql")
graphqlHandlersInit(graphqlGroup, repos, queries, httpClient, ethClient, ipfsClient, arweaveClient, stg, mcProvider, throttler, taskClient, pub, lock, secrets, graphqlAPQCache, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache, magicClient, recommender, p, neynar, mintLimiter)
graphqlHandlersInit(graphqlGroup, repos, queries, httpClient, ethClient, ipfsClient, arweaveClient, stg, throttler, taskClient, pub, lock, secrets, graphqlAPQCache, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache, magicClient, recommender, p, neynar, mintLimiter)

router.GET("/alive", util.HealthCheckHandler())

return router
}

func graphqlHandlersInit(parent *gin.RouterGroup, repos *postgres.Repositories, queries *db.Queries, httpClient *http.Client, ethClient *ethclient.Client, ipfsClient *shell.Shell, arweaveClient *goar.Client, storageClient *storage.Client, mcProvider *multichain.Provider, throttler *throttle.Locker, taskClient *task.Client, pub *pubsub.Client, lock *redislock.Client, secrets *secretmanager.Client, graphqlAPQCache, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache *redis.Cache, magicClient *magicclient.API, recommender *recommend.Recommender, p *userpref.Personalization, neynar *farcaster.NeynarAPI, mintLimiter *limiters.KeyRateLimiter) {
handler := graphqlHandler(repos, queries, httpClient, ethClient, ipfsClient, arweaveClient, storageClient, mcProvider, throttler, taskClient, pub, lock, secrets, graphqlAPQCache, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache, magicClient, recommender, p, neynar, mintLimiter)
func graphqlHandlersInit(parent *gin.RouterGroup, repos *postgres.Repositories, queries *db.Queries, httpClient *http.Client, ethClient *ethclient.Client, ipfsClient *shell.Shell, arweaveClient *goar.Client, storageClient *storage.Client, throttler *throttle.Locker, taskClient *task.Client, pub *pubsub.Client, lock *redislock.Client, secrets *secretmanager.Client, graphqlAPQCache, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache *redis.Cache, magicClient *magicclient.API, recommender *recommend.Recommender, p *userpref.Personalization, neynar *farcaster.NeynarAPI, mintLimiter *limiters.KeyRateLimiter) {
handler := graphqlHandler(repos, queries, httpClient, ethClient, ipfsClient, arweaveClient, storageClient, throttler, taskClient, pub, lock, secrets, graphqlAPQCache, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache, magicClient, recommender, p, neynar, mintLimiter)
parent.Any("/query", middleware.ContinueSession(queries, authRefreshCache), handler)
parent.Any("/query/:operationName", middleware.ContinueSession(queries, authRefreshCache), handler)
parent.GET("/playground", graphqlPlaygroundHandler())
}

func graphqlHandler(repos *postgres.Repositories, queries *db.Queries, httpClient *http.Client, ethClient *ethclient.Client, ipfsClient *shell.Shell, arweaveClient *goar.Client, storageClient *storage.Client, mp *multichain.Provider, throttler *throttle.Locker, taskClient *task.Client, pub *pubsub.Client, lock *redislock.Client, secrets *secretmanager.Client, graphqlAPQCache, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache *redis.Cache, magicClient *magicclient.API, recommender *recommend.Recommender, p *userpref.Personalization, neynar *farcaster.NeynarAPI, mintLimiter *limiters.KeyRateLimiter) gin.HandlerFunc {
func graphqlHandler(repos *postgres.Repositories, queries *db.Queries, httpClient *http.Client, ethClient *ethclient.Client, ipfsClient *shell.Shell, arweaveClient *goar.Client, storageClient *storage.Client, throttler *throttle.Locker, taskClient *task.Client, pub *pubsub.Client, lock *redislock.Client, secrets *secretmanager.Client, graphqlAPQCache, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache *redis.Cache, magicClient *magicclient.API, recommender *recommend.Recommender, p *userpref.Personalization, neynar *farcaster.NeynarAPI, mintLimiter *limiters.KeyRateLimiter) gin.HandlerFunc {
config := generated.Config{Resolvers: &graphql.Resolver{}}
config.Directives.AuthRequired = graphql.AuthRequiredDirectiveHandler()
config.Directives.RestrictEnvironment = graphql.RestrictEnvironmentDirectiveHandler()
Expand Down Expand Up @@ -124,7 +123,7 @@ func graphqlHandler(repos *postgres.Repositories, queries *db.Queries, httpClien
h.AroundFields(graphql.RemapAndReportErrors)

newPublicAPI := func(ctx context.Context, disableDataloaderCaching bool) *publicapi.PublicAPI {
return publicapi.New(ctx, disableDataloaderCaching, repos, queries, httpClient, ethClient, ipfsClient, arweaveClient, storageClient, mp, taskClient, throttler, secrets, apqCache, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache, magicClient, neynar, mintLimiter)
return publicapi.New(ctx, disableDataloaderCaching, repos, queries, httpClient, ethClient, ipfsClient, arweaveClient, storageClient, taskClient, throttler, secrets, apqCache, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache, magicClient, neynar, mintLimiter)
}

notificationsHandler := notifications.New(queries, pub, taskClient, lock, true)
Expand Down
8 changes: 3 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/mikeydub/go-gallery/service/farcaster"
"github.com/mikeydub/go-gallery/service/limiters"
"github.com/mikeydub/go-gallery/service/logger"
"github.com/mikeydub/go-gallery/service/multichain"
"github.com/mikeydub/go-gallery/service/persist/postgres"
"github.com/mikeydub/go-gallery/service/pubsub/gcp"
"github.com/mikeydub/go-gallery/service/recommend"
Expand Down Expand Up @@ -59,10 +58,9 @@ func Init() {

ctx := context.Background()
c := ClientInit(ctx)
provider, _ := NewMultichainProvider(ctx, SetDefaults)
recommender := recommend.NewRecommender(c.Queries, publicapi.GetOnboardingUserRecommendationsBootstrap(c.Queries))
p := userpref.NewPersonalization(ctx, c.Queries, c.StorageClient)
router := CoreInit(ctx, c, provider, recommender, p)
router := CoreInit(ctx, c, recommender, p)
http.Handle("/", router)
}

Expand Down Expand Up @@ -109,7 +107,7 @@ func ClientInit(ctx context.Context) *Clients {

// CoreInit initializes core server functionality. This is abstracted
// so the test server can also utilize it
func CoreInit(ctx context.Context, c *Clients, provider *multichain.Provider, recommender *recommend.Recommender, p *userpref.Personalization) *gin.Engine {
func CoreInit(ctx context.Context, c *Clients, recommender *recommend.Recommender, p *userpref.Personalization) *gin.Engine {
logger.For(nil).Info("initializing server...")

if env.GetString("ENV") != "production" {
Expand Down Expand Up @@ -139,7 +137,7 @@ func CoreInit(ctx context.Context, c *Clients, provider *multichain.Provider, re
recommender.Loop(ctx, time.NewTicker(time.Hour))
p.Loop(ctx, time.NewTicker(time.Minute*15))

return handlersInit(router, c.Repos, c.Queries, c.HTTPClient, c.EthClient, c.IPFSClient, c.ArweaveClient, c.StorageClient, provider, newThrottler(), c.TaskClient, c.PubSubClient, lock, c.SecretClient, graphqlAPQCache, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache, c.MagicLinkClient, recommender, p, neynar, mintLimiter)
return handlersInit(router, c.Repos, c.Queries, c.HTTPClient, c.EthClient, c.IPFSClient, c.ArweaveClient, c.StorageClient, newThrottler(), c.TaskClient, c.PubSubClient, lock, c.SecretClient, graphqlAPQCache, feedCache, socialCache, authRefreshCache, tokenManageCache, oneTimeLoginCache, c.MagicLinkClient, recommender, p, neynar, mintLimiter)
}

func newSecretsClient() *secretmanager.Client {
Expand Down
71 changes: 17 additions & 54 deletions service/multichain/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ package multichain

import (
"context"
"database/sql"
"net/http"

"github.com/ethereum/go-ethereum/ethclient"
"github.com/google/wire"
"github.com/jackc/pgx/v4/pgxpool"

db "github.com/mikeydub/go-gallery/db/gen/coredb"
"github.com/mikeydub/go-gallery/service/eth"
Expand All @@ -31,18 +29,10 @@ import (
"github.com/mikeydub/go-gallery/util"
)

// envInit is a type returned after setting up the environment
// Adding envInit as a dependency to a provider will ensure that the environment is set up prior
// to calling the provider
type envInit struct{}

// NewMultichainProvider is a wire injector that sets up a multichain provider instance
func NewMultichainProvider(ctx context.Context, envFunc func()) (*Provider, func()) {
wire.Build(
setEnv,
func NewMultichainProvider(context.Context, *postgres.Repositories, *db.Queries) *Provider {
panic(wire.Build(
wire.Value(http.DefaultClient), // HTTP client shared between providers
postgres.NewRepositories,
dbConnSet,
wire.Struct(new(ChainProvider), "*"),
tokenProcessingSubmitterInjector,
multichainProviderInjector,
Expand All @@ -54,34 +44,7 @@ func NewMultichainProvider(ctx context.Context, envFunc func()) (*Provider, func
baseInjector,
polygonInjector,
arbitrumInjector,
)
return nil, nil
}

// dbConnSet is a wire provider set for initializing a postgres connection
var dbConnSet = wire.NewSet(
newPqClient,
newPgxClient,
newQueries,
)

func setEnv(f func()) envInit {
f()
return envInit{}
}

func newPqClient(e envInit) (*sql.DB, func()) {
pq := postgres.MustCreateClient()
return pq, func() { pq.Close() }
}

func newPgxClient(envInit) (*pgxpool.Pool, func()) {
pgx := postgres.NewPgxClient()
return pgx, func() { pgx.Close() }
}

func newQueries(p *pgxpool.Pool) *db.Queries {
return db.New(p)
))
}

func newTokenManageCache() *redis.Cache {
Expand Down Expand Up @@ -119,7 +82,7 @@ func customMetadataHandlersInjector() *custom.CustomMetadataHandlers {
))
}

func ethInjector(envInit, context.Context, *http.Client) (*EthereumProvider, func()) {
func ethInjector(context.Context, *http.Client) *EthereumProvider {
panic(wire.Build(
rpc.NewEthClient,
wire.Value(persist.ChainETH),
Expand Down Expand Up @@ -161,7 +124,7 @@ func ethSyncPipelineInjector(
httpClient *http.Client,
chain persist.Chain,
simplehashProvider *simplehash.Provider,
) (*wrapper.SyncPipelineWrapper, func()) {
) *wrapper.SyncPipelineWrapper {
panic(wire.Build(
wire.Struct(new(wrapper.SyncPipelineWrapper), "*"),
wire.Bind(new(common.TokenIdentifierOwnerFetcher), util.ToPointer(simplehashProvider)),
Expand All @@ -174,7 +137,7 @@ func ethSyncPipelineInjector(
))
}

func tezosInjector(envInit, *http.Client) *TezosProvider {
func tezosInjector(*http.Client) *TezosProvider {
wire.Build(
tezosProviderInjector,
wire.Value(persist.ChainTezos),
Expand All @@ -201,7 +164,7 @@ func tezosProviderInjector(tezosProvider *tezos.Provider, simplehashProvider *si
))
}

func optimismInjector(context.Context, *http.Client) (*OptimismProvider, func()) {
func optimismInjector(context.Context, *http.Client) *OptimismProvider {
panic(wire.Build(
wire.Value(persist.ChainOptimism),
simplehash.NewProvider,
Expand Down Expand Up @@ -234,7 +197,7 @@ func optimismSyncPipelineInjector(
httpClient *http.Client,
chain persist.Chain,
simplehashProvider *simplehash.Provider,
) (*wrapper.SyncPipelineWrapper, func()) {
) *wrapper.SyncPipelineWrapper {
panic(wire.Build(
wire.Struct(new(wrapper.SyncPipelineWrapper), "*"),
wire.Bind(new(common.TokenIdentifierOwnerFetcher), util.ToPointer(simplehashProvider)),
Expand All @@ -247,7 +210,7 @@ func optimismSyncPipelineInjector(
))
}

func arbitrumInjector(context.Context, *http.Client) (*ArbitrumProvider, func()) {
func arbitrumInjector(context.Context, *http.Client) *ArbitrumProvider {
panic(wire.Build(
wire.Value(persist.ChainArbitrum),
simplehash.NewProvider,
Expand Down Expand Up @@ -280,7 +243,7 @@ func arbitrumSyncPipelineInjector(
httpClient *http.Client,
chain persist.Chain,
simplehashProvider *simplehash.Provider,
) (*wrapper.SyncPipelineWrapper, func()) {
) *wrapper.SyncPipelineWrapper {
panic(wire.Build(
wire.Struct(new(wrapper.SyncPipelineWrapper), "*"),
wire.Bind(new(common.TokenIdentifierOwnerFetcher), util.ToPointer(simplehashProvider)),
Expand All @@ -293,7 +256,7 @@ func arbitrumSyncPipelineInjector(
))
}

func poapInjector(envInit, *http.Client) *PoapProvider {
func poapInjector(*http.Client) *PoapProvider {
panic(wire.Build(
poapProviderInjector,
poap.NewProvider,
Expand All @@ -310,7 +273,7 @@ func poapProviderInjector(poapProvider *poap.Provider) *PoapProvider {
))
}

func zoraInjector(envInit, context.Context, *http.Client) (*ZoraProvider, func()) {
func zoraInjector(context.Context, *http.Client) *ZoraProvider {
panic(wire.Build(
wire.Value(persist.ChainZora),
simplehash.NewProvider,
Expand Down Expand Up @@ -343,7 +306,7 @@ func zoraSyncPipelineInjector(
httpClient *http.Client,
chain persist.Chain,
simplehashProvider *simplehash.Provider,
) (*wrapper.SyncPipelineWrapper, func()) {
) *wrapper.SyncPipelineWrapper {
panic(wire.Build(
wire.Struct(new(wrapper.SyncPipelineWrapper), "*"),
wire.Bind(new(common.TokenIdentifierOwnerFetcher), util.ToPointer(simplehashProvider)),
Expand All @@ -356,7 +319,7 @@ func zoraSyncPipelineInjector(
))
}

func baseInjector(context.Context, *http.Client) (*BaseProvider, func()) {
func baseInjector(context.Context, *http.Client) *BaseProvider {
panic(wire.Build(
wire.Value(persist.ChainBase),
simplehash.NewProvider,
Expand Down Expand Up @@ -389,7 +352,7 @@ func baseSyncPipelineInjector(
httpClient *http.Client,
chain persist.Chain,
simplehashProvider *simplehash.Provider,
) (*wrapper.SyncPipelineWrapper, func()) {
) *wrapper.SyncPipelineWrapper {
panic(wire.Build(
wire.Struct(new(wrapper.SyncPipelineWrapper), "*"),
wire.Bind(new(common.TokenIdentifierOwnerFetcher), util.ToPointer(simplehashProvider)),
Expand All @@ -402,7 +365,7 @@ func baseSyncPipelineInjector(
))
}

func polygonInjector(context.Context, *http.Client) (*PolygonProvider, func()) {
func polygonInjector(context.Context, *http.Client) *PolygonProvider {
panic(wire.Build(
wire.Value(persist.ChainPolygon),
simplehash.NewProvider,
Expand Down Expand Up @@ -435,7 +398,7 @@ func polygonSyncPipelineInjector(
httpClient *http.Client,
chain persist.Chain,
simplehashProvider *simplehash.Provider,
) (*wrapper.SyncPipelineWrapper, func()) {
) *wrapper.SyncPipelineWrapper {
panic(wire.Build(
wire.Struct(new(wrapper.SyncPipelineWrapper), "*"),
wire.Bind(new(common.TokenIdentifierOwnerFetcher), util.ToPointer(simplehashProvider)),
Expand Down
Loading

0 comments on commit 42e1fb8

Please sign in to comment.