Skip to content

Commit

Permalink
feat: Random HTTP port support (port=0) and disable HTTP support (por…
Browse files Browse the repository at this point in the history
…t=-1)
  • Loading branch information
szkiba committed Aug 15, 2023
1 parent 6d7aef6 commit 5e49cf7
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ The following parameters are recognized:
parameter | description
----------|------------
host | Hostname or IP address for HTTP endpoint (default: "", empty, listen on all interfaces)
port | TCP port for HTTP endpoint (default: `5665`), example: `8080`
port | TCP port for HTTP endpoint (default: `5665`; `0` = random, `-1` = no HTTP), example: `8080`
period | Event emitting frequency (default: `10s`), example: `1m`
open | Set to `true` (or empty) for opening browser window automatically
config | UI configuration file location (default: `.dashboard.js`) (see [Customization](#customization))
Expand Down
2 changes: 1 addition & 1 deletion dashboard/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func buildRootCmd(opts *options, uiFS fs.FS) *cobra.Command {
opts = new(options)

flags.StringVar(&opts.Host, flagHost, defaultHost, "Hostname or IP address for HTTP endpoint (default: '', empty, listen on all interfaces)")
flags.IntVar(&opts.Port, flagPort, defaultPort, "TCP port for HTTP endpoint (default: 5665), example: 8080")
flags.IntVar(&opts.Port, flagPort, defaultPort, "TCP port for HTTP endpoint (default: 5665; 0=random, -1=no HTTP), example: 8080")
flags.DurationVar(&opts.Period, flagPeriod, defaultPeriod, "Event emitting frequency (default: `10s`), example: `1m`")
flags.BoolVar(&opts.Open, flagOpen, defaultOpen, "Open browser window automatically")
flags.StringVar(&opts.Config, flagConfig, defaultHost, "UI configuration file location (default: '.dashboard.js')")
Expand Down
23 changes: 15 additions & 8 deletions dashboard/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Extension struct {

cumulative *meter

description string
name string

briefFS fs.FS
}
Expand All @@ -49,7 +49,7 @@ func New(params output.Params, uiFS fs.FS, briefFS fs.FS) (*Extension, error) {
briefFS: briefFS,
logger: params.Logger,
options: opts,
description: fmt.Sprintf("%s (%s) %s", params.OutputType, opts.addr(), opts.url()),
name: params.OutputType,
buffer: nil,
server: nil,
flusher: nil,
Expand All @@ -61,7 +61,11 @@ func New(params output.Params, uiFS fs.FS, briefFS fs.FS) (*Extension, error) {
}

func (ext *Extension) Description() string {
return ext.description
if ext.options.Port < 0 {
return ext.name
}

return fmt.Sprintf("%s (%s) %s", ext.name, ext.options.addr(), ext.options.url())
}

func (ext *Extension) Start() error {
Expand All @@ -85,11 +89,14 @@ func (ext *Extension) Start() error {
return err
}

go func() {
if err := ext.server.listenAndServe(ext.options.addr()); err != nil {
ext.logger.Error(err)
}
}()
addr, err := ext.server.listenAndServe(ext.options.addr())
if err != nil {
return err
}

if ext.options.Port == 0 {
ext.options.Port = addr.Port
}

ext.buffer = new(output.SampleBuffer)

Expand Down
4 changes: 4 additions & 0 deletions dashboard/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func (opts *options) config() ([]byte, error) {
}

func (opts *options) addr() string {
if opts.Port < 0 {
return ""
}

return net.JoinHostPort(opts.Host, strconv.Itoa(opts.Port))
}

Expand Down
7 changes: 6 additions & 1 deletion dashboard/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ func replay(opts *options, uiFS fs.FS, filename string) error {
rep.eventSource = new(eventSource)
rep.addEventListener(rep.server)

if err := rep.server.listenAndServe(rep.options.addr()); err != nil {
addr, err := rep.server.listenAndServe(rep.options.addr())
if err != nil {
return err
}

if rep.options.Port == 0 {
rep.options.Port = addr.Port
}

if rep.options.Open {
browser.OpenURL(rep.options.url()) // nolint:errcheck
}
Expand Down
8 changes: 5 additions & 3 deletions dashboard/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ func newWebServer(uiFS fs.FS, uiConfig []byte, logger logrus.FieldLogger) *webSe
return srv
}

func (srv *webServer) listenAndServe(addr string) error {
func (srv *webServer) listenAndServe(addr string) (*net.TCPAddr, error) {
listener, err := net.Listen("tcp", addr)
if err != nil {
return err
return nil, err
}

go func() {
Expand All @@ -55,7 +55,9 @@ func (srv *webServer) listenAndServe(addr string) error {
}
}()

return nil
a, _ := listener.Addr().(*net.TCPAddr)

return a, nil
}

func rootHandler(uiPath string) http.HandlerFunc {
Expand Down
12 changes: 9 additions & 3 deletions dashboard/web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ func Test_newWebServer(t *testing.T) {

addr := getRandomAddr(t)

assert.NoError(t, srv.listenAndServe(addr))
_, err := srv.listenAndServe(addr)

assert.NoError(t, err)

base := "http://" + addr

Expand Down Expand Up @@ -57,8 +59,12 @@ func Test_webServer_used_addr(t *testing.T) {

addr := getRandomAddr(t)

assert.NoError(t, srv.listenAndServe(addr))
assert.Error(t, srv.listenAndServe(addr))
_, err := srv.listenAndServe(addr)
assert.NoError(t, err)

_, err = srv.listenAndServe(addr)

assert.Error(t, err)
}

func Test_uiHandler_no_config(t *testing.T) {
Expand Down

0 comments on commit 5e49cf7

Please sign in to comment.