mirror of https://github.com/tidwall/tile38.git
Disable HTTP & WebSocket transports on demand. (#128)
* Added .idea folder into .gitignore * Added --http-transport flag * Fixed test & Removed some dumps * Added support of yes/no
This commit is contained in:
parent
06dbb14562
commit
a4e5d38461
|
@ -5,5 +5,6 @@ data*/
|
|||
coverage.out
|
||||
packages/
|
||||
|
||||
# Ignore VS Code folder
|
||||
# Ignore IDE folders
|
||||
.idea/
|
||||
.vscode/
|
|
@ -32,6 +32,9 @@ var (
|
|||
quiet bool
|
||||
)
|
||||
|
||||
// TODO: Set to false in 2.*
|
||||
var httpTransport bool = true
|
||||
|
||||
// Fire up a webhook test server by using the --webhook-http-consumer-port
|
||||
// for example
|
||||
// $ ./tile38-server --webhook-http-consumer-port 9999
|
||||
|
@ -106,6 +109,17 @@ func main() {
|
|||
case "--dev", "-dev":
|
||||
devMode = true
|
||||
continue
|
||||
case "--http-transport":
|
||||
i++
|
||||
if i < len(os.Args) {
|
||||
switch strings.ToLower(os.Args[i]) {
|
||||
case "1", "true", "yes":
|
||||
httpTransport = true
|
||||
case "0", "false", "no":
|
||||
httpTransport = false
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
nargs = append(nargs, os.Args[i])
|
||||
}
|
||||
|
@ -118,6 +132,7 @@ func main() {
|
|||
flag.BoolVar(&quiet, "q", false, "Quiet logging. Totally silent.")
|
||||
flag.BoolVar(&veryVerbose, "vv", false, "Enable very verbose logging.")
|
||||
flag.Parse()
|
||||
|
||||
var logw io.Writer = os.Stderr
|
||||
if quiet {
|
||||
logw = ioutil.Discard
|
||||
|
@ -138,6 +153,11 @@ func main() {
|
|||
gitsha = ""
|
||||
}
|
||||
|
||||
httpTransportEnabled := "Enabled"
|
||||
if !httpTransport {
|
||||
httpTransportEnabled = "Disabled"
|
||||
}
|
||||
|
||||
// _____ _ _ ___ ___
|
||||
// |_ _|_| |___|_ | . |
|
||||
// | | | | | -_|_ | . |
|
||||
|
@ -148,12 +168,12 @@ func main() {
|
|||
| | |
|
||||
|____ | _ | Tile38 %s%s %d bit (%s/%s)
|
||||
| | | %sPort: %d, PID: %d
|
||||
|____ | _ |
|
||||
| | | tile38.com
|
||||
|_______|_______|
|
||||
`+"\n", core.Version, gitsha, strconv.IntSize, runtime.GOARCH, runtime.GOOS, hostd, port, os.Getpid())
|
||||
|____ | _ | HTTP & WebSocket transports: %s
|
||||
| | |
|
||||
|_______|_______| tile38.com
|
||||
`+"\n", core.Version, gitsha, strconv.IntSize, runtime.GOARCH, runtime.GOOS, hostd, port, os.Getpid(), httpTransportEnabled)
|
||||
|
||||
if err := controller.ListenAndServe(host, port, dir); err != nil {
|
||||
if err := controller.ListenAndServe(host, port, dir, httpTransport); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -100,10 +100,10 @@ type Controller struct {
|
|||
}
|
||||
|
||||
// ListenAndServe starts a new tile38 server
|
||||
func ListenAndServe(host string, port int, dir string) error {
|
||||
return ListenAndServeEx(host, port, dir, nil)
|
||||
func ListenAndServe(host string, port int, dir string, http bool) error {
|
||||
return ListenAndServeEx(host, port, dir, nil, http)
|
||||
}
|
||||
func ListenAndServeEx(host string, port int, dir string, ln *net.Listener) error {
|
||||
func ListenAndServeEx(host string, port int, dir string, ln *net.Listener, http bool) error {
|
||||
log.Infof("Server started, Tile38 version %s, git %s", core.Version, core.GitSHA)
|
||||
c := &Controller{
|
||||
host: host,
|
||||
|
@ -221,7 +221,7 @@ func ListenAndServeEx(host string, port int, dir string, ln *net.Listener) error
|
|||
delete(c.conns, conn)
|
||||
c.mu.Unlock()
|
||||
}
|
||||
return server.ListenAndServe(host, port, protected, handler, opened, closed, ln)
|
||||
return server.ListenAndServe(host, port, protected, handler, opened, closed, ln, http)
|
||||
}
|
||||
|
||||
func (c *Controller) watchMemory() {
|
||||
|
|
|
@ -55,6 +55,7 @@ func ListenAndServe(
|
|||
opened func(conn *Conn),
|
||||
closed func(conn *Conn),
|
||||
lnp *net.Listener,
|
||||
http bool,
|
||||
) error {
|
||||
ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
|
||||
if err != nil {
|
||||
|
@ -70,7 +71,7 @@ func ListenAndServe(
|
|||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
go handleConn(&Conn{Conn: conn}, protected, handler, opened, closed)
|
||||
go handleConn(&Conn{Conn: conn}, protected, handler, opened, closed, http)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -87,6 +88,7 @@ func handleConn(
|
|||
handler func(conn *Conn, msg *Message, rd *AnyReaderWriter, w io.Writer, websocket bool) error,
|
||||
opened func(conn *Conn),
|
||||
closed func(conn *Conn),
|
||||
http bool,
|
||||
) {
|
||||
opened(conn)
|
||||
defer closed(conn)
|
||||
|
@ -110,6 +112,14 @@ func handleConn(
|
|||
rd := NewAnyReaderWriter(conn)
|
||||
for {
|
||||
msg, err := rd.ReadMessage()
|
||||
|
||||
// Just closing connection if we have deprecated HTTP or WS connection,
|
||||
// And --http-transport = false
|
||||
if !http && (msg.ConnType == WebSocket || msg.ConnType == HTTP) {
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return
|
||||
|
|
|
@ -51,7 +51,7 @@ func mockOpenServer() (*mockServer, error) {
|
|||
s := &mockServer{port: port}
|
||||
tlog.Default = tlog.New(logOutput, nil)
|
||||
go func() {
|
||||
if err := controller.ListenAndServe("localhost", port, dir); err != nil {
|
||||
if err := controller.ListenAndServe("localhost", port, dir, true); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
|
Loading…
Reference in New Issue