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:
Pavel Makarenko 2017-01-13 18:45:28 +03:00 committed by Josh Baker
parent 06dbb14562
commit a4e5d38461
5 changed files with 51 additions and 20 deletions

3
.gitignore vendored
View File

@ -5,5 +5,6 @@ data*/
coverage.out coverage.out
packages/ packages/
# Ignore VS Code folder # Ignore IDE folders
.idea/
.vscode/ .vscode/

View File

@ -32,6 +32,9 @@ var (
quiet bool 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 // Fire up a webhook test server by using the --webhook-http-consumer-port
// for example // for example
// $ ./tile38-server --webhook-http-consumer-port 9999 // $ ./tile38-server --webhook-http-consumer-port 9999
@ -106,6 +109,17 @@ func main() {
case "--dev", "-dev": case "--dev", "-dev":
devMode = true devMode = true
continue 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]) nargs = append(nargs, os.Args[i])
} }
@ -118,6 +132,7 @@ func main() {
flag.BoolVar(&quiet, "q", false, "Quiet logging. Totally silent.") flag.BoolVar(&quiet, "q", false, "Quiet logging. Totally silent.")
flag.BoolVar(&veryVerbose, "vv", false, "Enable very verbose logging.") flag.BoolVar(&veryVerbose, "vv", false, "Enable very verbose logging.")
flag.Parse() flag.Parse()
var logw io.Writer = os.Stderr var logw io.Writer = os.Stderr
if quiet { if quiet {
logw = ioutil.Discard logw = ioutil.Discard
@ -138,6 +153,11 @@ func main() {
gitsha = "" gitsha = ""
} }
httpTransportEnabled := "Enabled"
if !httpTransport {
httpTransportEnabled = "Disabled"
}
// _____ _ _ ___ ___ // _____ _ _ ___ ___
// |_ _|_| |___|_ | . | // |_ _|_| |___|_ | . |
// | | | | | -_|_ | . | // | | | | | -_|_ | . |
@ -148,12 +168,12 @@ func main() {
| | | | | |
|____ | _ | Tile38 %s%s %d bit (%s/%s) |____ | _ | Tile38 %s%s %d bit (%s/%s)
| | | %sPort: %d, PID: %d | | | %sPort: %d, PID: %d
|____ | _ | |____ | _ | HTTP & WebSocket transports: %s
| | | tile38.com | | |
|_______|_______| |_______|_______| tile38.com
`+"\n", core.Version, gitsha, strconv.IntSize, runtime.GOARCH, runtime.GOOS, hostd, port, os.Getpid()) `+"\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) log.Fatal(err)
} }
} }

View File

@ -100,10 +100,10 @@ type Controller struct {
} }
// ListenAndServe starts a new tile38 server // ListenAndServe starts a new tile38 server
func ListenAndServe(host string, port int, dir string) error { func ListenAndServe(host string, port int, dir string, http bool) error {
return ListenAndServeEx(host, port, dir, nil) 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) log.Infof("Server started, Tile38 version %s, git %s", core.Version, core.GitSHA)
c := &Controller{ c := &Controller{
host: host, host: host,
@ -221,7 +221,7 @@ func ListenAndServeEx(host string, port int, dir string, ln *net.Listener) error
delete(c.conns, conn) delete(c.conns, conn)
c.mu.Unlock() 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() { func (c *Controller) watchMemory() {

View File

@ -55,6 +55,7 @@ func ListenAndServe(
opened func(conn *Conn), opened func(conn *Conn),
closed func(conn *Conn), closed func(conn *Conn),
lnp *net.Listener, lnp *net.Listener,
http bool,
) error { ) error {
ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port)) ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil { if err != nil {
@ -70,7 +71,7 @@ func ListenAndServe(
log.Error(err) log.Error(err)
return 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, handler func(conn *Conn, msg *Message, rd *AnyReaderWriter, w io.Writer, websocket bool) error,
opened func(conn *Conn), opened func(conn *Conn),
closed func(conn *Conn), closed func(conn *Conn),
http bool,
) { ) {
opened(conn) opened(conn)
defer closed(conn) defer closed(conn)
@ -110,6 +112,14 @@ func handleConn(
rd := NewAnyReaderWriter(conn) rd := NewAnyReaderWriter(conn)
for { for {
msg, err := rd.ReadMessage() 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 != nil {
if err == io.EOF { if err == io.EOF {
return return

View File

@ -51,7 +51,7 @@ func mockOpenServer() (*mockServer, error) {
s := &mockServer{port: port} s := &mockServer{port: port}
tlog.Default = tlog.New(logOutput, nil) tlog.Default = tlog.New(logOutput, nil)
go func() { go func() {
if err := controller.ListenAndServe("localhost", port, dir); err != nil { if err := controller.ListenAndServe("localhost", port, dir, true); err != nil {
log.Fatal(err) log.Fatal(err)
} }
}() }()