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

feat: improve stdout ux and run test frontent at /web #55

Merged
merged 5 commits into from
Aug 30, 2024
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
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@ Maybe just deploy it on IPFS and reference it with DNSLink.

For anything other than local testing you're going to want to have a proxy to give you HTTPS support on the Go server.

When deploying to prod, since the addition of telemetry (https://github.com/ipfs/ipfs-check/pull/30) you will also need to run the following before serving the web assets:

```
cd web
npm install && npm run build
```

At a minimum, the following files should be available from your web-server on prod: `web/index.html`, `web/tachyons.min.css`, `web/dist/telemetry.js`.
At a minimum, the following files should be available from your web-server on prod: `web/index.html`, `web/tachyons.min.css`.

## Docker

Expand All @@ -43,15 +36,27 @@ docker run -d ipfs-check

### Terminal 1

```console
$ go build
$ ./ipfs-check
Starting ipfs-check
...
2024/08/29 20:42:34 Please wait, initializing accelerated-dht client.. (mapping Amino DHT may takes 5 or more minutes)
2024/08/29 20:42:34 Accelerated DHT client ready
2024/08/29 20:46:59 Backend ready and listening on [::]:3333
2024/08/29 20:46:59 Test fronted at http://localhost:3333/web/?backendURL=http://localhost:3333
2024/08/29 20:46:59 Ready to start serving.
```
go build
./ipfs-check # Note listening port.. output should say something like "listening on [::]:3333"
```

As a convenience, a test frontend is provided at <http://localhost:3333/web/?backendURL=http://localhost:3333>.

### Terminal 2

If you don't want to use test HTTP server from ipfs-check itself, feel free to
use any other tool to serve the contents of the /web folder (you can open the
html file directly in your browser).

```
# feel free to use any other tool to serve the contents of the /web folder (you can open the html file directly in your browser)
npx -y serve -l 3000 web
# Then open http://localhost:3000?backendURL=http://localhost:3333
```
Expand Down
9 changes: 6 additions & 3 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
return nil, err
}

c, err := connmgr.NewConnManager(600, 900, connmgr.WithGracePeriod(time.Second*30))
c, err := connmgr.NewConnManager(100, 900, connmgr.WithGracePeriod(time.Second*30))

Check warning on line 51 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L51

Added line #L51 was not covered by tests
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -115,11 +115,14 @@
func (d *daemon) mustStart() {
// Wait for the DHT to be ready
if frt, ok := d.dht.(*fullrt.FullRT); ok {
if !frt.Ready() {
log.Printf("Please wait, initializing accelerated-dht client.. (mapping Amino DHT takes 5 mins or more)")

Check warning on line 119 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L118-L119

Added lines #L118 - L119 were not covered by tests
}
for !frt.Ready() {
time.Sleep(time.Second * 10)
time.Sleep(time.Second * 1)

Check warning on line 122 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L122

Added line #L122 was not covered by tests
}
log.Printf("Accelerated DHT client is ready")

Check warning on line 124 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L124

Added line #L124 was not covered by tests
}

}

type cidCheckOutput *[]providerOutput
Expand Down
39 changes: 37 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"context"
"crypto/subtle"
"embed"
"encoding/json"
"errors"
"log"
Expand All @@ -17,6 +18,9 @@
"github.com/urfave/cli/v2"
)

//go:embed web
var webFS embed.FS

func main() {
app := cli.NewApp()
app.Name = name
Expand Down Expand Up @@ -72,11 +76,15 @@

log.Printf("Libp2p host peer id %s\n", d.h.ID())
log.Printf("Libp2p host listening on %v\n", d.h.Addrs())
log.Printf("listening on %v\n", l.Addr())

d.mustStart()

log.Printf("ready to start serving")
log.Printf("Backend ready and listening on %v\n", l.Addr())

webAddr := getWebAddress(l)
log.Printf("Test fronted at http://%s/web/?backendURL=http://%s\n", webAddr, webAddr)
log.Printf("Metrics endpoint at http://%s/metrics\n", webAddr)
log.Printf("Ready to start serving.")

checkHandler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")
Expand Down Expand Up @@ -156,6 +164,14 @@
// Use a single metrics endpoint for all Prometheus metrics
http.Handle("/metrics", BasicAuth(promhttp.HandlerFor(d.promRegistry, promhttp.HandlerOpts{}), metricsUsername, metricPassword))

// Serve frontend on /web
fileServer := http.FileServer(http.FS(webFS))
http.Handle("/web/", fileServer)
// Set up the root route to redirect to /web
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/web", http.StatusFound)
})

Check warning on line 173 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L172-L173

Added lines #L172 - L173 were not covered by tests

done := make(chan error, 1)
go func() {
defer close(done)
Expand Down Expand Up @@ -189,3 +205,22 @@
handler.ServeHTTP(w, r)
})
}

// getWebAddress returns listener with [::] and 0.0.0.0 replaced by localhost
func getWebAddress(l net.Listener) string {
addr := l.Addr().String()
host, port, err := net.SplitHostPort(addr)
if err != nil {
return addr

Check warning on line 214 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L214

Added line #L214 was not covered by tests
}
switch host {
case "":
fallthrough
case "0.0.0.0":
fallthrough

Check warning on line 220 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L217-L220

Added lines #L217 - L220 were not covered by tests
case "::":
return net.JoinHostPort("localhost", port)
default:
return addr

Check warning on line 224 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L223-L224

Added lines #L223 - L224 were not covered by tests
}
}
9 changes: 0 additions & 9 deletions web/dist/telemetry.min.js

This file was deleted.

Loading
Loading