-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserving.go
113 lines (95 loc) · 2.84 KB
/
serving.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package aspen
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/jteeuwen/go-pkg-optarg"
)
type serverContext struct {
PackageName string
ServerBind string
WwwRoot string
Debug bool
website *Website
}
func AddCommonServingOptions(serverBind,
wwwRoot, charsetDynamic, charsetStatic, indices string,
debug, listDirs bool) {
optarg.Add("w", "www_root",
"Filesystem path of the document publishing root", wwwRoot)
optarg.Add("a", "network_address", "The IPv4 or IPv6 address to which "+
"the generated server will bind by default", serverBind)
optarg.Add("x", "debug", "Print debugging output", debug)
optarg.Add("", "charset_dynamic", "Set as the charset for rendered "+
"and negotiated resources of Content-Type text/*", charsetDynamic)
optarg.Add("", "charset_static", "Set as the charset for static "+
"resources of Content-Type text/*", charsetStatic)
optarg.Add("", "indices", "A comma-separated list of filenames to "+
"look for when a directory is requested directly; prefix "+
"with + to extend previous configuration instead of overriding",
indices)
optarg.Add("", "list_directories", "if set to {true,1}, will serve "+
"a directory listing when no index is available", listDirs)
}
func RunServerMain(wwwRoot, serverBind, packageName,
charsetDynamic, charsetStatic, indices string, listDirs, debug bool) {
AddCommonServingOptions(serverBind,
wwwRoot, charsetDynamic, charsetStatic, indices, debug, listDirs)
for opt := range optarg.Parse() {
switch opt.Name {
case "network_address":
serverBind = opt.String()
case "www_root":
wwwRoot = opt.String()
case "debug":
debug = opt.Bool()
case "charset_dynamic":
charsetDynamic = opt.String()
case "charset_static":
charsetStatic = opt.String()
case "indices":
indices = opt.String()
case "list_directories":
listDirs = opt.Bool()
}
}
wwwRoot, err := filepath.Abs(wwwRoot)
if err != nil {
log.Fatal(err)
}
SetDebug(debug)
debugf("Declaring app for package %q", packageName)
website := DeclareWebsite(packageName)
website.Configure(serverBind, wwwRoot, charsetDynamic, charsetStatic,
indices, debug, listDirs)
err = website.RunServer()
if err != nil {
log.Fatal(err)
}
}
func newServerContext(website *Website, packageName, serverBind, wwwRoot string,
debug bool) *serverContext {
return &serverContext{
PackageName: packageName,
ServerBind: serverBind,
WwwRoot: wwwRoot,
Debug: debug,
website: website,
}
}
func (me *serverContext) serverQuitListener() {
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGQUIT)
<-ch
log.Println("Received SIGQUIT; exiting")
os.Exit(0)
}
func (me *serverContext) Run() error {
go me.serverQuitListener()
fmt.Printf("%s-http-server serving on %q\n", me.PackageName, me.ServerBind)
return http.ListenAndServe(me.ServerBind, nil)
}