Skip to content

Commit

Permalink
website
Browse files Browse the repository at this point in the history
  • Loading branch information
metachris committed Jun 12, 2024
1 parent d508488 commit d9ec5db
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
/relayscan
/deploy*
/test.csv
/csv/
/csv/
/build/
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,9 @@ docker-image:
generate-ssz:
rm -f common/ultrasoundbid_encoding.go
sszgen --path common --objs UltrasoundStreamBid

bids-website:
go run . service bidcollect --build-website --build-website-upload

bids-website-dev:
go run . service bidcollect --devserver
15 changes: 15 additions & 0 deletions cmd/service/bidcollect.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ var (

runDevServerOnly bool // used to play with file listing website
devServerListenAddr = ":8095"

buildWebsite bool
buildWebsiteUpload bool
buildWebsiteOutDir string
)

func init() {
Expand All @@ -40,6 +44,11 @@ func init() {

// for dev purposes
bidCollectCmd.Flags().BoolVar(&runDevServerOnly, "devserver", false, "only run devserver to play with file listing website")

// building the S3 website
bidCollectCmd.Flags().BoolVar(&buildWebsite, "build-website", false, "build file listing website")
bidCollectCmd.Flags().BoolVar(&buildWebsiteUpload, "build-website-upload", false, "upload after building")
bidCollectCmd.Flags().StringVar(&buildWebsiteOutDir, "build-website-out", "build", "output directory for website")
}

var bidCollectCmd = &cobra.Command{
Expand All @@ -52,6 +61,12 @@ var bidCollectCmd = &cobra.Command{
return
}

if buildWebsite {
log.Infof("Bidcollect %s building website (output: %s) ...", vars.Version, buildWebsiteOutDir)
website.BuildProdWebsite(log, buildWebsiteOutDir, buildWebsiteUpload)
return
}

log.Infof("Bidcollect starting (%s) ...", vars.Version)

// Prepare relays
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ echo "upload for: $d"

# change to project root directory
cd "$(dirname "$0")"
cd ..
cd ../../

# load environment variables
source .env.prod

# archive and upload!
./scripts/bids-combine-and-upload.sh "/mnt/data/relayscan-bids/$d/"
./scripts/bidcollect/bids-combine-and-upload.sh "/mnt/data/relayscan-bids/$d/"

# update website
# make website
make bids-website
File renamed without changes.
8 changes: 8 additions & 0 deletions scripts/bidcollect/s3/get-files.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
# require one argument
if [ $# -ne 1 ]; then
echo "Usage: $0 <folder>"
exit 1
fi

aws --profile r2 s3 ls s3://relayscan-bidarchive/$1 --endpoint-url "https://${CLOUDFLARE_R2_ACCOUNT_ID}.r2.cloudflarestorage.com"
2 changes: 2 additions & 0 deletions scripts/bidcollect/s3/get-folders.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
aws --profile r2 s3 ls s3://relayscan-bidarchive/$1 --endpoint-url "https://${CLOUDFLARE_R2_ACCOUNT_ID}.r2.cloudflarestorage.com" | awk '{ print $2 }'
16 changes: 16 additions & 0 deletions scripts/bidcollect/s3/upload-file-to-r2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
src=$1
target=$2
if [ -z "$src" ]; then
echo "Usage: $0 <local_file> [<s3_target>"]
exit 1
fi

# auto-fill target if not given
if [ -z "$target" ]; then
# remove "/mnt/data/relayscan-bidarchive/" prefix from src and make it the S3 prefix
target="/ethereum/mainnet/${src#"/mnt/data/relayscan-bidarchive/"}"
fi

echo "uploading $src to S3 $target ..."
aws --profile r2 s3 cp $src s3://relayscan-bidarchive$target --endpoint-url "https://${CLOUDFLARE_R2_ACCOUNT_ID}.r2.cloudflarestorage.com"
211 changes: 211 additions & 0 deletions services/bidcollect/website/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package website

//
// Quick and dirty website generator
//

import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"

"github.com/sirupsen/logrus"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/css"
"github.com/tdewolff/minify/html"
)

func BuildProdWebsite(log *logrus.Entry, outDir string, upload bool) {
log.Infof("Creating build server in %s", outDir)
err := os.MkdirAll(outDir, os.ModePerm)
if err != nil {
log.Fatal(err)
}

dir := "ethereum/mainnet/"

// Setup minifier
minifier := minify.New()
minifier.AddFunc("text/html", html.Minify)
minifier.AddFunc("text/css", css.Minify)

// Load month folders from S3
log.Infof("Getting folders from S3 for %s ...", dir)
months, err := getFoldersFromS3(dir)
if err != nil {
log.Fatal(err)
}
fmt.Println("Months:", months)

// build root page
log.Infof("Building root page ...")
rootPageData := HTMLData{ //nolint:exhaustruct
Title: "",
Path: "/index.html",
EthMainnetMonths: months,
}

tpl, err := ParseIndexTemplate()
if err != nil {
log.Fatal(err)
}

buf := new(bytes.Buffer)
err = tpl.ExecuteTemplate(buf, "base", rootPageData)
if err != nil {
log.Fatal(err)
}

// minify
mBytes, err := minifier.Bytes("text/html", buf.Bytes())
if err != nil {
log.Fatal(err)
}

// write to file
fn := filepath.Join(outDir, "index.html")
log.Infof("Writing to %s ...", fn)
err = os.WriteFile(fn, mBytes, 0o0600)
if err != nil {
log.Fatal(err)
}

toUpload := []struct{ from, to string }{
{fn, "/"},
}

// build files pages
for _, month := range months {
dir := "ethereum/mainnet/" + month + "/"
log.Infof("Getting files from S3 for %s ...", dir)
files, err := getFilesFromS3(dir)
if err != nil {
log.Fatal(err)
}

rootPageData := HTMLData{ //nolint:exhaustruct
Title: month,
Path: fmt.Sprintf("ethereum/mainnet/%s/index.html", month),

CurrentNetwork: "Ethereum Mainnet",
CurrentMonth: month,
Files: files,
}

tpl, err := ParseFilesTemplate()
if err != nil {
log.Fatal(err)
}

buf := new(bytes.Buffer)
err = tpl.ExecuteTemplate(buf, "base", rootPageData)
if err != nil {
log.Fatal(err)
}

// minify
mBytes, err := minifier.Bytes("text/html", buf.Bytes())
if err != nil {
log.Fatal(err)
}

// write to file
_outDir := filepath.Join(outDir, dir)
err = os.MkdirAll(_outDir, os.ModePerm)
if err != nil {
log.Fatal(err)
}

fn := filepath.Join(_outDir, "index.html")
log.Infof("Writing to %s ...", fn)
err = os.WriteFile(fn, mBytes, 0o0600)
if err != nil {
log.Fatal(err)
}

toUpload = append(toUpload, struct{ from, to string }{fn, "/" + dir})
}

if upload {
log.Info("Uploading to S3 ...")
// for _, file := range toUpload {
// fmt.Printf("- %s -> %s\n", file.from, file.to)
// }

for _, file := range toUpload {
app := "./scripts/bidcollect/s3/upload-file-to-r2.sh"
cmd := exec.Command(app, file.from, file.to) //nolint:gosec
stdout, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
fmt.Println(string(stdout))
}
}
}

func getFoldersFromS3(dir string) ([]string, error) {
folders := []string{}

app := "./scripts/bidcollect/s3/get-folders.sh"
cmd := exec.Command(app, dir)
stdout, err := cmd.Output()
if err != nil {
return folders, err
}

// Print the output
lines := strings.Split(string(stdout), "\n")
for _, line := range lines {
if line != "" && strings.HasPrefix(line, "20") {
folders = append(folders, strings.TrimSuffix(line, "/"))
}
}
return folders, nil
}

func getFilesFromS3(month string) ([]FileEntry, error) {
files := []FileEntry{}

app := "./scripts/bidcollect/s3/get-files.sh"
cmd := exec.Command(app, month)
stdout, err := cmd.Output()
if err != nil {
return files, err
}

space := regexp.MustCompile(`\s+`)
lines := strings.Split(string(stdout), "\n")
for _, line := range lines {
if line != "" {
line = space.ReplaceAllString(line, " ")
parts := strings.Split(line, " ")

// parts[2] is the size
size, err := strconv.ParseUint(parts[2], 10, 64)
if err != nil {
return files, err
}

filename := parts[3]

if filename == "index.html" {
continue
} else if strings.HasSuffix(filename, ".csv.gz") {
continue
}

files = append(files, FileEntry{
Filename: filename,
Size: size,
Modified: parts[1] + " " + parts[0],
})
}
}
return files, nil
}
26 changes: 13 additions & 13 deletions services/bidcollect/website/templates/base.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{ define "base" }}

