-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
110 lines (85 loc) · 3.53 KB
/
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
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
package main
// sackci
// Copyright (C) 2017 Maximilian Pachl
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY 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/>.
// ----------------------------------------------------------------------------------
// imports
// ----------------------------------------------------------------------------------
import (
"net/http"
"time"
"context"
log "github.com/sirupsen/logrus"
"github.com/faryon93/sackci/config"
)
// ----------------------------------------------------------------------------------
// public functions
// ----------------------------------------------------------------------------------
// Starts the http endpoint.
// If a TLS encrypted endpoint is configured this endpoint is just used
// to redirect automatically to the secured endpoint.
func SetupHttpEndpoint(conf *config.Config, mux http.Handler) (*http.Server) {
var srv *http.Server
// if https is enabled the http server is responsible for
// redirecting unsecured request to a secure endpoint only!
if conf.IsHttpsEnabled() {
srv = &http.Server{
Addr: conf.HttpListen,
Handler: http.HandlerFunc(RedirectHttps),
}
// serve the normal api and frontend endpoints
} else {
srv = &http.Server{Addr: conf.HttpListen, Handler: getHandler(conf, mux)}
}
go func() {
log.Infoln("http server is listening on http://" + conf.HttpListen)
// serve the http connection as configured
err := srv.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Errorln("failed to serv http:", err.Error())
return
}
log.Infoln("http server is now closed")
}()
return srv
}
// Starts an TLS encrypted http endpoint.
func SetupHttpsEndpoint(conf *config.Config, mux http.Handler) (*http.Server) {
srv := &http.Server{Addr: conf.HttpsListen, Handler: getHandler(conf, mux)}
go func() {
log.Infoln("https server is listening on https://" + conf.HttpsListen)
// serve the http connection as configured
err := srv.ListenAndServeTLS(conf.HttpsCert, conf.HttpsKey)
if err != nil && err != http.ErrServerClosed {
log.Errorln("failed to serv https:", err.Error())
return
}
log.Infoln("https server is now closed")
}()
return srv
}
// Gracefully destroys a http endpoint with the given timeout.
func ShutdownHttp(srv *http.Server, timeout time.Duration) {
httpCtx, _ := context.WithTimeout(context.Background(), timeout)
srv.Shutdown(httpCtx)
}
// ----------------------------------------------------------------------------------
// private functions
// ----------------------------------------------------------------------------------
func getHandler(conf *config.Config, mux http.Handler) (http.Handler) {
// only apply session check if authentication is enabled
handler := mux
if conf.IsAuthEnabled() {
handler = CheckSession(mux)
}
return handler
}