From 0f52b7d2f36e4eb3b23042037e18483a873769cf Mon Sep 17 00:00:00 2001 From: Owen Cabalceta Date: Wed, 18 Dec 2024 12:07:02 -0500 Subject: [PATCH] refactor: update code layout be to consistent with our newer servers (skeleton layout) --- .gitignore | 2 +- main.go => cmd/tr1d1um/main.go | 71 ++++---------- tr1d1um.yaml => cmd/tr1d1um/tr1d1um.yaml | 2 - acquire.go => internal/acquire.go | 2 +- auth.go => internal/auth.go | 4 +- .../basculeLogging.go | 2 +- .../basculeLogging_test.go | 2 +- .../primaryHandler.go | 22 ++--- routes.go => internal/routes.go | 14 +-- internal/setup.go | 95 +++++++++++++++++++ {stat => internal/stat}/endpoint.go | 0 {stat => internal/stat}/endpoint_test.go | 0 {stat => internal/stat}/mocks_test.go | 6 +- {stat => internal/stat}/service.go | 2 +- {stat => internal/stat}/service_test.go | 2 +- {stat => internal/stat}/transport.go | 2 +- {stat => internal/stat}/transport_test.go | 2 +- .../transaction}/context.go | 0 .../transaction}/errors.go | 0 .../transaction}/errors_test.go | 0 .../transaction}/transactor.go | 0 .../transaction}/transactor_test.go | 0 .../translation}/endpoint.go | 0 .../translation}/endpoint_test.go | 0 .../translation}/errors.go | 2 +- .../translation}/mocks_test.go | 6 +- .../translation}/service.go | 2 +- .../translation}/service_test.go | 6 +- .../translation}/transport.go | 2 +- .../translation}/transport_test.go | 2 +- .../translation}/transport_utils.go | 2 +- .../translation}/transport_utils_test.go | 2 +- .../translation}/wdmp_type.go | 0 setup.go | 56 ----------- 34 files changed, 156 insertions(+), 154 deletions(-) rename main.go => cmd/tr1d1um/main.go (64%) rename tr1d1um.yaml => cmd/tr1d1um/tr1d1um.yaml (99%) rename acquire.go => internal/acquire.go (99%) rename auth.go => internal/auth.go (96%) rename basculeLogging.go => internal/basculeLogging.go (96%) rename basculeLogging_test.go => internal/basculeLogging_test.go (98%) rename primaryHandler.go => internal/primaryHandler.go (91%) rename routes.go => internal/routes.go (97%) create mode 100644 internal/setup.go rename {stat => internal/stat}/endpoint.go (100%) rename {stat => internal/stat}/endpoint_test.go (100%) rename {stat => internal/stat}/mocks_test.go (94%) rename {stat => internal/stat}/service.go (97%) rename {stat => internal/stat}/service_test.go (97%) rename {stat => internal/stat}/transport.go (98%) rename {stat => internal/stat}/transport_test.go (98%) rename {transaction => internal/transaction}/context.go (100%) rename {transaction => internal/transaction}/errors.go (100%) rename {transaction => internal/transaction}/errors_test.go (100%) rename {transaction => internal/transaction}/transactor.go (100%) rename {transaction => internal/transaction}/transactor_test.go (100%) rename {translation => internal/translation}/endpoint.go (100%) rename {translation => internal/translation}/endpoint_test.go (100%) rename {translation => internal/translation}/errors.go (96%) rename {translation => internal/translation}/mocks_test.go (92%) rename {translation => internal/translation}/service.go (97%) rename {translation => internal/translation}/service_test.go (96%) rename {translation => internal/translation}/transport.go (99%) rename {translation => internal/translation}/transport_test.go (99%) rename {translation => internal/translation}/transport_utils.go (98%) rename {translation => internal/translation}/transport_utils_test.go (99%) rename {translation => internal/translation}/wdmp_type.go (100%) delete mode 100644 setup.go diff --git a/.gitignore b/.gitignore index c78299ce..4906627f 100644 --- a/.gitignore +++ b/.gitignore @@ -60,7 +60,7 @@ _testmain.go *.prof cpuprofile -tr1d1um +./tr1d1um .ignore .vscode .yaml diff --git a/main.go b/cmd/tr1d1um/main.go similarity index 64% rename from main.go rename to cmd/tr1d1um/main.go index 4eb727bf..7ead576b 100644 --- a/main.go +++ b/cmd/tr1d1um/main.go @@ -17,6 +17,7 @@ import ( "github.com/xmidt-org/arrange/arrangepprof" "github.com/xmidt-org/touchstone" "github.com/xmidt-org/touchstone/touchhttp" + tr1d1um "github.com/xmidt-org/tr1d1um/internal" "go.uber.org/fx" "go.uber.org/zap" @@ -25,32 +26,8 @@ import ( "github.com/xmidt-org/candlelight" ) -// convenient global values const ( - DefaultKeyID = "current" - apiVersion = "v3" - prevAPIVersion = "v2" - applicationName = "tr1d1um" - apiBase = "api/" + apiVersion - prevAPIBase = "api/" + prevAPIVersion - apiBaseDualVersion = "api/{version:" + apiVersion + "|" + prevAPIVersion + "}" -) - -const ( - translationServicesKey = "supportedServices" - targetURLKey = "targetURL" - netDialerTimeoutKey = "netDialerTimeout" - clientTimeoutKey = "clientTimeout" - reqTimeoutKey = "respWaitTimeout" - reqRetryIntervalKey = "requestRetryInterval" - reqMaxRetriesKey = "requestMaxRetries" - wrpSourceKey = "WRPSource" - hooksSchemeKey = "hooksScheme" - reducedTransactionLoggingCodesKey = "logging.reducedLoggingResponseCodes" - authAcquirerKey = "authAcquirer" - webhookConfigKey = "webhook" - anclaClientConfigKey = "webhook.BasicClientConfig" - tracingConfigKey = "tracing" + tracingConfigKey = "tracing" ) var ( @@ -60,26 +37,14 @@ var ( Commit string ) -var defaults = map[string]interface{}{ - translationServicesKey: []string{}, // no services allowed by the default - targetURLKey: "localhost:6000", - netDialerTimeoutKey: "5s", - clientTimeoutKey: "50s", - reqTimeoutKey: "40s", - reqRetryIntervalKey: "2s", - reqMaxRetriesKey: 2, - wrpSourceKey: "dns:localhost", - hooksSchemeKey: "https", -} - type XmidtClientTimeoutConfigIn struct { fx.In - XmidtClientTimeout httpClientTimeout `name:"xmidtClientTimeout"` + XmidtClientTimeout tr1d1um.HttpClientTimeout `name:"xmidtClientTimeout"` } type ArgusClientTimeoutConfigIn struct { fx.In - ArgusClientTimeout httpClientTimeout `name:"argusClientTimeout"` + ArgusClientTimeout tr1d1um.HttpClientTimeout `name:"argusClientTimeout"` } type TracingConfigIn struct { @@ -95,11 +60,11 @@ type ConstOut struct { func consts() ConstOut { return ConstOut{ - DefaultKeyID: DefaultKeyID, + DefaultKeyID: tr1d1um.DefaultKeyID, } } -func configureXmidtClientTimeout(in XmidtClientTimeoutConfigIn) httpClientTimeout { +func configureXmidtClientTimeout(in XmidtClientTimeoutConfigIn) tr1d1um.HttpClientTimeout { xct := in.XmidtClientTimeout if xct.ClientTimeout == 0 { @@ -114,7 +79,7 @@ func configureXmidtClientTimeout(in XmidtClientTimeoutConfigIn) httpClientTimeou return xct } -func configureArgusClientTimeout(in ArgusClientTimeoutConfigIn) httpClientTimeout { +func configureArgusClientTimeout(in ArgusClientTimeoutConfigIn) tr1d1um.HttpClientTimeout { act := in.ArgusClientTimeout if act.ClientTimeout == 0 { @@ -128,7 +93,7 @@ func configureArgusClientTimeout(in ArgusClientTimeoutConfigIn) httpClientTimeou func loadTracing(in TracingConfigIn) (candlelight.Tracing, error) { traceConfig := in.TracingConfig - traceConfig.ApplicationName = applicationName + traceConfig.ApplicationName = tr1d1um.ApplicationName tracing, err := candlelight.New(traceConfig) if err != nil { return candlelight.Tracing{}, err @@ -150,7 +115,7 @@ func printVersion(f *pflag.FlagSet, arguments []string) (bool, error) { } func printVersionInfo(writer io.Writer) { - fmt.Fprintf(writer, "%s:\n", applicationName) + fmt.Fprintf(writer, "%s:\n", tr1d1um.ApplicationName) fmt.Fprintf(writer, " version: \t%s\n", Version) fmt.Fprintf(writer, " go version: \t%s\n", runtime.Version()) fmt.Fprintf(writer, " built time: \t%s\n", Date) @@ -169,8 +134,8 @@ func exitIfError(logger *zap.Logger, err error) { } //nolint:funlen -func tr1d1um(arguments []string) (exitCode int) { - v, l, f, err := setup(arguments) +func Tr1d1um(arguments []string) (exitCode int) { + v, l, f, err := tr1d1um.Setup(arguments) if err != nil { fmt.Fprintln(os.Stderr, err) return 1 @@ -189,8 +154,8 @@ func tr1d1um(arguments []string) (exitCode int) { fx.Supply(l), fx.Supply(v), arrange.ForViper(v), - arrange.ProvideKey("xmidtClientTimeout", httpClientTimeout{}), - arrange.ProvideKey("argusClientTimeout", httpClientTimeout{}), + arrange.ProvideKey("xmidtClientTimeout", tr1d1um.HttpClientTimeout{}), + arrange.ProvideKey("argusClientTimeout", tr1d1um.HttpClientTimeout{}), touchstone.Provide(), touchhttp.Provide(), anclafx.Provide(), @@ -209,11 +174,11 @@ func tr1d1um(arguments []string) (exitCode int) { Target: configureArgusClientTimeout, }, loadTracing, - provideAnclaHTTPClient, + tr1d1um.ProvideAnclaHTTPClient, ), - provideAuthChain("authx.inbound"), - provideServers(), - provideHandlers(), + tr1d1um.ProvideAuthChain("authx.inbound"), + tr1d1um.ProvideServers(), + tr1d1um.ProvideHandlers(), ) switch err := app.Err(); { @@ -230,5 +195,5 @@ func tr1d1um(arguments []string) (exitCode int) { } func main() { - os.Exit(tr1d1um(os.Args)) + os.Exit(Tr1d1um(os.Args)) } diff --git a/tr1d1um.yaml b/cmd/tr1d1um/tr1d1um.yaml similarity index 99% rename from tr1d1um.yaml rename to cmd/tr1d1um/tr1d1um.yaml index 45136597..5c748add 100644 --- a/tr1d1um.yaml +++ b/cmd/tr1d1um/tr1d1um.yaml @@ -91,7 +91,6 @@ logging: # OutputPaths is a list of URLs or file paths to write logging output to. outputPaths: - stdout - # - /var/log/tr1d1um/tr1d1um.log # Level is the minimum enabled logging level. Note that this is a dynamic # level, so calling Config.Level.SetLevel will atomically change the log @@ -102,7 +101,6 @@ logging: # zapcore.EncoderConfig for details. errorOutputPaths: - stderr - - denopink-tr1d1um.log # EncoderConfig sets options for the chosen encoder. See # zapcore.EncoderConfig for details. diff --git a/acquire.go b/internal/acquire.go similarity index 99% rename from acquire.go rename to internal/acquire.go index 9fa6bbf4..ab7cfe80 100644 --- a/acquire.go +++ b/internal/acquire.go @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC // SPDX-License-Identifier: Apache-2.0 -package main +package tr1d1um import ( "encoding/json" diff --git a/auth.go b/internal/auth.go similarity index 96% rename from auth.go rename to internal/auth.go index b8128a3b..f16c3de2 100644 --- a/auth.go +++ b/internal/auth.go @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC // SPDX-License-Identifier: Apache-2.0 -package main +package tr1d1um import ( "errors" @@ -31,7 +31,7 @@ type JWTValidator struct { Leeway bascule.Leeway } -func provideAuthChain(configKey string) fx.Option { +func ProvideAuthChain(configKey string) fx.Option { return fx.Options( basculehttp.ProvideMetrics(), basculechecks.ProvideMetrics(), diff --git a/basculeLogging.go b/internal/basculeLogging.go similarity index 96% rename from basculeLogging.go rename to internal/basculeLogging.go index 57d4de96..c0f0db55 100644 --- a/basculeLogging.go +++ b/internal/basculeLogging.go @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC // SPDX-License-Identifier: Apache-2.0 -package main +package tr1d1um import ( "net/http" diff --git a/basculeLogging_test.go b/internal/basculeLogging_test.go similarity index 98% rename from basculeLogging_test.go rename to internal/basculeLogging_test.go index 91d75917..d28e82c2 100644 --- a/basculeLogging_test.go +++ b/internal/basculeLogging_test.go @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC // SPDX-License-Identifier: Apache-2.0 -package main +package tr1d1um_test import ( "net/http" diff --git a/primaryHandler.go b/internal/primaryHandler.go similarity index 91% rename from primaryHandler.go rename to internal/primaryHandler.go index 785e7645..7ca89507 100644 --- a/primaryHandler.go +++ b/internal/primaryHandler.go @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC // SPDX-License-Identifier: Apache-2.0 -package main +package tr1d1um import ( "errors" @@ -18,9 +18,9 @@ import ( "github.com/xmidt-org/candlelight" "github.com/xmidt-org/touchstone" "github.com/xmidt-org/touchstone/touchhttp" - "github.com/xmidt-org/tr1d1um/stat" - "github.com/xmidt-org/tr1d1um/transaction" - "github.com/xmidt-org/tr1d1um/translation" + "github.com/xmidt-org/tr1d1um/internal/stat" + "github.com/xmidt-org/tr1d1um/internal/transaction" + "github.com/xmidt-org/tr1d1um/internal/translation" webhook "github.com/xmidt-org/webhook-schema" "github.com/xmidt-org/webpa-common/v2/xhttp" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" @@ -28,8 +28,8 @@ import ( "go.uber.org/zap" ) -// httpClientTimeout contains timeouts for an HTTP client and its requests. -type httpClientTimeout struct { +// HttpClientTimeout contains timeouts for an HTTP client and its requests. +type HttpClientTimeout struct { // ClientTimeout is HTTP Client Timeout. ClientTimeout time.Duration @@ -60,7 +60,7 @@ type provideWebhookHandlersIn struct { type provideAnclaHTTPClientIn struct { fx.In - ArgusClientTimeout httpClientTimeout `name:"argus_client_timeout"` + ArgusClientTimeout HttpClientTimeout `name:"argus_client_timeout"` Tracing candlelight.Tracing } @@ -74,7 +74,7 @@ type provideWebhookHandlersOut struct { type ServiceOptionsIn struct { fx.In Logger *zap.Logger - XmidtClientTimeout httpClientTimeout `name:"xmidt_client_timeout"` + XmidtClientTimeout HttpClientTimeout `name:"xmidt_client_timeout"` RequestMaxRetries int `name:"requestMaxRetries"` RequestRetryInterval time.Duration `name:"requestRetryInterval"` TargetURL string `name:"targetURL"` @@ -88,11 +88,11 @@ type ServiceOptionsOut struct { TranslationServiceOptions *translation.ServiceOptions } -func provideAnclaHTTPClient(in provideAnclaHTTPClientIn) chrysom.HTTPClient { +func ProvideAnclaHTTPClient(in provideAnclaHTTPClientIn) chrysom.HTTPClient { return newHTTPClient(in.ArgusClientTimeout, in.Tracing) } -func newHTTPClient(timeouts httpClientTimeout, tracing candlelight.Tracing) *http.Client { +func newHTTPClient(timeouts HttpClientTimeout, tracing candlelight.Tracing) *http.Client { var transport http.RoundTripper = &http.Transport{ Dial: (&net.Dialer{ Timeout: timeouts.NetDialerTimeout, @@ -162,7 +162,7 @@ func provideWebhookHandlers(in provideWebhookHandlersIn) (out provideWebhookHand return } -func provideHandlers() fx.Option { +func ProvideHandlers() fx.Option { return fx.Options( arrange.ProvideKey(authAcquirerKey, authAcquirerConfig{}), fx.Provide( diff --git a/routes.go b/internal/routes.go similarity index 97% rename from routes.go rename to internal/routes.go index c47cd5cb..0f9def2a 100644 --- a/routes.go +++ b/internal/routes.go @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC // SPDX-License-Identifier: Apache-2.0 -package main +package tr1d1um import ( "bytes" @@ -9,7 +9,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" "os" "time" @@ -26,8 +26,8 @@ import ( "github.com/xmidt-org/sallust/sallusthttp" "github.com/xmidt-org/touchstone" "github.com/xmidt-org/touchstone/touchhttp" - "github.com/xmidt-org/tr1d1um/stat" - "github.com/xmidt-org/tr1d1um/translation" + "github.com/xmidt-org/tr1d1um/internal/stat" + "github.com/xmidt-org/tr1d1um/internal/translation" webhook "github.com/xmidt-org/webhook-schema" "go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux" "go.uber.org/fx" @@ -115,7 +115,7 @@ type metricsRoutesIn struct { Handler touchhttp.Handler } -func provideServers() fx.Option { +func ProvideServers() fx.Option { return fx.Options( arrange.ProvideKey(reqMaxRetriesKey, 0), arrange.ProvideKey(reqRetryIntervalKey, time.Duration(0)), @@ -315,7 +315,7 @@ func fixV2Duration(getLogger func(context.Context) *zap.Logger, config ancla.TTL // the v2 handler. logger := sallusthttp.Get(r) - requestPayload, err := ioutil.ReadAll(r.Body) + requestPayload, err := io.ReadAll(r.Body) if err != nil { v2ErrEncode(w, logger, err, 0) return @@ -366,7 +366,7 @@ func fixV2Duration(getLogger func(context.Context) *zap.Logger, config ancla.TTL if err != nil { v2ErrEncode(w, logger, fmt.Errorf("failed to recreate request body: %v", err), 0) } - r.Body = ioutil.NopCloser(bytes.NewBuffer(body)) + r.Body = io.NopCloser(bytes.NewBuffer(body)) if v2Handler == nil { v2Handler = next diff --git a/internal/setup.go b/internal/setup.go new file mode 100644 index 00000000..79fd16b7 --- /dev/null +++ b/internal/setup.go @@ -0,0 +1,95 @@ +// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC +// SPDX-License-Identifier: Apache-2.0 + +package tr1d1um + +import ( + "fmt" + + "github.com/spf13/pflag" + "github.com/spf13/viper" + "github.com/xmidt-org/sallust" + "go.uber.org/zap" +) + +// convenient global values +const ( + DefaultKeyID = "current" + apiVersion = "v3" + prevAPIVersion = "v2" + ApplicationName = "tr1d1um" + apiBase = "api/" + apiVersion + prevAPIBase = "api/" + prevAPIVersion + apiBaseDualVersion = "api/{version:" + apiVersion + "|" + prevAPIVersion + "}" +) + +const ( + translationServicesKey = "supportedServices" + targetURLKey = "targetURL" + netDialerTimeoutKey = "netDialerTimeout" + clientTimeoutKey = "clientTimeout" + reqTimeoutKey = "respWaitTimeout" + reqRetryIntervalKey = "requestRetryInterval" + reqMaxRetriesKey = "requestMaxRetries" + wrpSourceKey = "WRPSource" + hooksSchemeKey = "hooksScheme" + reducedTransactionLoggingCodesKey = "logging.reducedLoggingResponseCodes" + authAcquirerKey = "authAcquirer" + webhookConfigKey = "webhook" + anclaClientConfigKey = "webhook.BasicClientConfig" +) + +var defaults = map[string]interface{}{ + translationServicesKey: []string{}, // no services allowed by the default + targetURLKey: "localhost:6000", + netDialerTimeoutKey: "5s", + clientTimeoutKey: "50s", + reqTimeoutKey: "40s", + reqRetryIntervalKey: "2s", + reqMaxRetriesKey: 2, + wrpSourceKey: "dns:localhost", + hooksSchemeKey: "https", +} + +func setupFlagSet(fs *pflag.FlagSet) { + fs.StringP("file", "f", "", "the configuration file to use. Overrides the search path.") + fs.BoolP("debug", "d", false, "enables debug logging. Overrides configuration.") + fs.BoolP("version", "v", false, "print version and exit") +} + +func Setup(args []string) (*viper.Viper, *zap.Logger, *pflag.FlagSet, error) { + fs := pflag.NewFlagSet(ApplicationName, pflag.ContinueOnError) + setupFlagSet(fs) + err := fs.Parse(args) + if err != nil { + return nil, nil, fs, fmt.Errorf("failed to create parse args: %w", err) + } + + v := viper.New() + for k, va := range defaults { + v.SetDefault(k, va) + } + + if file, _ := fs.GetString("file"); len(file) > 0 { + v.SetConfigFile(file) + err = v.ReadInConfig() + } else { + v.SetConfigName(ApplicationName) + v.AddConfigPath(fmt.Sprintf("/etc/%s", ApplicationName)) + v.AddConfigPath(fmt.Sprintf("$HOME/.%s", ApplicationName)) + v.AddConfigPath(".") + err = v.ReadInConfig() + } + if err != nil { + return v, nil, fs, fmt.Errorf("failed to read config file: %w", err) + } + + if debug, _ := fs.GetBool("debug"); debug { + v.Set("log.level", "DEBUG") + } + + var c sallust.Config + v.UnmarshalKey("logging", &c) + l := zap.Must(c.Build()) + return v, l, fs, err +} diff --git a/stat/endpoint.go b/internal/stat/endpoint.go similarity index 100% rename from stat/endpoint.go rename to internal/stat/endpoint.go diff --git a/stat/endpoint_test.go b/internal/stat/endpoint_test.go similarity index 100% rename from stat/endpoint_test.go rename to internal/stat/endpoint_test.go diff --git a/stat/mocks_test.go b/internal/stat/mocks_test.go similarity index 94% rename from stat/mocks_test.go rename to internal/stat/mocks_test.go index ebdbdaeb..906ddd40 100644 --- a/stat/mocks_test.go +++ b/internal/stat/mocks_test.go @@ -4,12 +4,12 @@ package stat import ( - context "context" + "context" "net/http" - "github.com/xmidt-org/tr1d1um/transaction" + "github.com/xmidt-org/tr1d1um/internal/transaction" - mock "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/mock" ) // MockService is an autogenerated mock type for the Service type diff --git a/stat/service.go b/internal/stat/service.go similarity index 97% rename from stat/service.go rename to internal/stat/service.go index 5f9d3b0a..fd2316f2 100644 --- a/stat/service.go +++ b/internal/stat/service.go @@ -10,7 +10,7 @@ import ( "github.com/xmidt-org/bascule/acquire" - "github.com/xmidt-org/tr1d1um/transaction" + "github.com/xmidt-org/tr1d1um/internal/transaction" ) // Service defines the behavior of the device statistics Tr1d1um Service. diff --git a/stat/service_test.go b/internal/stat/service_test.go similarity index 97% rename from stat/service_test.go rename to internal/stat/service_test.go index c194a846..f388e6f6 100644 --- a/stat/service_test.go +++ b/internal/stat/service_test.go @@ -11,7 +11,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" - "github.com/xmidt-org/tr1d1um/transaction" + "github.com/xmidt-org/tr1d1um/internal/transaction" ) func TestRequestStat(t *testing.T) { diff --git a/stat/transport.go b/internal/stat/transport.go similarity index 98% rename from stat/transport.go rename to internal/stat/transport.go index fb77a768..80cc52e9 100644 --- a/stat/transport.go +++ b/internal/stat/transport.go @@ -11,7 +11,7 @@ import ( "github.com/xmidt-org/candlelight" "github.com/xmidt-org/sallust" - "github.com/xmidt-org/tr1d1um/transaction" + "github.com/xmidt-org/tr1d1um/internal/transaction" "github.com/xmidt-org/wrp-go/v3" "go.uber.org/zap" diff --git a/stat/transport_test.go b/internal/stat/transport_test.go similarity index 98% rename from stat/transport_test.go rename to internal/stat/transport_test.go index 04abbd85..a215e2d6 100644 --- a/stat/transport_test.go +++ b/internal/stat/transport_test.go @@ -13,7 +13,7 @@ import ( "net/http/httptest" "testing" - "github.com/xmidt-org/tr1d1um/transaction" + "github.com/xmidt-org/tr1d1um/internal/transaction" "github.com/xmidt-org/wrp-go/v3" "github.com/gorilla/mux" diff --git a/transaction/context.go b/internal/transaction/context.go similarity index 100% rename from transaction/context.go rename to internal/transaction/context.go diff --git a/transaction/errors.go b/internal/transaction/errors.go similarity index 100% rename from transaction/errors.go rename to internal/transaction/errors.go diff --git a/transaction/errors_test.go b/internal/transaction/errors_test.go similarity index 100% rename from transaction/errors_test.go rename to internal/transaction/errors_test.go diff --git a/transaction/transactor.go b/internal/transaction/transactor.go similarity index 100% rename from transaction/transactor.go rename to internal/transaction/transactor.go diff --git a/transaction/transactor_test.go b/internal/transaction/transactor_test.go similarity index 100% rename from transaction/transactor_test.go rename to internal/transaction/transactor_test.go diff --git a/translation/endpoint.go b/internal/translation/endpoint.go similarity index 100% rename from translation/endpoint.go rename to internal/translation/endpoint.go diff --git a/translation/endpoint_test.go b/internal/translation/endpoint_test.go similarity index 100% rename from translation/endpoint_test.go rename to internal/translation/endpoint_test.go diff --git a/translation/errors.go b/internal/translation/errors.go similarity index 96% rename from translation/errors.go rename to internal/translation/errors.go index 7fd99122..85f32162 100644 --- a/translation/errors.go +++ b/internal/translation/errors.go @@ -6,7 +6,7 @@ package translation import ( "errors" - "github.com/xmidt-org/tr1d1um/transaction" + "github.com/xmidt-org/tr1d1um/internal/transaction" ) // Error values definitions for the translation service diff --git a/translation/mocks_test.go b/internal/translation/mocks_test.go similarity index 92% rename from translation/mocks_test.go rename to internal/translation/mocks_test.go index ad73749d..a20d8e96 100644 --- a/translation/mocks_test.go +++ b/internal/translation/mocks_test.go @@ -7,11 +7,11 @@ import ( context "context" "net/http" - transaction "github.com/xmidt-org/tr1d1um/transaction" + "github.com/xmidt-org/tr1d1um/internal/transaction" - mock "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/mock" - wrp "github.com/xmidt-org/wrp-go/v3" + "github.com/xmidt-org/wrp-go/v3" ) // MockService is an autogenerated mock type for the Service type diff --git a/translation/service.go b/internal/translation/service.go similarity index 97% rename from translation/service.go rename to internal/translation/service.go index 1462bb43..6e1344ba 100644 --- a/translation/service.go +++ b/internal/translation/service.go @@ -10,7 +10,7 @@ import ( "net/http" "github.com/xmidt-org/bascule/acquire" - "github.com/xmidt-org/tr1d1um/transaction" + "github.com/xmidt-org/tr1d1um/internal/transaction" "github.com/xmidt-org/wrp-go/v3" ) diff --git a/translation/service_test.go b/internal/translation/service_test.go similarity index 96% rename from translation/service_test.go rename to internal/translation/service_test.go index 22af2070..567a52d4 100644 --- a/translation/service_test.go +++ b/internal/translation/service_test.go @@ -7,7 +7,7 @@ import ( "bytes" "context" "errors" - "io/ioutil" + "io" "net/http" "testing" @@ -78,9 +78,9 @@ func TestSendWRP(t *testing.T) { assert.EqualValues(testCase.ExpectedRequestAuth, r.Header.Get("Authorization")) assert.EqualValues(wrp.Msgpack.ContentType(), r.Header.Get("Content-Type")) - data, err := ioutil.ReadAll(r.Body) + data, err := io.ReadAll(r.Body) require.Nil(err) - r.Body = ioutil.NopCloser(bytes.NewBuffer(data)) + r.Body = io.NopCloser(bytes.NewBuffer(data)) assert.EqualValues(string(expected), string(data)) diff --git a/translation/transport.go b/internal/translation/transport.go similarity index 99% rename from translation/transport.go rename to internal/translation/transport.go index 9ebb1c74..a0883b4f 100644 --- a/translation/transport.go +++ b/internal/translation/transport.go @@ -23,7 +23,7 @@ import ( "github.com/xmidt-org/bascule/basculechecks" "github.com/xmidt-org/candlelight" "github.com/xmidt-org/sallust" - "github.com/xmidt-org/tr1d1um/transaction" + "github.com/xmidt-org/tr1d1um/internal/transaction" "github.com/xmidt-org/wrp-go/v3" "github.com/xmidt-org/wrp-go/v3/wrphttp" ) diff --git a/translation/transport_test.go b/internal/translation/transport_test.go similarity index 99% rename from translation/transport_test.go rename to internal/translation/transport_test.go index 5e52e7f6..2d7b1d93 100644 --- a/translation/transport_test.go +++ b/internal/translation/transport_test.go @@ -13,7 +13,7 @@ import ( "net/http/httptest" "testing" - "github.com/xmidt-org/tr1d1um/transaction" + "github.com/xmidt-org/tr1d1um/internal/transaction" "github.com/gorilla/mux" "github.com/stretchr/testify/assert" diff --git a/translation/transport_utils.go b/internal/translation/transport_utils.go similarity index 98% rename from translation/transport_utils.go rename to internal/translation/transport_utils.go index 17d3e39e..ccafc5b0 100644 --- a/translation/transport_utils.go +++ b/internal/translation/transport_utils.go @@ -12,7 +12,7 @@ import ( "net/http" "github.com/xmidt-org/sallust" - "github.com/xmidt-org/tr1d1um/transaction" + "github.com/xmidt-org/tr1d1um/internal/transaction" "go.uber.org/zap" kithttp "github.com/go-kit/kit/transport/http" diff --git a/translation/transport_utils_test.go b/internal/translation/transport_utils_test.go similarity index 99% rename from translation/transport_utils_test.go rename to internal/translation/transport_utils_test.go index e6aa07ee..9408a63c 100644 --- a/translation/transport_utils_test.go +++ b/internal/translation/transport_utils_test.go @@ -12,7 +12,7 @@ import ( "github.com/gorilla/mux" "github.com/stretchr/testify/assert" - transaction "github.com/xmidt-org/tr1d1um/transaction" + "github.com/xmidt-org/tr1d1um/internal/transaction" "github.com/xmidt-org/wrp-go/v3" ) diff --git a/translation/wdmp_type.go b/internal/translation/wdmp_type.go similarity index 100% rename from translation/wdmp_type.go rename to internal/translation/wdmp_type.go diff --git a/setup.go b/setup.go deleted file mode 100644 index 3025b7ea..00000000 --- a/setup.go +++ /dev/null @@ -1,56 +0,0 @@ -// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC -// SPDX-License-Identifier: Apache-2.0 - -package main - -import ( - "fmt" - - "github.com/spf13/pflag" - "github.com/spf13/viper" - "github.com/xmidt-org/sallust" - "go.uber.org/zap" -) - -func setupFlagSet(fs *pflag.FlagSet) { - fs.StringP("file", "f", "", "the configuration file to use. Overrides the search path.") - fs.BoolP("debug", "d", false, "enables debug logging. Overrides configuration.") - fs.BoolP("version", "v", false, "print version and exit") -} - -func setup(args []string) (*viper.Viper, *zap.Logger, *pflag.FlagSet, error) { - fs := pflag.NewFlagSet(applicationName, pflag.ContinueOnError) - setupFlagSet(fs) - err := fs.Parse(args) - if err != nil { - return nil, nil, fs, fmt.Errorf("failed to create parse args: %w", err) - } - - v := viper.New() - for k, va := range defaults { - v.SetDefault(k, va) - } - - if file, _ := fs.GetString("file"); len(file) > 0 { - v.SetConfigFile(file) - err = v.ReadInConfig() - } else { - v.SetConfigName(applicationName) - v.AddConfigPath(fmt.Sprintf("/etc/%s", applicationName)) - v.AddConfigPath(fmt.Sprintf("$HOME/.%s", applicationName)) - v.AddConfigPath(".") - err = v.ReadInConfig() - } - if err != nil { - return v, nil, fs, fmt.Errorf("failed to read config file: %w", err) - } - - if debug, _ := fs.GetBool("debug"); debug { - v.Set("log.level", "DEBUG") - } - - var c sallust.Config - v.UnmarshalKey("logging", &c) - l := zap.Must(c.Build()) - return v, l, fs, err -}