Skip to content

Commit

Permalink
Merge pull request #55 from ipfs/chore/remove-npm-build
Browse files Browse the repository at this point in the history
feat: improve stdout ux and run test frontent at /web
  • Loading branch information
2color authored Aug 30, 2024
2 parents efc30fe + 4719503 commit 2f48510
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 2,566 deletions.
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 @@ func newDaemon(ctx context.Context, acceleratedDHT bool) (*daemon, error) {
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))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -115,11 +115,14 @@ func newDaemon(ctx context.Context, acceleratedDHT bool) (*daemon, error) {
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)")
}
for !frt.Ready() {
time.Sleep(time.Second * 10)
time.Sleep(time.Second * 1)
}
log.Printf("Accelerated DHT client is ready")
}

}

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 @@ package main
import (
"context"
"crypto/subtle"
"embed"
"encoding/json"
"errors"
"log"
Expand All @@ -17,6 +18,9 @@ import (
"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 @@ func startServer(ctx context.Context, d *daemon, tcpListener, metricsUsername, m

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 @@ func startServer(ctx context.Context, d *daemon, tcpListener, metricsUsername, m
// 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)
})

done := make(chan error, 1)
go func() {
defer close(done)
Expand Down Expand Up @@ -189,3 +205,22 @@ func BasicAuth(handler http.Handler, username, password string) http.Handler {
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
}
switch host {
case "":
fallthrough
case "0.0.0.0":
fallthrough
case "::":
return net.JoinHostPort("localhost", port)
default:
return addr
}
}
9 changes: 0 additions & 9 deletions web/dist/telemetry.min.js

This file was deleted.

Loading

0 comments on commit 2f48510

Please sign in to comment.