Files
prysm/tools/http-request-sink/main.go
Preston Van Loon 2fd6bd8150 Add golang.org/x/tools modernize static analyzer and fix violations (#15946)
* Ran gopls modernize to fix everything

go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...

* Override rules_go provided dependency for golang.org/x/tools to v0.38.0.

To update this, checked out rules_go, then ran `bazel run //go/tools/releaser -- upgrade-dep -mirror=false org_golang_x_tools` and copied the patches.

* Fix buildtag violations and ignore buildtag violations in external

* Introduce modernize analyzer package.

* Add modernize "any" analyzer.

* Fix violations of any analyzer

* Add modernize "appendclipped" analyzer.

* Fix violations of appendclipped

* Add modernize "bloop" analyzer.

* Add modernize "fmtappendf" analyzer.

* Add modernize "forvar" analyzer.

* Add modernize "mapsloop" analyzer.

* Add modernize "minmax" analyzer.

* Fix violations of minmax analyzer

* Add modernize "omitzero" analyzer.

* Add modernize "rangeint" analyzer.

* Fix violations of rangeint.

* Add modernize "reflecttypefor" analyzer.

* Fix violations of reflecttypefor analyzer.

* Add modernize "slicescontains" analyzer.

* Add modernize "slicessort" analyzer.

* Add modernize "slicesdelete" analyzer. This is disabled by default for now. See https://go.dev/issue/73686.

* Add modernize "stringscutprefix" analyzer.

* Add modernize "stringsbuilder" analyzer.

* Fix violations of stringsbuilder analyzer.

* Add modernize "stringsseq" analyzer.

* Add modernize "testingcontext" analyzer.

* Add modernize "waitgroup" analyzer.

* Changelog fragment

* gofmt

* gazelle

* Add modernize "newexpr" analyzer.

* Disable newexpr until go1.26

* Add more details in WORKSPACE on how to update the override

* @nalepae feedback on min()

* gofmt

* Fix violations of forvar
2025-11-14 01:27:22 +00:00

82 lines
1.9 KiB
Go

// Package main implements a simple, http-request-sink which writes
// incoming http request bodies to an append-only text file at a specified directory.
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
"github.com/OffchainLabs/prysm/v7/config/params"
)
func main() {
port := flag.Int("port", 8080, "port to listen on")
writeDirPath := flag.String("write-dir", "", "directory to write an append-only file")
flag.Parse()
if *writeDirPath == "" {
log.Fatal("Needs a -write-dir path")
}
// If the file doesn't exist, create it, or append to the file.
f, err := os.OpenFile(
filepath.Join(*writeDirPath, "requests.log"),
os.O_APPEND|os.O_CREATE|os.O_RDWR,
params.BeaconIoConfig().ReadWritePermissions,
)
if err != nil {
log.Println(err)
}
defer func() {
if err = f.Close(); err != nil {
log.Fatal(err)
}
}()
http.HandleFunc("/", func(writer http.ResponseWriter, r *http.Request) {
reqContent := map[string]any{}
if err = parseRequest(r, &reqContent); err != nil {
log.Println(err)
}
log.Printf("Capturing request from %s", r.RemoteAddr)
if err = captureRequest(f, reqContent); err != nil {
log.Println(err)
}
})
log.Printf("Listening on port %d", *port)
srv := &http.Server{
Addr: ":" + strconv.Itoa(*port),
ReadHeaderTimeout: 3 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}
func captureRequest(f *os.File, m map[string]any) error {
enc, err := json.Marshal(m)
if err != nil {
return err
}
_, err = f.WriteString(fmt.Sprintf("%s\n", enc))
return err
}
func parseRequest(req *http.Request, unmarshalStruct any) error {
body, err := io.ReadAll(req.Body)
if err != nil {
return err
}
if err = req.Body.Close(); err != nil {
return err
}
req.Body = io.NopCloser(bytes.NewBuffer(body))
return json.Unmarshal(body, unmarshalStruct)
}