Skip to content

Commit

Permalink
New Project Structure (#1560)
Browse files Browse the repository at this point in the history
* moving all tools (settlement, command line tools) to ./tools, moving middleware to utils, create go.mod for utils/tools/base

* moving services into services dir

* fixing up the dep tree

* go mod for services

* temp add tools to bat-go root cmd

* fixing tools/services to be built into same binary for the moment

* fixing make test run with go workspace

* ratios test failure from reorg

* update for tests based on new org

* goodbye utils, hello libs

* fix module utils->libs in mocks

* fixing more tests
  • Loading branch information
husobee authored Aug 22, 2022
1 parent cf9671d commit d134260
Show file tree
Hide file tree
Showing 336 changed files with 8,437 additions and 1,265 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ ARG BUILD_TIME
ARG COMMIT

WORKDIR /src/
COPY go.mod go.sum ./
RUN go mod download
COPY . ./
RUN go mod download

RUN CGO_ENABLED=0 GOOS=linux go build \
-ldflags "-w -s -X main.version=${VERSION} -X main.buildTime=${BUILD_TIME} -X main.commit=${COMMIT}" \
Expand Down
84 changes: 45 additions & 39 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,55 @@ buildcmd:
CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build -v -ldflags "-w -s -X main.version=${GIT_VERSION} -X main.buildTime=${BUILD_TIME} -X main.commit=${GIT_COMMIT}" -o bat-go main.go

mock:
mockgen -source=./promotion/claim.go -destination=promotion/mockclaim.go -package=promotion
mockgen -source=./promotion/drain.go -destination=promotion/mockdrain.go -package=promotion
mockgen -source=./promotion/datastore.go -destination=promotion/mockdatastore.go -package=promotion
mockgen -source=./promotion/service.go -destination=promotion/mockservice.go -package=promotion
mockgen -source=./grant/datastore.go -destination=grant/mockdatastore.go -package=grant
mockgen -source=./utils/clients/ratios/client.go -destination=utils/clients/ratios/mock/mock.go -package=mock_ratios
mockgen -source=./utils/clients/cbr/client.go -destination=utils/clients/cbr/mock/mock.go -package=mock_cbr
mockgen -source=./utils/clients/reputation/client.go -destination=utils/clients/reputation/mock/mock.go -package=mock_reputation
mockgen -source=./utils/clients/gemini/client.go -destination=utils/clients/gemini/mock/mock.go -package=mock_gemini
mockgen -source=./utils/clients/bitflyer/client.go -destination=utils/clients/bitflyer/mock/mock.go -package=mock_bitflyer
mockgen -source=./utils/clients/coingecko/client.go -destination=utils/clients/coingecko/mock/mock.go -package=mock_coingecko
mockgen -source=./utils/backoff/retrypolicy/retrypolicy.go -destination=utils/backoff/retrypolicy/mock/retrypolicy.go -package=mockretrypolicy
mockgen -source=./services/promotion/claim.go -destination=services/promotion/mockclaim.go -package=promotion
mockgen -source=./services/promotion/drain.go -destination=services/promotion/mockdrain.go -package=promotion
mockgen -source=./services/promotion/datastore.go -destination=services/promotion/mockdatastore.go -package=promotion
mockgen -source=./services/promotion/service.go -destination=services/promotion/mockservice.go -package=promotion
mockgen -source=./services/grant/datastore.go -destination=services/grant/mockdatastore.go -package=grant
mockgen -source=./libs/clients/ratios/client.go -destination=libs/clients/ratios/mock/mock.go -package=mock_ratios
mockgen -source=./libs/clients/cbr/client.go -destination=libs/clients/cbr/mock/mock.go -package=mock_cbr
mockgen -source=./libs/clients/reputation/client.go -destination=libs/clients/reputation/mock/mock.go -package=mock_reputation
mockgen -source=./libs/clients/gemini/client.go -destination=libs/clients/gemini/mock/mock.go -package=mock_gemini
mockgen -source=./libs/clients/bitflyer/client.go -destination=libs/clients/bitflyer/mock/mock.go -package=mock_bitflyer
mockgen -source=./libs/clients/coingecko/client.go -destination=libs/clients/coingecko/mock/mock.go -package=mock_coingecko
mockgen -source=./libs/backoff/retrypolicy/retrypolicy.go -destination=libs/backoff/retrypolicy/mock/retrypolicy.go -package=mockretrypolicy

instrumented:
gowrap gen -p github.com/brave-intl/bat-go/grant -i Datastore -t ./.prom-gowrap.tmpl -o ./grant/instrumented_datastore.go
gowrap gen -p github.com/brave-intl/bat-go/grant -i ReadOnlyDatastore -t ./.prom-gowrap.tmpl -o ./grant/instrumented_read_only_datastore.go
gowrap gen -p github.com/brave-intl/bat-go/promotion -i Datastore -t ./.prom-gowrap.tmpl -o ./promotion/instrumented_datastore.go
gowrap gen -p github.com/brave-intl/bat-go/promotion -i ReadOnlyDatastore -t ./.prom-gowrap.tmpl -o ./promotion/instrumented_read_only_datastore.go
gowrap gen -p github.com/brave-intl/bat-go/skus -i Datastore -t ./.prom-gowrap.tmpl -o ./skus/instrumented_datastore.go
gowrap gen -p github.com/brave-intl/bat-go/wallet -i Datastore -t ./.prom-gowrap.tmpl -o ./wallet/instrumented_datastore.go
gowrap gen -p github.com/brave-intl/bat-go/wallet -i ReadOnlyDatastore -t ./.prom-gowrap.tmpl -o ./wallet/instrumented_read_only_datastore.go
gowrap gen -p github.com/brave-intl/bat-go/services/grant -i Datastore -t ./.prom-gowrap.tmpl -o ./services/grant/instrumented_datastore.go
gowrap gen -p github.com/brave-intl/bat-go/services/grant -i ReadOnlyDatastore -t ./.prom-gowrap.tmpl -o ./services/grant/instrumented_read_only_datastore.go
gowrap gen -p github.com/brave-intl/bat-go/services/promotion -i Datastore -t ./.prom-gowrap.tmpl -o ./services/promotion/instrumented_datastore.go
gowrap gen -p github.com/brave-intl/bat-go/services/promotion -i ReadOnlyDatastore -t ./.prom-gowrap.tmpl -o ./services/promotion/instrumented_read_only_datastore.go
gowrap gen -p github.com/brave-intl/bat-go/services/skus -i Datastore -t ./.prom-gowrap.tmpl -o ./services/skus/instrumented_datastore.go
gowrap gen -p github.com/brave-intl/bat-go/services/wallet -i Datastore -t ./.prom-gowrap.tmpl -o ./services/wallet/instrumented_datastore.go
gowrap gen -p github.com/brave-intl/bat-go/services/wallet -i ReadOnlyDatastore -t ./.prom-gowrap.tmpl -o ./services/wallet/instrumented_read_only_datastore.go
# fix everything called datastore...
sed -i'bak' 's/datastore_duration_seconds/grant_datastore_duration_seconds/g' grant/instrumented_datastore.go
sed -i'bak' 's/readonlydatastore_duration_seconds/grant_readonly_datastore_duration_seconds/g' ./grant/instrumented_read_only_datastore.go
sed -i'bak' 's/datastore_duration_seconds/promotion_datastore_duration_seconds/g' ./promotion/instrumented_datastore.go
sed -i'bak' 's/readonlydatastore_duration_seconds/promotion_readonly_datastore_duration_seconds/g' ./promotion/instrumented_read_only_datastore.go
sed -i'bak' 's/datastore_duration_seconds/skus_datastore_duration_seconds/g' ./skus/instrumented_datastore.go
sed -i'bak' 's/datastore_duration_seconds/wallet_datastore_duration_seconds/g' ./wallet/instrumented_datastore.go
sed -i'bak' 's/readonlydatastore_duration_seconds/wallet_readonly_datastore_duration_seconds/g' ./wallet/instrumented_read_only_datastore.go
sed -i'bak' 's/datastore_duration_seconds/grant_datastore_duration_seconds/g' services/grant/instrumented_datastore.go
sed -i'bak' 's/readonlydatastore_duration_seconds/grant_readonly_datastore_duration_seconds/g' ./services/grant/instrumented_read_only_datastore.go
sed -i'bak' 's/datastore_duration_seconds/promotion_datastore_duration_seconds/g' ./services/promotion/instrumented_datastore.go
sed -i'bak' 's/readonlydatastore_duration_seconds/promotion_readonly_datastore_duration_seconds/g' ./services/promotion/instrumented_read_only_datastore.go
sed -i'bak' 's/datastore_duration_seconds/skus_datastore_duration_seconds/g' ./services/skus/instrumented_datastore.go
sed -i'bak' 's/datastore_duration_seconds/wallet_datastore_duration_seconds/g' ./services/wallet/instrumented_datastore.go
sed -i'bak' 's/readonlydatastore_duration_seconds/wallet_readonly_datastore_duration_seconds/g' ./services/wallet/instrumented_read_only_datastore.go
# http clients
gowrap gen -p github.com/brave-intl/bat-go/utils/clients/cbr -i Client -t ./.prom-gowrap.tmpl -o ./utils/clients/cbr/instrumented_client.go
gowrap gen -p github.com/brave-intl/bat-go/utils/clients/ratios -i Client -t ./.prom-gowrap.tmpl -o ./utils/clients/ratios/instrumented_client.go
gowrap gen -p github.com/brave-intl/bat-go/utils/clients/reputation -i Client -t ./.prom-gowrap.tmpl -o ./utils/clients/reputation/instrumented_client.go
gowrap gen -p github.com/brave-intl/bat-go/utils/clients/gemini -i Client -t ./.prom-gowrap.tmpl -o ./utils/clients/gemini/instrumented_client.go
gowrap gen -p github.com/brave-intl/bat-go/utils/clients/bitflyer -i Client -t ./.prom-gowrap.tmpl -o ./utils/clients/bitflyer/instrumented_client.go
gowrap gen -p github.com/brave-intl/bat-go/utils/clients/coingecko -i Client -t ./.prom-gowrap.tmpl -o ./utils/clients/coingecko/instrumented_client.go
gowrap gen -p github.com/brave-intl/bat-go/libs/clients/cbr -i Client -t ./.prom-gowrap.tmpl -o ./libs/clients/cbr/instrumented_client.go
sed -i'bak' 's/cbr.//g' libs/clients/cbr/instrumented_client.go
gowrap gen -p github.com/brave-intl/bat-go/libs/clients/ratios -i Client -t ./.prom-gowrap.tmpl -o ./libs/clients/ratios/instrumented_client.go
sed -i'bak' 's/ratios.//g' libs/clients/ratios/instrumented_client.go
gowrap gen -p github.com/brave-intl/bat-go/libs/clients/reputation -i Client -t ./.prom-gowrap.tmpl -o ./libs/clients/reputation/instrumented_client.go
sed -i'bak' 's/reputation.//g' libs/clients/reputation/instrumented_client.go
gowrap gen -p github.com/brave-intl/bat-go/libs/clients/gemini -i Client -t ./.prom-gowrap.tmpl -o ./libs/clients/gemini/instrumented_client.go
sed -i'bak' 's/gemini.//g' libs/clients/gemini/instrumented_client.go
gowrap gen -p github.com/brave-intl/bat-go/libs/clients/bitflyer -i Client -t ./.prom-gowrap.tmpl -o ./libs/clients/bitflyer/instrumented_client.go
sed -i'bak' 's/bitflyer.//g' libs/clients/bitflyer/instrumented_client.go
gowrap gen -p github.com/brave-intl/bat-go/libs/clients/coingecko -i Client -t ./.prom-gowrap.tmpl -o ./libs/clients/coingecko/instrumented_client.go
sed -i'bak' 's/coingecko.//g' libs/clients/coingecko/instrumented_client.go
# fix all instrumented cause the interfaces are all called "client"
sed -i'bak' 's/client_duration_seconds/cbr_client_duration_seconds/g' utils/clients/cbr/instrumented_client.go
sed -i'bak' 's/client_duration_seconds/ratios_client_duration_seconds/g' utils/clients/ratios/instrumented_client.go
sed -i'bak' 's/client_duration_seconds/reputation_client_duration_seconds/g' utils/clients/reputation/instrumented_client.go
sed -i'bak' 's/client_duration_seconds/gemini_client_duration_seconds/g' utils/clients/gemini/instrumented_client.go
sed -i'bak' 's/client_duration_seconds/bitflyer_client_duration_seconds/g' utils/clients/bitflyer/instrumented_client.go
sed -i'bak' 's/client_duration_seconds/coingecko_client_duration_seconds/g' utils/clients/coingecko/instrumented_client.go
sed -i'bak' 's/client_duration_seconds/cbr_client_duration_seconds/g' libs/clients/cbr/instrumented_client.go
sed -i'bak' 's/client_duration_seconds/ratios_client_duration_seconds/g' libs/clients/ratios/instrumented_client.go
sed -i'bak' 's/client_duration_seconds/reputation_client_duration_seconds/g' libs/clients/reputation/instrumented_client.go
sed -i'bak' 's/client_duration_seconds/gemini_client_duration_seconds/g' libs/clients/gemini/instrumented_client.go
sed -i'bak' 's/client_duration_seconds/bitflyer_client_duration_seconds/g' libs/clients/bitflyer/instrumented_client.go
sed -i'bak' 's/client_duration_seconds/coingecko_client_duration_seconds/g' libs/clients/coingecko/instrumented_client.go

%-docker: docker
docker build --build-arg COMMIT=$(GIT_COMMIT) --build-arg VERSION=$(GIT_VERSION) \
Expand Down Expand Up @@ -152,7 +158,7 @@ create-json-schema:
go run main.go generate json-schema

test:
GODEBUG=x509ignoreCN=0 go test -count 1 -v -p 1 $(TEST_FLAGS)
go test -count 1 -v -p 1 $(TEST_FLAGS) github.com/brave-intl/bat-go/...

format:
gofmt -s -w ./
Expand Down
59 changes: 0 additions & 59 deletions cmd/ratios/ratios.go

This file was deleted.

47 changes: 19 additions & 28 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package cmd
import (
"context"
"fmt"
"log"
"os"
"time"

"github.com/brave-intl/bat-go/utils/clients"
appctx "github.com/brave-intl/bat-go/utils/context"
errorutils "github.com/brave-intl/bat-go/utils/errors"
"github.com/brave-intl/bat-go/utils/logging"
"github.com/brave-intl/bat-go/libs/clients"
cmdutils "github.com/brave-intl/bat-go/libs/cmd"
appctx "github.com/brave-intl/bat-go/libs/context"
errorutils "github.com/brave-intl/bat-go/libs/errors"
"github.com/brave-intl/bat-go/libs/logging"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -25,15 +25,6 @@ var (
ctx = context.Background()
)

// Must helper to make sure there is no errors
func Must(err error) {
if err != nil {
log.Printf("failed to initialize: %s\n", err.Error())
// exit with failure
os.Exit(1)
}
}

// Execute - the main entrypoint for all subcommands in bat-go
func Execute(version, commit, buildTime string) {
// setup context with logging, but first we need to setup the environment
Expand All @@ -60,43 +51,43 @@ func init() {
// pprof-enabled - defaults to ""
RootCmd.PersistentFlags().String("pprof-enabled", "",
"pprof enablement")
Must(viper.BindPFlag("pprof-enabled", RootCmd.PersistentFlags().Lookup("pprof-enabled")))
Must(viper.BindEnv("pprof-enabled", "PPROF_ENABLED"))
cmdutils.Must(viper.BindPFlag("pprof-enabled", RootCmd.PersistentFlags().Lookup("pprof-enabled")))
cmdutils.Must(viper.BindEnv("pprof-enabled", "PPROF_ENABLED"))

// env - defaults to local
RootCmd.PersistentFlags().String("environment", "local",
"the default environment")
Must(viper.BindPFlag("environment", RootCmd.PersistentFlags().Lookup("environment")))
Must(viper.BindEnv("environment", "ENV"))
cmdutils.Must(viper.BindPFlag("environment", RootCmd.PersistentFlags().Lookup("environment")))
cmdutils.Must(viper.BindEnv("environment", "ENV"))

// debug logging - defaults to off
RootCmd.PersistentFlags().Bool("debug", false, "turn on debug logging")
Must(viper.BindPFlag("debug", RootCmd.PersistentFlags().Lookup("debug")))
Must(viper.BindEnv("debug", "DEBUG"))
cmdutils.Must(viper.BindPFlag("debug", RootCmd.PersistentFlags().Lookup("debug")))
cmdutils.Must(viper.BindEnv("debug", "DEBUG"))

// ratiosAccessToken (required by all)
RootCmd.PersistentFlags().String("ratios-token", "",
"the ratios service token for this service")
Must(viper.BindPFlag("ratios-token", RootCmd.PersistentFlags().Lookup("ratios-token")))
Must(viper.BindEnv("ratios-token", "RATIOS_TOKEN"))
cmdutils.Must(viper.BindPFlag("ratios-token", RootCmd.PersistentFlags().Lookup("ratios-token")))
cmdutils.Must(viper.BindEnv("ratios-token", "RATIOS_TOKEN"))

// ratiosService (required by all)
RootCmd.PersistentFlags().String("ratios-service", "",
"the ratios service address")
Must(viper.BindPFlag("ratios-service", RootCmd.PersistentFlags().Lookup("ratios-service")))
Must(viper.BindEnv("ratios-service", "RATIOS_SERVICE"))
cmdutils.Must(viper.BindPFlag("ratios-service", RootCmd.PersistentFlags().Lookup("ratios-service")))
cmdutils.Must(viper.BindEnv("ratios-service", "RATIOS_SERVICE"))

// ratiosClientExpiry
RootCmd.PersistentFlags().Duration("ratios-client-cache-expiry", 5*time.Second,
"the ratios client cache default eviction duration")
Must(viper.BindPFlag("ratios-client-cache-expiry", RootCmd.PersistentFlags().Lookup("ratios-client-cache-expiry")))
Must(viper.BindEnv("ratios-client-cache-expiry", "RATIOS_CACHE_EXPIRY"))
cmdutils.Must(viper.BindPFlag("ratios-client-cache-expiry", RootCmd.PersistentFlags().Lookup("ratios-client-cache-expiry")))
cmdutils.Must(viper.BindEnv("ratios-client-cache-expiry", "RATIOS_CACHE_EXPIRY"))

// ratiosClientPurge
RootCmd.PersistentFlags().Duration("ratios-client-cache-purge", 1*time.Minute,
"the ratios client cache default purge duration")
Must(viper.BindPFlag("ratios-client-cache-purge", RootCmd.PersistentFlags().Lookup("ratios-client-cache-purge")))
Must(viper.BindEnv("ratios-client-cache-purge", "RATIOS_CACHE_PURGE"))
cmdutils.Must(viper.BindPFlag("ratios-client-cache-purge", RootCmd.PersistentFlags().Lookup("ratios-client-cache-purge")))
cmdutils.Must(viper.BindEnv("ratios-client-cache-purge", "RATIOS_CACHE_PURGE"))

RootCmd.AddCommand(VersionCmd)
}
Expand Down
Loading

0 comments on commit d134260

Please sign in to comment.