Skip to content

Commit

Permalink
refactor: Remove hard-coded ports (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmichaelchen authored Jan 6, 2024
1 parent 62de83d commit cdb6515
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
39 changes: 27 additions & 12 deletions cmd/saga/worker/app/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"buf.build/gen/go/kevinmichaelchen/orgapis/connectrpc/go/org/v1beta1/orgv1beta1connect"
"buf.build/gen/go/kevinmichaelchen/profileapis/connectrpc/go/profile/v1beta1/profilev1beta1connect"
"connectrpc.com/connect"
"github.com/sethvargo/go-envconfig"
"github.com/sirupsen/logrus"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/worker"
Expand All @@ -19,15 +20,10 @@ import (
"github.com/kevinmichaelchen/temporal-saga-grpc/pkg/saga"
)

const (
servicePortLicense = 9090
servicePortOrg = 9091
servicePortProfile = 9092
)

// Module - An FX module for a Temporal worker.
var Module = fx.Module("worker",
fx.Provide(
NewConfig,
NewController,
NewWorker,
NewLicenseClient,
Expand All @@ -39,6 +35,25 @@ var Module = fx.Module("worker",
),
)

// Config - Environment-based config.
type Config struct {
PortAPILicense int `env:"PORT_API_LICENSE"`
PortAPIOrg int `env:"PORT_API_ORG"`
PortAPIProfile int `env:"PORT_API_PROFILE"`
}

// NewConfig - Reads config from environment.
func NewConfig() (*Config, error) {
var cfg Config

err := envconfig.Process(context.Background(), &cfg)
if err != nil {
return nil, fmt.Errorf("unable to read config from environment: %w", err)
}

return &cfg, nil
}

// NewController - Returns a new controller for our Temporal workflow.
func NewController(
licenseClient licensev1beta1connect.LicenseServiceClient,
Expand All @@ -49,8 +64,8 @@ func NewController(
}

// NewLicenseClient - Returns a new Connect client for the License service.
func NewLicenseClient() licensev1beta1connect.LicenseServiceClient {
addr := fmt.Sprintf("http://localhost:%d", servicePortLicense)
func NewLicenseClient(cfg *Config) licensev1beta1connect.LicenseServiceClient {
addr := fmt.Sprintf("http://localhost:%d", cfg.PortAPILicense)

return licensev1beta1connect.NewLicenseServiceClient(
http.DefaultClient,
Expand All @@ -60,8 +75,8 @@ func NewLicenseClient() licensev1beta1connect.LicenseServiceClient {
}

// NewOrgClient - Returns a new Connect client for the Org service.
func NewOrgClient() orgv1beta1connect.OrgServiceClient {
addr := fmt.Sprintf("http://localhost:%d", servicePortOrg)
func NewOrgClient(cfg *Config) orgv1beta1connect.OrgServiceClient {
addr := fmt.Sprintf("http://localhost:%d", cfg.PortAPIOrg)

return orgv1beta1connect.NewOrgServiceClient(
http.DefaultClient,
Expand All @@ -71,8 +86,8 @@ func NewOrgClient() orgv1beta1connect.OrgServiceClient {
}

// NewProfileClient - Returns a new Connect client for the Profile service.
func NewProfileClient() profilev1beta1connect.ProfileServiceClient {
addr := fmt.Sprintf("http://localhost:%d", servicePortProfile)
func NewProfileClient(cfg *Config) profilev1beta1connect.ProfileServiceClient {
addr := fmt.Sprintf("http://localhost:%d", cfg.PortAPIProfile)

return profilev1beta1connect.NewProfileServiceClient(
http.DefaultClient,
Expand Down
4 changes: 4 additions & 0 deletions taskfiles/run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ tasks:
- task: infra
desc: "Runs Temporal worker"
cmd: go run cmd/saga/worker/main.go
env:
PORT_API_LICENSE: "{{ .PORT_API_LICENSE }}"
PORT_API_ORG: "{{ .PORT_API_ORG }}"
PORT_API_PROFILE: "{{ .PORT_API_PROFILE }}"

starter:
run: once
Expand Down

0 comments on commit cdb6515

Please sign in to comment.