-
Notifications
You must be signed in to change notification settings - Fork 1
/
serve.go
47 lines (36 loc) · 944 Bytes
/
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
// +build !appengine
package slim
import (
"flag"
"log"
"net/http"
"github.com/vanackere/slim/bind"
"github.com/vanackere/slim/graceful"
)
func init() {
bind.WithFlag()
if fl := log.Flags(); fl&log.Ltime != 0 {
log.SetFlags(fl | log.Lmicroseconds)
}
}
// Serve starts Goji using reasonable defaults.
func Serve() {
if !flag.Parsed() {
flag.Parse()
}
DefaultMux.Compile()
// 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("/", DefaultMux)
listener := bind.Default()
log.Println("Starting Goji on", listener.Addr())
graceful.HandleSignals()
bind.Ready()
graceful.PreHook(func() { log.Printf("Goji received signal, gracefully stopping") })
graceful.PostHook(func() { log.Printf("Goji stopped") })
err := graceful.Serve(listener, http.DefaultServeMux)
if err != nil {
log.Fatal(err)
}
graceful.Wait()
}