-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhttp_service.go
59 lines (49 loc) · 1.4 KB
/
http_service.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
// Copyright 2016 struktur AG. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package phoenix
import (
"crypto/tls"
"log"
"net/http"
"time"
"github.com/strukturag/httputils"
)
type httpService struct {
*httputils.Server
}
func newHTTPService(logger *log.Logger, handler http.Handler, addr string, readtimeout, writetimeout int, tlsConfig *tls.Config) Service {
server := &httputils.Server{
Server: http.Server{
Addr: addr,
Handler: handler,
ReadTimeout: time.Duration(readtimeout) * time.Second,
WriteTimeout: time.Duration(writetimeout) * time.Second,
MaxHeaderBytes: 1 << 20,
TLSConfig: tlsConfig,
},
Logger: logger,
}
return &httpService{server}
}
func (service *httpService) OnStart(container Container) (err error) {
container.Printf("Starting %s server on %s", service.protocol(), service.addr())
if service.TLSConfig == nil {
err = service.Listen()
} else {
err = service.ListenTLSWithConfig(service.TLSConfig)
}
return
}
func (service *httpService) OnStop(container Container) {
container.Printf("Stopped %s server on %s", service.protocol(), service.addr())
}
func (service *httpService) addr() string {
return service.Server.Server.Addr
}
func (service *httpService) protocol() string {
if service.Server.Server.TLSConfig != nil {
return "HTTPS"
}
return "HTTP"
}