This repository has been archived by the owner on May 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
goserve.go
176 lines (152 loc) · 3.75 KB
/
goserve.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"gopkg.in/v1/yaml"
"flag"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"syscall"
)
var verbose bool
var cfg ServerConfig
func init() {
flag.BoolVar(&verbose, "verbose", false, "Increase verbosity")
configPath := flag.String("config", "", "Path to configuration")
checkConfig := flag.Bool("config.check", false, "Check config then quit")
echoConfig := flag.Bool("config.echo", false, "Echo config then quit")
indexes := flag.Bool("indexes", true, "Allow directory listing")
httpEnabled := flag.Bool("http", true, "Enable HTTP listener")
httpAddr := flag.String("http.addr", ":8080", "HTTP address")
httpGzip := flag.Bool("http.gzip", true, "Enable HTTP gzip compression")
httpsEnabled := flag.Bool("https", false, "Enable HTTPS listener")
httpsAddr := flag.String("https.addr", ":8443", "HTTPS address")
httpsGzip := flag.Bool("https.gzip", true, "Enable HTTPS gzip compression")
httpsKey := flag.String("https.key", "", "Path to HTTPS key")
httpsCert := flag.String("https.cert", "", "Path to HTTPS cert")
flag.Parse()
if *configPath == "" {
if verbose {
log.Println("Config file not specified; using arguments")
}
cfg.Listeners = []Listener{}
if *httpEnabled {
cfg.Listeners = append(cfg.Listeners, Listener{
Protocol: "http",
Addr: *httpAddr,
Gzip: *httpGzip,
})
}
if *httpsEnabled {
cfg.Listeners = append(cfg.Listeners, Listener{
Protocol: "https",
Addr: *httpsAddr,
Gzip: *httpsGzip,
KeyFile: *httpsKey,
CertFile: *httpsCert,
})
}
// Serve from first path given on cmdline
target := flag.Arg(0)
if target == "" {
target = "."
}
cfg.Serves = []Serve{
Serve{
Path: "/",
Target: target,
Indexes: *indexes,
},
}
} else {
if verbose {
log.Println("Config file specified; ignoring command line arguments")
}
var err error
cfg, err = readServerConfig(*configPath)
if err != nil {
log.Fatalln("Couldn't load config:", err)
}
}
cfg.sanitise()
if *echoConfig {
b, err := yaml.Marshal(cfg)
if err != nil {
log.Fatalln(err)
}
print(string(b))
}
if !cfg.check() {
log.Fatalln("Invalid config. Exiting.")
}
if *checkConfig {
log.Println("Config check passed.")
}
if *echoConfig || *checkConfig {
os.Exit(0)
}
}
func readServerConfig(filename string) (cfg ServerConfig, err error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return
}
err = yaml.Unmarshal(data, &cfg)
return
}
func main() {
// Setup handlers
mux := NewStaticServeMux()
for _, e := range cfg.Errors {
mux.HandleError(e.Status, e.handler())
}
for _, s := range cfg.Serves {
mux.Handle(s.Path, s.handler())
}
for _, r := range cfg.Redirects {
mux.Handle(r.From, r.handler())
}
// Start listeners
for _, l := range cfg.Listeners {
var h http.Handler = mux
if len(l.Headers) > 0 {
h = CustomHeadersHandler(h, l.Headers)
}
if l.Gzip {
h = GzipHandler(h)
}
h = LogHandler(h)
if l.Protocol == "http" {
go func(l Listener) {
if verbose {
log.Printf("listening on HTTP %s\n", l.Addr)
}
err := http.ListenAndServe(l.Addr, h)
if err != nil {
log.Fatalln(err)
}
}(l)
} else if l.Protocol == "https" {
go func(l Listener) {
if verbose {
log.Printf(
"listening on HTTPS %s (cert: %s, key: %s)\n",
l.Addr, l.CertFile, l.KeyFile)
}
err := http.ListenAndServeTLS(l.Addr, l.CertFile, l.KeyFile, h)
if err != nil {
log.Fatalln(err)
}
}(l)
} else {
log.Printf("Unsupported protocol %s\n", l.Protocol)
}
}
// Since all the listeners are running in separate gorotines, we have to
// wait here for a termination signal.
exit := make(chan os.Signal, 1)
signal.Notify(exit, os.Interrupt, syscall.SIGTERM)
<-exit
os.Exit(0)
}