Skip to content

Commit

Permalink
Make use of flag optional in bind
Browse files Browse the repository at this point in the history
Expose an additional function, bind.WithFlag(), which allows callers to
use the previously-default "global flag" mode.

This change allows the bind string parser (etc.) to be used without
unwanted side effects. The behavior of the top-level "goji" package has
not been changed.

Fixes #47.
  • Loading branch information
zenazn committed Jul 21, 2014
1 parent 32da78b commit eab7e2d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
11 changes: 11 additions & 0 deletions bind/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,18 @@ var bind string
func init() {
einhornInit()
systemdInit()
}

// WithFlag adds a standard flag to the global flag instance that allows
// configuration of the default socket. Users who call Default() must call this
// function before flags are parsed, for example in an init() block.
//
// When selecting the default bind string, this function will examine its
// environment for hints about what port to bind to, selecting the GOJI_BIND
// environment variable, Einhorn, systemd, the PORT environment variable, and
// the port 8000, in order. In most cases, this means that the default behavior
// of the default socket will be reasonable for use in your circumstance.
func WithFlag() {
defaultBind := ":8000"
if bind := os.Getenv("GOJI_BIND"); bind != "" {
defaultBind = bind
Expand Down
4 changes: 4 additions & 0 deletions goji.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ import (
"github.com/zenazn/goji/graceful"
)

func init() {
bind.WithFlag()
}

// Serve starts Goji using reasonable defaults.
func Serve() {
if !flag.Parsed() {
Expand Down

0 comments on commit eab7e2d

Please sign in to comment.