From eab7e2ddb1fa8a42a6b23c827046621d4d9629f9 Mon Sep 17 00:00:00 2001 From: Carl Jackson Date: Mon, 21 Jul 2014 11:40:54 -0700 Subject: [PATCH] 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. --- bind/bind.go | 11 +++++++++++ goji.go | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/bind/bind.go b/bind/bind.go index b8ff22a..9228f5a 100644 --- a/bind/bind.go +++ b/bind/bind.go @@ -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 diff --git a/goji.go b/goji.go index b68b93f..5f2d61c 100644 --- a/goji.go +++ b/goji.go @@ -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() {