-
Notifications
You must be signed in to change notification settings - Fork 2
/
https.go
47 lines (42 loc) · 856 Bytes
/
https.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
package ginshared
import (
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"github.com/unrolled/secure"
"go.uber.org/zap"
)
type Tlssettings struct {
Enabled bool
Pem string
Key string
}
func init() {
Provide(CheckAndSetupTLS)
}
func CheckAndSetupTLS(logger *zap.Logger) (tls *Tlssettings) {
tls = &Tlssettings{}
settings := viper.Sub("tls")
if settings == nil {
logger.Info("TLS is not enabled")
return
}
settings.Unmarshal(tls)
if tls.Key != "" && tls.Pem != "" {
tls.Enabled = true
logger.Info("TLS is enabled.")
}
return
}
func (s *Tlssettings) Middleware() gin.HandlerFunc {
return func(ctx *gin.Context) {
mw := secure.New(secure.Options{
SSLRedirect: true,
})
err := mw.Process(ctx.Writer, ctx.Request)
if err != nil {
zap.L().Warn("tls error", zap.Error(err))
return
}
ctx.Next()
}
}