/
runner_http.go
50 lines (43 loc) · 1.64 KB
/
runner_http.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
/*
Copyright 2013-2014 Canonical Ltd.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License version 3, as published
by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package server
import (
"launchpad.net/ubuntu-push/config"
"net"
"net/http"
)
// A HTTPServeParsedConfig holds and can be used to parse the HTTP server config.
type HTTPServeParsedConfig struct {
ParsedHTTPAddr config.ConfigHostPort `json:"http_addr"`
ParsedHTTPReadTimeout config.ConfigTimeDuration `json:"http_read_timeout"`
ParsedHTTPWriteTimeout config.ConfigTimeDuration `json:"http_write_timeout"`
}
// HTTPServeRunner returns a function to serve HTTP requests.
func HTTPServeRunner(h http.Handler, parsedCfg *HTTPServeParsedConfig) func() {
httpLst, err := net.Listen("tcp", parsedCfg.ParsedHTTPAddr.HostPort())
if err != nil {
BootLogFatalf("start http listening: %v", err)
}
BootLogListener("http", httpLst)
srv := &http.Server{
Handler: h,
ReadTimeout: parsedCfg.ParsedHTTPReadTimeout.TimeDuration(),
WriteTimeout: parsedCfg.ParsedHTTPWriteTimeout.TimeDuration(),
}
return func() {
err := srv.Serve(httpLst)
if err != nil {
BootLogFatalf("accepting http connections: %v", err)
}
}
}