Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
metachris committed Sep 21, 2024
1 parent 11ef5e7 commit 3f80c37
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
# id: meta
# uses: docker/metadata-action@v5
# with:
# images: flashbots/go-template
# images: flashbots/builder-tools
# tags: |
# type=sha
# type=pep440,pattern={{version}}
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ clean:
.PHONY: build-cli
build-cli:
@mkdir -p ./build
go build -trimpath -ldflags "-X github.com/flashbots/go-template/common.Version=${VERSION}" -v -o ./build/cli cmd/cli/main.go
go build -trimpath -ldflags "-X github.com/flashbots/builder-tools/common.Version=${VERSION}" -v -o ./build/cli cmd/cli/main.go

.PHONY: build-httpserver
build-httpserver:
@mkdir -p ./build
go build -trimpath -ldflags "-X github.com/flashbots/go-template/common.Version=${VERSION}" -v -o ./build/httpserver cmd/httpserver/main.go
go build -trimpath -ldflags "-X github.com/flashbots/builder-tools/common.Version=${VERSION}" -v -o ./build/httpserver cmd/httpserver/main.go

.PHONY: test
test:
Expand All @@ -36,7 +36,7 @@ lint:
go vet ./...
staticcheck ./...
golangci-lint run
nilaway ./...
# nilaway ./...

