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

chore: run golines #40

Merged
merged 1 commit into from
Oct 26, 2023
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
34 changes: 29 additions & 5 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ func Start(cfg *config.Config) error {
router.POST("/api/submit/tx", handleSubmitTx)

// Start listener
err := router.Run(fmt.Sprintf("%s:%d", cfg.Api.ListenAddress, cfg.Api.ListenPort))
err := router.Run(
fmt.Sprintf("%s:%d", cfg.Api.ListenAddress, cfg.Api.ListenPort),
)
return err
}

Expand Down Expand Up @@ -88,16 +90,28 @@ func handleSubmitTx(c *gin.Context) {
clientTrace := &httptrace.ClientTrace{
GotConn: func(info httptrace.GotConnInfo) { connReused = info.Reused },
}
traceCtx := httptrace.WithClientTrace(context.Background(), clientTrace)
req, err := http.NewRequestWithContext(traceCtx, http.MethodPost, backend, body)
traceCtx := httptrace.WithClientTrace(
context.Background(),
clientTrace,
)
req, err := http.NewRequestWithContext(
traceCtx,
http.MethodPost,
backend,
body,
)
if err != nil {
logger.Errorf("failed to create request: %s", err)
return
}
req.Header.Add("Content-Type", "application/cbor")
resp, err := http.DefaultClient.Do(req)
if err != nil {
logger.Errorf("failed to send request to backend %s: %s", backend, err)
logger.Errorf(
"failed to send request to backend %s: %s",
backend,
err,
)
return
}
elapsedTime := time.Since(startTime)
Expand All @@ -109,7 +123,17 @@ func handleSubmitTx(c *gin.Context) {
}
defer resp.Body.Close()
if resp.StatusCode == 202 {
logger.Infow(fmt.Sprintf("successfully submitted transaction %s to backend %s", tx.Hash(), backend), "latency", elapsedTime.Seconds(), "connReused", connReused)
logger.Infow(
fmt.Sprintf(
"successfully submitted transaction %s to backend %s",
tx.Hash(),
backend,
),
"latency",
elapsedTime.Seconds(),
"connReused",
connReused,
)
} else {
logger.Errorw(fmt.Sprintf("failed to send request to backend %s: got response %d, %s", backend, resp.StatusCode, string(respBody)), "latency", elapsedTime.Seconds(), "connReused", connReused)
}
Expand Down
13 changes: 11 additions & 2 deletions cmd/tx-submit-api-mirror/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ var cmdlineFlags struct {
}

func main() {
flag.StringVar(&cmdlineFlags.configFile, "config", "", "path to config file to load")
flag.StringVar(
&cmdlineFlags.configFile,
"config",
"",
"path to config file to load",
)
flag.Parse()

// Load config
Expand All @@ -38,7 +43,11 @@ func main() {
}()

// Start API listener
logger.Infof("starting API listener on %s:%d", cfg.Api.ListenAddress, cfg.Api.ListenPort)
logger.Infof(
"starting API listener on %s:%d",
cfg.Api.ListenAddress,
cfg.Api.ListenPort,
)
if err := api.Start(cfg); err != nil {
logger.Fatalf("failed to start API: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type LoggingConfig struct {

type ApiConfig struct {
ListenAddress string `yaml:"address" envconfig:"API_LISTEN_ADDRESS"`
ListenPort uint `yaml:"port" envconfig:"API_LISTEN_PORT"`
ListenPort uint `yaml:"port" envconfig:"API_LISTEN_PORT"`
}

// Singleton config instance with default values
Expand Down
8 changes: 6 additions & 2 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ func Setup(cfg *config.LoggingConfig) {
// Change timestamp key name
loggerConfig.EncoderConfig.TimeKey = "timestamp"
// Use a human readable time format
loggerConfig.EncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout(time.RFC3339)
loggerConfig.EncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout(
time.RFC3339,
)

// Set level
if cfg.Level != "" {
Expand Down Expand Up @@ -50,5 +52,7 @@ func GetDesugaredLogger() *zap.Logger {
}

func GetAccessLogger() *zap.Logger {
return globalLogger.Desugar().With(zap.String("type", "access")).WithOptions(zap.WithCaller(false))
return globalLogger.Desugar().
With(zap.String("type", "access")).
WithOptions(zap.WithCaller(false))
}
Loading