Browse Source

Make use of flag optional in bind

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.
Carl Jackson 12 years ago
parent
commit
eab7e2ddb1
2 changed files with 15 additions and 0 deletions
  1. +11
    -0
      bind/bind.go
  2. +4
    -0
      goji.go

+ 11
- 0
bind/bind.go View File

@ -39,7 +39,18 @@ var bind string
func init() { func init() {
einhornInit() einhornInit()
systemdInit() 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" defaultBind := ":8000"
if bind := os.Getenv("GOJI_BIND"); bind != "" { if bind := os.Getenv("GOJI_BIND"); bind != "" {
defaultBind = bind defaultBind = bind


+ 4
- 0
goji.go View File

@ -44,6 +44,10 @@ import (
"github.com/zenazn/goji/graceful" "github.com/zenazn/goji/graceful"
) )
func init() {
bind.WithFlag()
}
// Serve starts Goji using reasonable defaults. // Serve starts Goji using reasonable defaults.
func Serve() { func Serve() {
if !flag.Parsed() { if !flag.Parsed() {


Loading…
Cancel
Save