.PHONY: fmt
fmt:
Expand Down
16 changes: 4 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,10 @@ $ go run cmd/status-api/*
$ echo 111 > pipe.fifo
$ curl localhost:8082/api/v1/new_event?message=222

# Query events
$ curl -s localhost:8082/api/v1/events | jq
[
{
"ReceivedAt": "2024-09-21T07:51:47.709512Z",
"Message": "111"
},
{
"ReceivedAt": "2024-09-21T07:51:49.294175Z",
"Message": "222"
}
]
# Query events (timestamp in UTC)
$ curl -s localhost:8082/api/v1/events | jq -r '(.[] | [.received_at, .message]) | @tsv'
2024-09-21T14:35:37.674863Z 111
2024-09-21T14:35:42.486016Z 222
```


Expand Down
2 changes: 1 addition & 1 deletion cmd/ecdsa-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/flashbots/go-template/common"
"github.com/flashbots/builder-tools/common"
cli "github.com/urfave/cli/v2" // imports as package "cli"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/https-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"net/http"
"os"

"github.com/flashbots/go-template/common"
"github.com/flashbots/builder-tools/common"
cli "github.com/urfave/cli/v2"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/https-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os"
"time"

"github.com/flashbots/go-template/common"
"github.com/flashbots/builder-tools/common"
cli "github.com/urfave/cli/v2" // imports as package "cli"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/status-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"
"os"

"github.com/flashbots/go-template/common"
"github.com/flashbots/builder-tools/common"
cli "github.com/urfave/cli/v2" // imports as package "cli"
)

Expand Down
8 changes: 3 additions & 5 deletions cmd/status-api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"syscall"
"time"

"github.com/flashbots/go-utils/httplogger"
"github.com/flashbots/builder-tools/common"
chi "github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
Expand Down Expand Up @@ -56,7 +56,7 @@ func NewServer(cfg *HTTPServerConfig) (srv *Server, err error) {

if cfg.PipeFilename != "" {
os.Remove(cfg.PipeFilename)
err := syscall.Mkfifo(cfg.PipeFilename, 0o666)
err := syscall.Mknod(cfg.PipeFilename, syscall.S_IFIFO|0o666, 0)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -108,7 +108,7 @@ func (s *Server) readPipeInBackground() {
}

func (s *Server) httpLogger(next http.Handler) http.Handler {
return httplogger.LoggingMiddlewareSlog(s.log, next)
return common.LoggingMiddlewareSlog(s.log, next)
}

func (s *Server) Start() {
Expand Down Expand Up @@ -154,6 +154,4 @@ func (s *Server) handleGetEvents(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
}
67 changes: 67 additions & 0 deletions common/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
package common

import (
"fmt"
"log/slog"
"net/http"
"os"
"runtime/debug"
"strconv"
"time"
)

type LoggingOpts struct {
Expand Down Expand Up @@ -35,3 +40,65 @@ func SetupLogger(opts *LoggingOpts) (log *slog.Logger) {

return log
}

// LoggingMiddlewareSlog logs the incoming HTTP request & its duration.
func LoggingMiddlewareSlog(logger *slog.Logger, next http.Handler) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
w.WriteHeader(http.StatusInternalServerError)

method := ""
url := ""
if r != nil {
method = r.Method
url = r.URL.EscapedPath()
}

logger.Error(fmt.Sprintf("http request panic: %s %s", method, url),
"err", err,
"trace", string(debug.Stack()),
"method", r.Method,
)
}
}()
start := time.Now()
wrapped := wrapResponseWriter(w)
next.ServeHTTP(wrapped, r)
logger.Info(fmt.Sprintf("http: %s %s %d", r.Method, r.URL.EscapedPath(), wrapped.status),
"status", wrapped.status,
"method", r.Method,
"path", r.URL.EscapedPath(),
"duration", fmt.Sprintf("%f", time.Since(start).Seconds()),
"durationUs", strconv.FormatInt(time.Since(start).Microseconds(), 10),
)
},
)
}

// responseWriter is a minimal wrapper for http.ResponseWriter that allows the
// written HTTP status code to be captured for logging.
type responseWriter struct {
http.ResponseWriter
status int
wroteHeader bool
}

func wrapResponseWriter(w http.ResponseWriter) *responseWriter {
return &responseWriter{ResponseWriter: w}
}

func (rw *responseWriter) Status() int {
return rw.status
}

func (rw *responseWriter) WriteHeader(code int) {
if rw.wroteHeader {
return
}

rw.status = code
rw.ResponseWriter.WriteHeader(code)
rw.wroteHeader = true
}
2 changes: 1 addition & 1 deletion common/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package common
var Version = "dev"

const (
PackageName = "github.com/flashbots/go-template"
PackageName = "github.com/flashbots/builder-tools"
)
7 changes: 1 addition & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
module github.com/flashbots/go-template
module github.com/flashbots/builder-tools

go 1.22

toolchain go1.23.1

require (
github.com/ethereum/go-ethereum v1.14.9
github.com/flashbots/go-utils v0.6.1-0.20240611151222-14d18c5e94d0
github.com/go-chi/chi/v5 v5.1.0
github.com/urfave/cli/v2 v2.27.2
)
Expand All @@ -15,13 +14,9 @@ require (
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/holiman/uint256 v1.3.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/sys v0.25.0 // indirect
)
24 changes: 0 additions & 24 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOF
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5ilcvdfma9wOH6Y=
Expand All @@ -13,40 +12,17 @@ github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etly
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
github.com/ethereum/go-ethereum v1.14.9 h1:J7iwXDrtUyE9FUjUYbd4c9tyzwMh6dTJsKzo9i6SrwA=
github.com/ethereum/go-ethereum v1.14.9/go.mod h1:QeW+MtTpRdBEm2pUFoonByee8zfHv7kGp0wK0odvU1I=
github.com/flashbots/go-utils v0.6.1-0.20240611151222-14d18c5e94d0 h1:aJ+jlJVI8p8Wnj/+o6Ds/URwMg0d/+msQB7RgrxacOA=
github.com/flashbots/go-utils v0.6.1-0.20240611151222-14d18c5e94d0/go.mod h1:l5jkDHCCt225ohwNtqafEJSKV7PfrFfXb2JT145I1ms=
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/holiman/uint256 v1.3.1 h1:JfTzmih28bittyHM8z360dCjIA9dbPIBlcTI6lmctQs=
github.com/holiman/uint256 v1.3.1/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 3f80c37

Please sign in to comment.