Skip to content

Commit

Permalink
add Serve* methods to Mux
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed May 20, 2016
1 parent 3f87efb commit 250b4ba
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions serve.go
Expand Up @@ -20,13 +20,16 @@ func init() {
}

// Serve starts kami with reasonable defaults.
// It works (exactly) like Goji, looking for Einhorn, the bind flag, GOJI_BIND...
// The bind address can be changed by setting the GOJI_BIND environment variable, or
// by setting the "bind" command line flag.
// Serve detects einhorn and systemd for you.
// It works exactly like zenazn/goji.
func Serve() {
if !flag.Parsed() {
flag.Parse()
}

ServeListener(bind.Default())
serveListener(Handler(), bind.Default())
}

// ServeTLS is like Serve, but enables TLS using the given config.
Expand All @@ -35,14 +38,46 @@ func ServeTLS(config *tls.Config) {
flag.Parse()
}

ServeListener(tls.NewListener(bind.Default(), config))
serveListener(Handler(), tls.NewListener(bind.Default(), config))
}

// ServeListener is like Serve, but runs kami on top of an arbitrary net.Listener.
func ServeListener(listener net.Listener) {
serveListener(Handler(), listener)
}

// Serve starts serving this mux with reasonable defaults.
// The bind address can be changed by setting the GOJI_BIND environment variable, or
// by setting the "--bind" command line flag.
// Serve detects einhorn and systemd for you.
// It works exactly like zenazn/goji. Only one mux may be served at a time.
func (m *Mux) Serve() {
if !flag.Parsed() {
flag.Parse()
}

serveListener(m, bind.Default())
}

// ServeTLS is like Serve, but enables TLS using the given config.
func (m *Mux) ServeTLS(config *tls.Config) {
if !flag.Parsed() {
flag.Parse()
}

serveListener(m, tls.NewListener(bind.Default(), config))
}

// ServeListener is like Serve, but runs kami on top of an arbitrary net.Listener.
func (m *Mux) ServeListener(listener net.Listener) {
serveListener(m, listener)
}

// ServeListener is like Serve, but runs kami on top of an arbitrary net.Listener.
func serveListener(h http.Handler, listener net.Listener) {
// Install our handler at the root of the standard net/http default mux.
// This allows packages like expvar to continue working as expected.
http.Handle("/", Handler())
http.Handle("/", h)

log.Println("Starting kami on", listener.Addr())

Expand Down

0 comments on commit 250b4ba

Please sign in to comment.