-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.go
More file actions
68 lines (54 loc) · 1.35 KB
/
server.go
File metadata and controls
68 lines (54 loc) · 1.35 KB
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
/*
TypePress server module.
*/
package server
import (
"net/http"
"os"
"sync"
"time"
"github.com/braintree/manners"
"github.com/codegangsta/martini"
"github.com/typepress/core"
"github.com/typepress/core/types"
)
var (
AppVersion string
srv *manners.GracefulServer
onceInit sync.Once
door *doorHandler
stopString string
)
func Run(m *martini.Martini, r martini.Router) error {
if stopString == "" {
stopString = time.Now().UTC().Format("15040501022006.000000000")
}
stopSig := types.NewStringSignal(stopString, nil)
onceInit.Do(initOnce)
m.Map(http.Dir(staticPath))
m.Map(types.ContentDir(contentPath))
m.Map(types.TemplateDir(templatePath))
srv = manners.NewServer()
core.ListenSignal(sigReceive(srv.Shutdown), os.Interrupt, os.Kill, stopSig)
stop := make(chan bool)
go func() {
stop <- true
core.FireSignal(types.NewStringSignal(core.ServerShutDown, nil), true)
srv.Shutdown <- true
}()
door = NewDoor(m, stop, false)
return srv.ListenAndServe(laddr, door)
}
func Simple() error {
return Run(core.Martini())
}
// 返回一个停止信号, 可用 core.FireSignal 触发信号通知服务器安全关闭.
func StopSignal() os.Signal {
if stopString == "" {
stopString = time.Now().UTC().Format("15040501022006.000000000")
}
return types.NewStringSignal(stopString, nil)
}
func initOnce() {
LoadConfig()
}