-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
111 lines (93 loc) · 2.53 KB
/
server.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
package server
import (
"errors"
"net/http"
"github.com/larisgo/laravel-echo-server/express"
"github.com/larisgo/laravel-echo-server/options"
"github.com/zishang520/engine.io/types"
"github.com/zishang520/engine.io/utils"
"github.com/zishang520/socket.io/socket"
)
type Server struct {
Express *express.Express
// Socket.io client.
Io *socket.Server
// Configurable server options.
options *options.Config
// The http server.
server *types.HttpServer
}
// Create a new server instance.
func NewServer(_options *options.Config) *Server {
serv := &Server{}
serv.options = _options
return serv
}
// Start the Socket.io server.
func (serv *Server) Init() (*socket.Server, error) {
if err := serv.ServerProtocol(); err != nil {
return nil, err
}
host := serv.options.Host
if host == "" {
host = "localhost"
}
utils.Log().Success(`Running at %s on port %s`, host, serv.GetPort())
return serv.Io, nil
}
// Sanitize the port string from any extra characters
func (serv *Server) GetPort() string {
return serv.options.Port
}
// Select the http protocol to run on.
func (serv *Server) ServerProtocol() error {
if serv.options.Protocol == "https" {
if err := serv.Secure(); err != nil {
return err
}
return serv.httpServer(true)
}
return serv.httpServer(false)
}
// Load SSL 'key' & 'cert' files if https is enabled.
func (serv *Server) Secure() error {
if serv.options.SslCertPath == "" || serv.options.SslKeyPath == "" {
return errors.New(`SSL paths are missing in server config.`)
}
return nil
}
// Create a socket.io server.
func (serv *Server) httpServer(secure bool) (err error) {
serv.Express = express.NewExpress(serv.options)
serv.Express.Use(func(w http.ResponseWriter, r *http.Request, next func()) {
for key, value := range serv.options.Headers {
w.Header().Set(key, value)
}
next()
})
serv.server = types.CreateServer(serv.Express)
serv.Io = socket.NewServer(serv.server, serv.options.Socketio.Config())
switch hosts := serv.options.Host.(type) {
case string:
serv.listen(hosts, secure)
case options.Hosts:
for _, host := range hosts {
serv.listen(host, secure)
}
default:
return errors.New(`Host type error, can only be a string or string slice.`)
}
return nil
}
// Close
func (serv *Server) Close() error {
return serv.server.Close(nil)
}
// listen
func (serv *Server) listen(host string, secure bool) {
if secure {
serv.server.ListenTLS(host+":"+serv.GetPort(), serv.options.SslCertPath, serv.options.SslKeyPath, nil)
} else {
serv.server.Listen(host+":"+serv.GetPort(), nil)
}
}