-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fca160e
commit cefb8e3
Showing
27 changed files
with
692 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2024 Outreach Corporation. All Rights Reserved. | ||
// Description: async infra for example application | ||
|
||
// Package async provides async infra for example application | ||
package async | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/getoutreach/plumber" | ||
"github.com/getoutreach/plumber/example/contract" | ||
) | ||
|
||
// Publisher service | ||
type Publisher struct { | ||
*plumber.BaseLooper | ||
broker string | ||
} | ||
|
||
func NewPublisher(broker string) *Publisher { | ||
return &Publisher{ | ||
broker: broker, | ||
BaseLooper: contract.NewWorker("async.Publisher"), | ||
} | ||
} | ||
|
||
func (p *Publisher) Publish(ctx context.Context, e *contract.Entity) error { | ||
fmt.Printf("Publishing entity #%v name=%s\n", e.ID, e.Name) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2024 Outreach Corporation. All Rights Reserved. | ||
// Description: database infra for example application | ||
|
||
// Package database provides database infra for example application | ||
package database | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync/atomic" | ||
|
||
"github.com/getoutreach/plumber" | ||
"github.com/getoutreach/plumber/example/contract" | ||
) | ||
|
||
// EntityMask provides a mask to format an entity | ||
const EntityMask = "Entity %v" | ||
|
||
// Repository represents a plain database repository | ||
type Repository struct { | ||
id int64 | ||
} | ||
|
||
func NewRepository() (*Repository, error) { | ||
return &Repository{}, nil | ||
} | ||
|
||
func (s *Repository) Get(ctx context.Context, id int64) (*contract.Entity, error) { | ||
return &contract.Entity{ | ||
ID: id, | ||
Name: fmt.Sprintf(EntityMask, id), | ||
}, nil | ||
} | ||
|
||
func (s *Repository) Create(ctx context.Context, name string) (*contract.Entity, error) { | ||
nextID := atomic.AddInt64(&s.id, 1) | ||
return &contract.Entity{ | ||
ID: nextID, | ||
Name: fmt.Sprintf(EntityMask, nextID), | ||
}, nil | ||
} | ||
|
||
// BatchingRepository represents a database repository that can batch single entity reads into batch query | ||
type BatchingRepository struct { | ||
*plumber.BaseLooper | ||
inner contract.Repository | ||
} | ||
|
||
func NewBatchingRepository(inner contract.Repository, batchSize int) (*BatchingRepository, error) { | ||
r := &BatchingRepository{ | ||
inner: inner, | ||
BaseLooper: contract.NewWorker("database.BatchingRepository"), | ||
} | ||
return r, nil | ||
} | ||
|
||
func (s *BatchingRepository) Get(ctx context.Context, id int64) (*contract.Entity, error) { | ||
// Here suppose to be a logic that batches requests into a single batch query | ||
return s.inner.Get(ctx, id) | ||
} | ||
|
||
func (s *BatchingRepository) Create(ctx context.Context, name string) (*contract.Entity, error) { | ||
return s.inner.Create(ctx, name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2024 Outreach Corporation. All Rights Reserved. | ||
// Description: graphql infra for example application | ||
|
||
// Package graphql provides graphql infra for example application | ||
package graphql | ||
|
||
import ( | ||
"github.com/getoutreach/plumber" | ||
"github.com/getoutreach/plumber/example/contract" | ||
"github.com/getoutreach/plumber/example/service" | ||
) | ||
|
||
// Server represents a graphql server | ||
type Server struct { | ||
*plumber.BaseLooper | ||
port int32 | ||
querier *service.QueryService | ||
mutator contract.MutatorService | ||
} | ||
|
||
// NewServer returns intance of the *Server | ||
func NewServer( | ||
port int32, | ||
querier *service.QueryService, | ||
mutator contract.MutatorService, | ||
) (*Server, error) { | ||
return &Server{ | ||
port: port, | ||
querier: querier, | ||
mutator: mutator, | ||
BaseLooper: contract.NewWorker("graphql.Server"), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2024 Outreach Corporation. All Rights Reserved. | ||
// Description: grpc infra for example application | ||
|
||
// Package grpc provides grpc infra for example application | ||
package grpc | ||
|
||
import ( | ||
"github.com/getoutreach/plumber" | ||
"github.com/getoutreach/plumber/example/contract" | ||
"github.com/getoutreach/plumber/example/service" | ||
) | ||
|
||
// Server represents a grpc server | ||
type Server struct { | ||
*plumber.BaseLooper | ||
port int32 | ||
querier *service.QueryService | ||
mutator contract.MutatorService | ||
} | ||
|
||
// NewServer returns intance of the *Server | ||
func NewServer( | ||
port int32, | ||
querier *service.QueryService, | ||
mutator contract.MutatorService, | ||
) (*Server, error) { | ||
return &Server{ | ||
port: port, | ||
querier: querier, | ||
mutator: mutator, | ||
BaseLooper: contract.NewWorker("grpc.Server"), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2024 Outreach Corporation. All Rights Reserved. | ||
// Description: application root dependency container | ||
package example | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/getoutreach/plumber" | ||
) | ||
|
||
// Config represents a application configuration structure | ||
type Config struct { | ||
AsyncBroker string | ||
} | ||
|
||
// Definer allows to redefine container on startup | ||
type Definer = func(ctx context.Context, cf *Config, a *Container) | ||
|
||
// Container represents root application dependency container | ||
type Container struct { | ||
plumber.Container | ||
Async *Async | ||
Database *Database | ||
GraphQL *GraphQL | ||
GRPC *GRPC | ||
Service *Service | ||
} | ||
|
||
// NewApplication returns instance of the root dependency container | ||
func NewApplication(ctx context.Context, cf *Config, definers ...Definer) *Container { | ||
a := &Container{ | ||
GRPC: new(GRPC), | ||
Database: new(Database), | ||
GraphQL: new(GraphQL), | ||
Service: new(Service), | ||
Async: new(Async), | ||
} | ||
return plumber.DefineContainers(ctx, cf, definers, a, | ||
a.Async, a.Database, a.GRPC, a.GraphQL, a.Service, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2024 Outreach Corporation. All Rights Reserved. | ||
// Description: async related dependencies | ||
package example | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/getoutreach/plumber" | ||
"github.com/getoutreach/plumber/example/adapter/async" | ||
) | ||
|
||
// Async service represents async processing related dependency container | ||
type Async struct { | ||
Publisher plumber.R[*async.Publisher] | ||
} | ||
|
||
// Define resolves dependencies | ||
func (c *Async) Define(ctx context.Context, cf *Config, a *Container) { | ||
c.Publisher.Resolve(func(rr *plumber.ResolutionR[*async.Publisher]) { | ||
rr.Resolve(async.NewPublisher(cf.AsyncBroker)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2024 Outreach Corporation. All Rights Reserved. | ||
// Description: database related dependencies | ||
package example | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/getoutreach/plumber" | ||
"github.com/getoutreach/plumber/example/adapter/database" | ||
"github.com/getoutreach/plumber/example/contract" | ||
) | ||
|
||
// Database represents database related dependency container | ||
type Database struct { | ||
Repository plumber.D[contract.Repository] | ||
BatchingRepository plumber.R[*database.BatchingRepository] | ||
} | ||
|
||
// Define resolves dependencies | ||
func (c *Database) Define(ctx context.Context, cf *Config, a *Container) { | ||
c.Repository.DefineError(func() (contract.Repository, error) { | ||
return database.NewRepository() | ||
}) | ||
|
||
c.BatchingRepository.Resolve(func(r *plumber.ResolutionR[*database.BatchingRepository]) { | ||
r.Require(&c.Repository).Then(func() { | ||
r.ResolveError(database.NewBatchingRepository(c.Repository.Instance(), 100)) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2024 Outreach Corporation. All Rights Reserved. | ||
// Description: env specific overrides | ||
package example | ||
|
||
import "context" | ||
|
||
// WithTestEnvironment redefines application dependency graph for test environment | ||
func WithTestEnvironment(ctx context.Context, cf *Config, a *Container) { | ||
a.GRPC.Port.Const(1001) | ||
} | ||
|
||
// WithIntegrationEnvironment redefines application graph for integration environment | ||
func WithIntegrationEnvironment(ctx context.Context, cf *Config, a *Container) { | ||
a.GRPC.Port.Const(1000) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2024 Outreach Corporation. All Rights Reserved. | ||
// Description: graphql related dependencies | ||
package example | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/getoutreach/plumber" | ||
"github.com/getoutreach/plumber/example/adapter/graphql" | ||
) | ||
|
||
// GraphQL represents graphql related dependency container | ||
type GraphQL struct { | ||
Port plumber.D[int32] | ||
Server plumber.R[*graphql.Server] | ||
} | ||
|
||
// Define resolves dependencies | ||
func (c *GraphQL) Define(ctx context.Context, cf *Config, a *Container) { | ||
c.Port.Const(5000) | ||
|
||
c.Server.Resolve(func(r *plumber.ResolutionR[*graphql.Server]) { | ||
r.Require( | ||
&a.Service.Querier, | ||
&a.Service.Mutator, | ||
).Then(func() { | ||
r.ResolveError(graphql.NewServer( | ||
c.Port.Instance(), | ||
a.Service.Querier.Instance(), | ||
a.Service.Mutator.Instance(), | ||
)) | ||
}) | ||
}) | ||
} |
Oops, something went wrong.