{{ $title:="Mempool Dumpster ♻️" }}
{{ $title:="Relayscan Bidarchive 📚" }}

{{ if ne .Title "" }}
{{ $title = (printf "%v | %v" .Title $title) }}
Expand All @@ -24,22 +24,22 @@

<!-- HTML Meta Tags -->
<title>{{ $title }}</title>
<meta name="description" content="Free Ethereum mempool transactions archive, brought to you by Flashbots ⚡️🤖 and friends 💫">
<meta name="description" content="Free Ethereum mempool transactions archive, brought to you by Flashbots ⚡️🤖">

<!-- Facebook Meta Tags -->
<meta property="og:url" content="https://mempool-dumpster.flashbots.net{{ .Path }}">
<meta property="og:url" content="https://relayscan-bidarchive.flashbots.net{{ .Path }}">
<meta property="og:type" content="website">
<meta property="og:title" content="{{ $title }}">
<meta property="og:description" content="Free Ethereum mempool transactions archive, brought to you by Flashbots ⚡️🤖 and friends 💫">
<meta property="og:image" content="https://mempool-dumpster.flashbots.net/static/favicon/android-chrome-512x512.png">
<meta property="og:description" content="Free MEV-Boost Bid Archive, brought to you by Flashbots ⚡️🤖">
<meta property="og:image" content="https://relayscan-bidarchive.flashbots.net/static/favicon/android-chrome-512x512.png">

