Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update code layout be to consistent with our newer servers (skeleton layout) #604

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ _testmain.go
*.prof
cpuprofile

tr1d1um
./tr1d1um
.ignore
.vscode
.yaml
Expand Down
71 changes: 18 additions & 53 deletions main.go → cmd/tr1d1um/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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 (
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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(),
Expand All @@ -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(); {
Expand All @@ -230,5 +195,5 @@ func tr1d1um(arguments []string) (exitCode int) {
}

func main() {
os.Exit(tr1d1um(os.Args))
os.Exit(Tr1d1um(os.Args))
}
2 changes: 0 additions & 2 deletions tr1d1um.yaml → cmd/tr1d1um/tr1d1um.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion acquire.go → internal/acquire.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 2 additions & 2 deletions auth.go → internal/auth.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0

package main
package tr1d1um

import (
"errors"
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion basculeLogging.go → internal/basculeLogging.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion basculeLogging_test.go → internal/basculeLogging_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
22 changes: 11 additions & 11 deletions primaryHandler.go → internal/primaryHandler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0

package main
package tr1d1um

import (
"errors"
Expand All @@ -18,18 +18,18 @@ 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"
"go.uber.org/fx"
"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

Expand Down Expand Up @@ -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
}

Expand All @@ -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"`
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
14 changes: 7 additions & 7 deletions routes.go → internal/routes.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0

package main
package tr1d1um

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"time"
Expand All @@ -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"
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading