-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
142 lines (118 loc) · 3.94 KB
/
main.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
package main
import (
"crypto/tls"
"flag"
"fmt"
"net/http"
"time"
"github.com/gin-gonic/contrib/ginrus"
"github.com/gin-gonic/gin"
"github.com/lacion/iothub/config"
"github.com/lacion/iothub/log"
"github.com/lacion/iothub/middlewares"
"github.com/olahol/melody"
)
func main() {
versionFlag := flag.Bool("version", false, "Version")
flag.Parse()
if *versionFlag {
fmt.Println("Git Commit:", GitCommit)
fmt.Println("Version:", Version)
if VersionPrerelease != "" {
fmt.Println("Version PreRelease:", VersionPrerelease)
}
return
}
log.WithFields(log.Fields{
"EventName": "get_config_env_vars",
}).Debug("Reading configuration from env vars")
cfg := config.Config()
log.WithFields(log.Fields{
"EventName": "set_gin_mode",
"Mode": cfg.GetString("mode"),
}).Debug("Setting gin mode to ", cfg.GetString("mode"))
gin.SetMode(cfg.GetString("mode"))
r := gin.New()
m := melody.New()
r.Use(ginrus.Ginrus(log.NewLogger(cfg), time.RFC3339, true))
r.Use(gin.Recovery())
authorized := r.Group("/")
authorized.Use(middlewares.Auth())
// Web Sockets
authorized.GET("/channel/:name/ws", func(c *gin.Context) {
m.HandleRequest(c.Writer, c.Request)
})
m.HandleConnect(func(s *melody.Session) {
log.WithFields(log.Fields{
"EventName": "ws_client_connect",
"RemoteAddress": s.Request.RemoteAddr,
}).Debug("new ws client connected ", s.Request.RemoteAddr)
})
m.HandleDisconnect(func(s *melody.Session) {
log.WithFields(log.Fields{
"EventName": "ws_client_disconnect",
"RemoteAddress": s.Request.RemoteAddr,
}).Debug("ws client disconnected ", s.Request.RemoteAddr)
})
m.HandleError(func(s *melody.Session, err error) {
log.WithFields(log.Fields{
"EventName": "ws_error",
"RemoteAddress": s.Request.RemoteAddr,
"Error": err.Error(),
}).Error("error ocurred with ws client ", err.Error())
})
m.HandleMessage(func(s *melody.Session, msg []byte) {
m.BroadcastFilter(msg, func(q *melody.Session) bool {
msgStr := string(msg[:])
log.WithFields(log.Fields{
"EventName": "ws_client_message",
"Message": msgStr,
"Channel": q.Request.URL.Path,
}).Debug("got msg: ", msgStr)
return q.Request.URL.Path == s.Request.URL.Path
})
})
// Start Server
log.WithFields(log.Fields{
"EventName": "start",
"ListenAddress": cfg.GetString("listen_address"),
"GitCommit": GitCommit,
"Version": Version,
"VersionPrerelease": VersionPrerelease,
"TLS": cfg.GetBool("secure"),
"ReadTimeout": cfg.GetDuration("read_timeout"),
"WriteTimeout": cfg.GetDuration("write_timeout"),
"MaxHeaderBytes": cfg.GetInt("max_header_bytes"),
}).Info("starting server and listening on ", cfg.GetString("listen_address"))
// ssl labs recomendations
tlscfg := &tls.Config{
// the bellow 3 options would get a perfect score in ssl labs test, but you will losse client compatibility
// MinVersion: tls.VersionTLS12,
// CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
//CipherSuites: []uint16{
// tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
// tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
// tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
// tls.TLS_RSA_WITH_AES_256_CBC_SHA,
//},
PreferServerCipherSuites: true,
}
server := &http.Server{
Addr: cfg.GetString("listen_address"),
Handler: r,
ReadTimeout: cfg.GetDuration("read_timeout"),
WriteTimeout: cfg.GetDuration("write_timeout"),
MaxHeaderBytes: cfg.GetInt("max_header_bytes"),
TLSConfig: tlscfg,
// the bellow line would get a perfect score in ssl labs test, but you will losse client compatibility
// also no http/2
// TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler), 0),
}
switch cfg.GetBool("secure") {
case true:
// Key or DH parameter strength >= 4096 bits (e.g., 4096)
log.Fatal(server.ListenAndServeTLS(cfg.GetString("cert_file"), cfg.GetString("key_file")))
default:
log.Fatal(server.ListenAndServe())
}
}