<!-- Twitter Meta Tags -->
<meta name="twitter:card" content="summary">
<meta property="twitter:domain" content="mempool-dumpster.flashbots.net">
<meta property="twitter:url" content="https://mempool-dumpster.flashbots.net{{ .Path }}">
<meta property="twitter:domain" content="relayscan-bidarchive.flashbots.net">
<meta property="twitter:url" content="https://relayscan-bidarchive.flashbots.net{{ .Path }}">
<meta name="twitter:title" content="{{ $title }}">
<meta name="twitter:description" content="Free Ethereum mempool transactions archive, brought to you by Flashbots ⚡️🤖 and friends 💫">
<meta name="twitter:image" content="https://mempool-dumpster.flashbots.net/static/favicon/android-chrome-512x512.png">
<meta name="twitter:description" content="Free MEV-Boost Bid Archive, brought to you by Flashbots ⚡️🤖">
<meta name="twitter:image" content="https://relayscan-bidarchive.flashbots.net/static/favicon/android-chrome-512x512.png">

<style>
body {
Expand Down Expand Up @@ -100,9 +100,9 @@
}
</style>
<script type="text/javascript">
if (window.location.host.indexOf("r2.dev") > -1) {
window.location = "https://mempool-dumpster.flashbots.net" + window.location.pathname + window.location.search + window.location.hash;
}
// if (window.location.host.indexOf("r2.dev") > -1) {
// window.location = "https://relayscan-bidarchive.flashbots.net" + window.location.pathname + window.location.search + window.location.hash;
// }
</script>
</head>

Expand All @@ -111,7 +111,7 @@
<a href="https://collective.flashbots.net/">
<img style="float:right; background:white; margin-left: 64px; width: 100px; height: 100px;" src="https://d33wubrfki0l68.cloudfront.net/ae8530415158fbbbbe17fb033855452f792606c7/fe19f/img/logo.png">
</a>
<h1>Relayscan Bidarchive</h1>
<h1>{{ $title }}</h1>
<p>
<a href="https://github.com/flashbots/relayscan">https://github.com/flashbots/relayscan</a>
</p>
Expand Down

0 comments on commit d9ec5db

Please sign in to comment.