-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
serve.go
96 lines (76 loc) · 2.38 KB
/
serve.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// +build !appengine
package kami
import (
"crypto/tls"
"flag"
"log"
"net"
"net/http"
"time"
"github.com/zenazn/goji/bind"
"github.com/zenazn/goji/graceful"
)
func init() {
bind.WithFlag()
graceful.DoubleKickWindow(2 * time.Second)
}
// Serve starts kami 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.
func Serve() {
if !flag.Parsed() {
flag.Parse()
}
serveListener(Handler(), bind.Default())
}
// ServeTLS is like Serve, but enables TLS using the given config.
func ServeTLS(config *tls.Config) {
if !flag.Parsed() {
flag.Parse()
}
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("/", h)
log.Println("Starting kami on", listener.Addr())
graceful.HandleSignals()
bind.Ready()
graceful.PreHook(func() { log.Printf("kami received signal, gracefully stopping") })
graceful.PostHook(func() { log.Printf("kami stopped") })
err := graceful.Serve(listener, http.DefaultServeMux)
if err != nil {
log.Fatal(err)
}
graceful.Wait()
}