diff --git a/cmd/tile38-server/main.go b/cmd/tile38-server/main.go index fd221dfc..3469d12e 100644 --- a/cmd/tile38-server/main.go +++ b/cmd/tile38-server/main.go @@ -18,6 +18,7 @@ import ( var ( dir string port int + host string verbose bool veryVerbose bool devMode bool @@ -25,7 +26,8 @@ var ( ) func main() { - flag.IntVar(&port, "p", 9851, "The listening port for communication.") + flag.IntVar(&port, "p", 9851, "The listening port.") + flag.StringVar(&host, "h", "127.0.0.1", "The listening host.") flag.StringVar(&dir, "d", "data", "The data directory.") flag.BoolVar(&verbose, "v", false, "Enable verbose logging.") flag.BoolVar(&quiet, "q", false, "Quiet logging. Totally silent.") @@ -53,13 +55,13 @@ func main() { _______ _______ | | | |____ | _ | Tile38 %s (%s) %d bit (%s/%s) - | | | Port: %d, PID: %d + | | | Host: %s, Port: %d, PID: %d |____ | _ | | | | tile38.com |_______|_______| -`+"\n", core.Version, core.GitSHA, strconv.IntSize, runtime.GOARCH, runtime.GOOS, port, os.Getpid()) +`+"\n", core.Version, core.GitSHA, strconv.IntSize, runtime.GOARCH, runtime.GOOS, host, port, os.Getpid()) - if err := controller.ListenAndServe(port, dir); err != nil { + if err := controller.ListenAndServe(host, port, dir); err != nil { log.Fatal(err) } } diff --git a/controller/controller.go b/controller/controller.go index e1d04127..976a9074 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -52,6 +52,7 @@ func (col *collectionT) Less(item btree.Item) bool { // Controller is a tile38 controller type Controller struct { mu sync.RWMutex + host string port int f *os.File cols *btree.BTree // use both tree and map. provide ordering. @@ -80,9 +81,10 @@ type Config struct { } // ListenAndServe starts a new tile38 server -func ListenAndServe(port int, dir string) error { +func ListenAndServe(host string, port int, dir string) error { log.Infof("Server started, Tile38 version %s, git %s", core.Version, core.GitSHA) c := &Controller{ + host: host, port: port, dir: dir, cols: btree.New(16), @@ -127,7 +129,7 @@ func ListenAndServe(port int, dir string) error { } return nil } - return server.ListenAndServe(port, handler) + return server.ListenAndServe(host, port, handler) } func (c *Controller) setCol(key string, col *collection.Collection) { diff --git a/server/server.go b/server/server.go index c888a9c3..1daf0267 100644 --- a/server/server.go +++ b/server/server.go @@ -20,10 +20,10 @@ var ShowDebugMessages = false // ListenAndServe starts a tile38 server at the specified address. func ListenAndServe( - port int, + host string, port int, handler func(command []byte, conn net.Conn, rd *bufio.Reader, w io.Writer, websocket bool) error, ) error { - ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) + ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port)) if err != nil { return err }