-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.go
257 lines (217 loc) · 5.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Website dev server (-dev) and prod build/upload tool (-build and -upload)
package main
import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/flashbots/mempool-dumpster/common"
"github.com/flashbots/mempool-dumpster/website"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/css"
"github.com/tdewolff/minify/html"
"go.uber.org/zap"
)
var (
listenAddr = ":8095"
dev = flag.Bool("dev", false, "run dev server")
build = flag.Bool("build", false, "build prod output")
upload = flag.Bool("upload", false, "upload prod output")
outDir = flag.String("out", "./build/website", "where to save output files")
// Helpers
log *zap.SugaredLogger
)
func main() {
flag.Parse()
// Logger setup
log = common.GetLogger(false, false)
defer func() { _ = log.Sync() }()
if *dev {
runDevServer()
} else if *build {
buildWebsite()
} else {
fmt.Println("No action specified -- use either -dev or -build")
flag.Usage()
os.Exit(1)
}
}
func runDevServer() {
log.Infof("Starting webserver on %s", listenAddr)
webserver, err := website.NewDevWebserver(&website.DevWebserverOpts{ //nolint:exhaustruct
ListenAddress: listenAddr,
Log: log,
Dev: *dev,
})
if err != nil {
log.Fatal(err)
}
err = webserver.StartServer()
if err != nil {
log.Fatal(err)
}
}
func buildWebsite() {
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 := website.HTMLData{ //nolint:exhaustruct
Title: "",
Path: "/index.html",
EthMainnetMonths: months,
}
tpl, err := website.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 := website.HTMLData{ //nolint:exhaustruct
Title: month,
Path: fmt.Sprintf("ethereum/mainnet/%s/index.html", month),
CurrentNetwork: "Ethereum Mainnet",
CurrentMonth: month,
Files: files,
}
tpl, err := website.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.Infow("Uploading to S3 ...")
// for _, file := range toUpload {
// fmt.Printf("- %s -> %s\n", file.from, file.to)
// }
for _, file := range toUpload {
app := "./scripts/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/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) ([]website.FileEntry, error) {
files := []website.FileEntry{}
app := "./scripts/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, website.FileEntry{
Filename: filename,
Size: size,
Modified: parts[1] + " " + parts[0],
})
}
}
return files, nil
}