prysm-pulse/validator/web/server.go
Raul Jordan a3a77ab5a8
Open Web UI in Browser Automatically (#7481)
* open web

* Update shared/browser/browser_test.go

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>

* Update shared/browser/browser.go

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>

* add to depz.bazel

* run gazelle

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2020-10-09 19:51:46 +00:00

67 lines
1.4 KiB
Go

package web
import (
"context"
"net/http"
"time"
"github.com/prysmaticlabs/prysm/shared"
"github.com/prysmaticlabs/prysm/shared/browser"
"github.com/sirupsen/logrus"
)
var (
_ = shared.Service(&Server{})
log = logrus.WithField("prefix", "prysm-web")
)
// Server for the Prysm Web UI.
type Server struct {
http *http.Server
}
// NewServer creates a server service for the Prysm web UI.
func NewServer(addr string) *Server {
mux := http.NewServeMux()
mux.HandleFunc("/", webHandler)
return &Server{
http: &http.Server{
Addr: addr,
Handler: mux,
},
}
}
// Start the web server.
func (s *Server) Start() {
go func() {
log.WithField("address", s.http.Addr).Info("Starting Prysm web UI")
if err := s.http.ListenAndServe(); err != nil {
log.WithError(err).Error("Failed to start validator web server")
}
}()
time.Sleep(time.Second * 1)
cmd, err := browser.Command("http://" + s.http.Addr)
if err != nil {
log.WithError(err).Errorf("Could not open Prysm web UI in browser")
return
}
if err := cmd.Run(); err != nil {
log.WithError(err).Errorf("Could not open Prysm web UI in browser")
return
}
}
// Stop the web server gracefully with 1s timeout.
func (s *Server) Stop() error {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
return s.http.Shutdown(ctx)
}
// Status check for web server. Always returns nil.
func (s *Server) Status() error {
return nil
}