Skip to content

Commit

Permalink
refactor: update code layout be to consistent with our newer servers …
Browse files Browse the repository at this point in the history
…(skeleton layout)
  • Loading branch information
denopink committed Dec 18, 2024
1 parent 374af5f commit 8413214
Show file tree
Hide file tree
Showing 33 changed files with 137 additions and 740 deletions.
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_test

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
95 changes: 95 additions & 0 deletions internal/setup.go
Original file line number Diff line number Diff line change
@@ -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
}
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions stat/mocks_test.go → internal/stat/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion stat/service.go → internal/stat/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion stat/service_test.go → internal/stat/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion stat/transport.go → internal/stat/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion stat/transport_test.go → internal/stat/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion translation/errors.go → internal/translation/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion translation/service.go → internal/translation/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"bytes"
"context"
"errors"
"io/ioutil"
"io"
"net/http"
"testing"

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading

0 comments on commit 8413214

Please sign in to comment.