From 42e1fb86d5e9857a6de00e8d282654fdb510b661 Mon Sep 17 00:00:00 2001 From: jarrel Date: Mon, 29 Apr 2024 16:17:22 -0700 Subject: [PATCH] Move init to request handler --- publicapi/publicapi.go | 3 +- server/handler.go | 13 ++- server/server.go | 8 +- service/multichain/inject.go | 71 ++++--------- service/multichain/wire_gen.go | 154 +++++++++-------------------- tokenprocessing/tokenprocessing.go | 2 +- 6 files changed, 73 insertions(+), 178 deletions(-) diff --git a/publicapi/publicapi.go b/publicapi/publicapi.go index 7254d11b6..c4a5a3d42 100644 --- a/publicapi/publicapi.go +++ b/publicapi/publicapi.go @@ -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, diff --git a/server/handler.go b/server/handler.go index d02e6588b..7eca1456b 100644 --- a/server/handler.go +++ b/server/handler.go @@ -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" @@ -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() @@ -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) diff --git a/server/server.go b/server/server.go index 8eae5081f..a5467018e 100644 --- a/server/server.go +++ b/server/server.go @@ -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" @@ -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) } @@ -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" { @@ -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 { diff --git a/service/multichain/inject.go b/service/multichain/inject.go index 2e4cb61c6..f8db5f97d 100644 --- a/service/multichain/inject.go +++ b/service/multichain/inject.go @@ -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" @@ -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, @@ -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 { @@ -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), @@ -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)), @@ -174,7 +137,7 @@ func ethSyncPipelineInjector( )) } -func tezosInjector(envInit, *http.Client) *TezosProvider { +func tezosInjector(*http.Client) *TezosProvider { wire.Build( tezosProviderInjector, wire.Value(persist.ChainTezos), @@ -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, @@ -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)), @@ -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, @@ -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)), @@ -293,7 +256,7 @@ func arbitrumSyncPipelineInjector( )) } -func poapInjector(envInit, *http.Client) *PoapProvider { +func poapInjector(*http.Client) *PoapProvider { panic(wire.Build( poapProviderInjector, poap.NewProvider, @@ -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, @@ -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)), @@ -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, @@ -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)), @@ -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, @@ -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)), diff --git a/service/multichain/wire_gen.go b/service/multichain/wire_gen.go index a57bd3dca..2f1394b8e 100644 --- a/service/multichain/wire_gen.go +++ b/service/multichain/wire_gen.go @@ -8,10 +8,7 @@ package multichain import ( "context" - "database/sql" "github.com/ethereum/go-ethereum/ethclient" - "github.com/google/wire" - "github.com/jackc/pgx/v4/pgxpool" "github.com/mikeydub/go-gallery/db/gen/coredb" "github.com/mikeydub/go-gallery/service/eth" "github.com/mikeydub/go-gallery/service/multichain/custom" @@ -33,21 +30,16 @@ import ( // Injectors from inject.go: // NewMultichainProvider is a wire injector that sets up a multichain provider instance -func NewMultichainProvider(ctx context.Context, envFunc func()) (*Provider, func()) { - multichainEnvInit := setEnv(envFunc) - db, cleanup := newPqClient(multichainEnvInit) - pool, cleanup2 := newPgxClient(multichainEnvInit) - repositories := postgres.NewRepositories(db, pool) - queries := newQueries(pool) +func NewMultichainProvider(contextContext context.Context, repositories *postgres.Repositories, queries *coredb.Queries) *Provider { client := _wireClientValue - ethereumProvider, cleanup3 := ethInjector(multichainEnvInit, ctx, client) - tezosProvider := tezosInjector(multichainEnvInit, client) - optimismProvider, cleanup4 := optimismInjector(ctx, client) - arbitrumProvider, cleanup5 := arbitrumInjector(ctx, client) - poapProvider := poapInjector(multichainEnvInit, client) - zoraProvider, cleanup6 := zoraInjector(multichainEnvInit, ctx, client) - baseProvider, cleanup7 := baseInjector(ctx, client) - polygonProvider, cleanup8 := polygonInjector(ctx, client) + ethereumProvider := ethInjector(contextContext, client) + tezosProvider := tezosInjector(client) + optimismProvider := optimismInjector(contextContext, client) + arbitrumProvider := arbitrumInjector(contextContext, client) + poapProvider := poapInjector(client) + zoraProvider := zoraInjector(contextContext, client) + baseProvider := baseInjector(contextContext, client) + polygonProvider := polygonInjector(contextContext, client) chainProvider := &ChainProvider{ Ethereum: ethereumProvider, Tezos: tezosProvider, @@ -58,18 +50,9 @@ func NewMultichainProvider(ctx context.Context, envFunc func()) (*Provider, func Base: baseProvider, Polygon: polygonProvider, } - tokenProcessingSubmitter := tokenProcessingSubmitterInjector(ctx) - provider := multichainProviderInjector(ctx, repositories, queries, chainProvider, tokenProcessingSubmitter) - return provider, func() { - cleanup8() - cleanup7() - cleanup6() - cleanup5() - cleanup4() - cleanup3() - cleanup2() - cleanup() - } + tokenProcessingSubmitter := tokenProcessingSubmitterInjector(contextContext) + provider := multichainProviderInjector(contextContext, repositories, queries, chainProvider, tokenProcessingSubmitter) + return provider } var ( @@ -95,16 +78,14 @@ func customMetadataHandlersInjector() *custom.CustomMetadataHandlers { return customMetadataHandlers } -func ethInjector(multichainEnvInit envInit, contextContext context.Context, client *http.Client) (*EthereumProvider, func()) { +func ethInjector(contextContext context.Context, client *http.Client) *EthereumProvider { chain := _wireChainValue provider := simplehash.NewProvider(chain, client) - syncPipelineWrapper, cleanup := ethSyncPipelineInjector(contextContext, client, chain, provider) + syncPipelineWrapper := ethSyncPipelineInjector(contextContext, client, chain, provider) ethclientClient := rpc.NewEthClient() verifier := ethVerifierInjector(ethclientClient) ethereumProvider := ethProviderInjector(contextContext, syncPipelineWrapper, verifier, provider) - return ethereumProvider, func() { - cleanup() - } + return ethereumProvider } var ( @@ -135,7 +116,7 @@ func ethProviderInjector(ctx context.Context, syncPipeline *wrapper.SyncPipeline return ethereumProvider } -func ethSyncPipelineInjector(ctx context.Context, httpClient *http.Client, chain persist.Chain, simplehashProvider *simplehash.Provider) (*wrapper.SyncPipelineWrapper, func()) { +func ethSyncPipelineInjector(ctx context.Context, httpClient *http.Client, chain persist.Chain, simplehashProvider *simplehash.Provider) *wrapper.SyncPipelineWrapper { customMetadataHandlers := customMetadataHandlersInjector() syncPipelineWrapper := &wrapper.SyncPipelineWrapper{ Chain: chain, @@ -147,11 +128,10 @@ func ethSyncPipelineInjector(ctx context.Context, httpClient *http.Client, chain TokensByContractWalletFetcher: simplehashProvider, CustomMetadataWrapper: customMetadataHandlers, } - return syncPipelineWrapper, func() { - } + return syncPipelineWrapper } -func tezosInjector(multichainEnvInit envInit, client *http.Client) *TezosProvider { +func tezosInjector(client *http.Client) *TezosProvider { provider := tezos.NewProvider() chain := _wirePersistChainValue simplehashProvider := simplehash.NewProvider(chain, client) @@ -180,14 +160,12 @@ func tezosProviderInjector(tezosProvider *tezos.Provider, simplehashProvider *si return multichainTezosProvider } -func optimismInjector(contextContext context.Context, client *http.Client) (*OptimismProvider, func()) { +func optimismInjector(contextContext context.Context, client *http.Client) *OptimismProvider { chain := _wireChainValue2 provider := simplehash.NewProvider(chain, client) - syncPipelineWrapper, cleanup := optimismSyncPipelineInjector(contextContext, client, chain, provider) + syncPipelineWrapper := optimismSyncPipelineInjector(contextContext, client, chain, provider) optimismProvider := optimismProviderInjector(syncPipelineWrapper, provider) - return optimismProvider, func() { - cleanup() - } + return optimismProvider } var ( @@ -210,7 +188,7 @@ func optimismProviderInjector(syncPipeline *wrapper.SyncPipelineWrapper, simpleh return optimismProvider } -func optimismSyncPipelineInjector(ctx context.Context, httpClient *http.Client, chain persist.Chain, simplehashProvider *simplehash.Provider) (*wrapper.SyncPipelineWrapper, func()) { +func optimismSyncPipelineInjector(ctx context.Context, httpClient *http.Client, chain persist.Chain, simplehashProvider *simplehash.Provider) *wrapper.SyncPipelineWrapper { customMetadataHandlers := customMetadataHandlersInjector() syncPipelineWrapper := &wrapper.SyncPipelineWrapper{ Chain: chain, @@ -222,18 +200,15 @@ func optimismSyncPipelineInjector(ctx context.Context, httpClient *http.Client, TokensByContractWalletFetcher: simplehashProvider, CustomMetadataWrapper: customMetadataHandlers, } - return syncPipelineWrapper, func() { - } + return syncPipelineWrapper } -func arbitrumInjector(contextContext context.Context, client *http.Client) (*ArbitrumProvider, func()) { +func arbitrumInjector(contextContext context.Context, client *http.Client) *ArbitrumProvider { chain := _wireChainValue3 provider := simplehash.NewProvider(chain, client) - syncPipelineWrapper, cleanup := arbitrumSyncPipelineInjector(contextContext, client, chain, provider) + syncPipelineWrapper := arbitrumSyncPipelineInjector(contextContext, client, chain, provider) arbitrumProvider := arbitrumProviderInjector(syncPipelineWrapper, provider) - return arbitrumProvider, func() { - cleanup() - } + return arbitrumProvider } var ( @@ -256,7 +231,7 @@ func arbitrumProviderInjector(syncPipeline *wrapper.SyncPipelineWrapper, simpleh return arbitrumProvider } -func arbitrumSyncPipelineInjector(ctx context.Context, httpClient *http.Client, chain persist.Chain, simplehashProvider *simplehash.Provider) (*wrapper.SyncPipelineWrapper, func()) { +func arbitrumSyncPipelineInjector(ctx context.Context, httpClient *http.Client, chain persist.Chain, simplehashProvider *simplehash.Provider) *wrapper.SyncPipelineWrapper { customMetadataHandlers := customMetadataHandlersInjector() syncPipelineWrapper := &wrapper.SyncPipelineWrapper{ Chain: chain, @@ -268,11 +243,10 @@ func arbitrumSyncPipelineInjector(ctx context.Context, httpClient *http.Client, TokensByContractWalletFetcher: simplehashProvider, CustomMetadataWrapper: customMetadataHandlers, } - return syncPipelineWrapper, func() { - } + return syncPipelineWrapper } -func poapInjector(multichainEnvInit envInit, client *http.Client) *PoapProvider { +func poapInjector(client *http.Client) *PoapProvider { provider := poap.NewProvider(client) poapProvider := poapProviderInjector(provider) return poapProvider @@ -288,14 +262,12 @@ func poapProviderInjector(poapProvider *poap.Provider) *PoapProvider { return multichainPoapProvider } -func zoraInjector(multichainEnvInit envInit, contextContext context.Context, client *http.Client) (*ZoraProvider, func()) { +func zoraInjector(contextContext context.Context, client *http.Client) *ZoraProvider { chain := _wireChainValue4 provider := simplehash.NewProvider(chain, client) - syncPipelineWrapper, cleanup := zoraSyncPipelineInjector(contextContext, client, chain, provider) + syncPipelineWrapper := zoraSyncPipelineInjector(contextContext, client, chain, provider) zoraProvider := zoraProviderInjector(syncPipelineWrapper, provider) - return zoraProvider, func() { - cleanup() - } + return zoraProvider } var ( @@ -318,7 +290,7 @@ func zoraProviderInjector(syncPipeline *wrapper.SyncPipelineWrapper, simplehashP return zoraProvider } -func zoraSyncPipelineInjector(ctx context.Context, httpClient *http.Client, chain persist.Chain, simplehashProvider *simplehash.Provider) (*wrapper.SyncPipelineWrapper, func()) { +func zoraSyncPipelineInjector(ctx context.Context, httpClient *http.Client, chain persist.Chain, simplehashProvider *simplehash.Provider) *wrapper.SyncPipelineWrapper { customMetadataHandlers := customMetadataHandlersInjector() syncPipelineWrapper := &wrapper.SyncPipelineWrapper{ Chain: chain, @@ -330,18 +302,15 @@ func zoraSyncPipelineInjector(ctx context.Context, httpClient *http.Client, chai TokensByContractWalletFetcher: simplehashProvider, CustomMetadataWrapper: customMetadataHandlers, } - return syncPipelineWrapper, func() { - } + return syncPipelineWrapper } -func baseInjector(contextContext context.Context, client *http.Client) (*BaseProvider, func()) { +func baseInjector(contextContext context.Context, client *http.Client) *BaseProvider { chain := _wireChainValue5 provider := simplehash.NewProvider(chain, client) - syncPipelineWrapper, cleanup := baseSyncPipelineInjector(contextContext, client, chain, provider) + syncPipelineWrapper := baseSyncPipelineInjector(contextContext, client, chain, provider) baseProvider := baseProvidersInjector(syncPipelineWrapper, provider) - return baseProvider, func() { - cleanup() - } + return baseProvider } var ( @@ -364,7 +333,7 @@ func baseProvidersInjector(syncPipeline *wrapper.SyncPipelineWrapper, simplehash return baseProvider } -func baseSyncPipelineInjector(ctx context.Context, httpClient *http.Client, chain persist.Chain, simplehashProvider *simplehash.Provider) (*wrapper.SyncPipelineWrapper, func()) { +func baseSyncPipelineInjector(ctx context.Context, httpClient *http.Client, chain persist.Chain, simplehashProvider *simplehash.Provider) *wrapper.SyncPipelineWrapper { customMetadataHandlers := customMetadataHandlersInjector() syncPipelineWrapper := &wrapper.SyncPipelineWrapper{ Chain: chain, @@ -376,18 +345,15 @@ func baseSyncPipelineInjector(ctx context.Context, httpClient *http.Client, chai TokensByContractWalletFetcher: simplehashProvider, CustomMetadataWrapper: customMetadataHandlers, } - return syncPipelineWrapper, func() { - } + return syncPipelineWrapper } -func polygonInjector(contextContext context.Context, client *http.Client) (*PolygonProvider, func()) { +func polygonInjector(contextContext context.Context, client *http.Client) *PolygonProvider { chain := _wireChainValue6 provider := simplehash.NewProvider(chain, client) - syncPipelineWrapper, cleanup := polygonSyncPipelineInjector(contextContext, client, chain, provider) + syncPipelineWrapper := polygonSyncPipelineInjector(contextContext, client, chain, provider) polygonProvider := polygonProvidersInjector(syncPipelineWrapper, provider) - return polygonProvider, func() { - cleanup() - } + return polygonProvider } var ( @@ -410,7 +376,7 @@ func polygonProvidersInjector(syncPipeline *wrapper.SyncPipelineWrapper, simpleh return polygonProvider } -func polygonSyncPipelineInjector(ctx context.Context, httpClient *http.Client, chain persist.Chain, simplehashProvider *simplehash.Provider) (*wrapper.SyncPipelineWrapper, func()) { +func polygonSyncPipelineInjector(ctx context.Context, httpClient *http.Client, chain persist.Chain, simplehashProvider *simplehash.Provider) *wrapper.SyncPipelineWrapper { customMetadataHandlers := customMetadataHandlersInjector() syncPipelineWrapper := &wrapper.SyncPipelineWrapper{ Chain: chain, @@ -422,8 +388,7 @@ func polygonSyncPipelineInjector(ctx context.Context, httpClient *http.Client, c TokensByContractWalletFetcher: simplehashProvider, CustomMetadataWrapper: customMetadataHandlers, } - return syncPipelineWrapper, func() { - } + return syncPipelineWrapper } func tokenProcessingSubmitterInjector(contextContext context.Context) *tokenmanage.TokenProcessingSubmitter { @@ -441,37 +406,6 @@ func tokenProcessingSubmitterInjector(contextContext context.Context) *tokenmana // inject.go: -// 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{} - -// 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) *coredb.Queries { - return coredb.New(p) -} - func newTokenManageCache() *redis.Cache { return redis.NewCache(redis.TokenManageCache) } diff --git a/tokenprocessing/tokenprocessing.go b/tokenprocessing/tokenprocessing.go index 77a71aecb..060c6babb 100644 --- a/tokenprocessing/tokenprocessing.go +++ b/tokenprocessing/tokenprocessing.go @@ -37,7 +37,7 @@ func InitServer() { setDefaults() ctx := context.Background() c := server.ClientInit(ctx) - mc, _ := server.NewMultichainProvider(ctx, setDefaults) + mc := multichain.NewMultichainProvider(ctx, c.Repos, c.Queries) router := CoreInitServer(ctx, c, mc) logger.For(nil).Info("Starting tokenprocessing server...") http.Handle("/", router)