Skip to content

Commit

Permalink
Add http/2 server push for index
Browse files Browse the repository at this point in the history
  • Loading branch information
heppu committed Mar 19, 2024
1 parent 933e249 commit 5a0efa1
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"
"time"

"github.com/a-h/templ"
"github.com/gevulotnetwork/devnet-explorer/api/templates"
"github.com/gevulotnetwork/devnet-explorer/model"
)
Expand Down Expand Up @@ -52,7 +51,7 @@ func New(s Store, b *Broadcaster, statsTTL time.Duration) (*API, error) {
return nil, err
}

a.r.Handle("GET /", templ.Handler(templates.Index()))
a.r.HandleFunc("GET /", a.index)
a.r.HandleFunc("GET /api/v1/stream", a.stream)
a.r.HandleFunc("GET /api/v1/stats", a.stats)
a.r.HandleFunc("GET /api/v1/events", a.table)
Expand All @@ -65,6 +64,21 @@ func (a *API) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.r.ServeHTTP(w, r)
}

func (a *API) index(w http.ResponseWriter, r *http.Request) {
// nolint:errcheck
if pusher, ok := w.(http.Pusher); ok {
pusher.Push("/assets/style.css", nil)
pusher.Push("/assets/htmx.min.js", nil)
pusher.Push("/assets/sse.js", nil)
pusher.Push("/assets/Inter-Regular.ttf", nil)
pusher.Push("/assets/Inter-Bold.ttf", nil)
pusher.Push("/assets/Inter-SemiBold.ttf", nil)
}
if err := templates.Index().Render(r.Context(), w); err != nil {
slog.Error("failed to render index", slog.Any("err", err))
}
}

func (a *API) stats(w http.ResponseWriter, r *http.Request) {
if time.Since(a.st.updated) > time.Second*5 {
var err error
Expand Down

0 comments on commit 5a0efa1

Please sign in to comment.