package main import ( "flag" "fmt" "log" "net" "os" "os/signal" ) func main() { var port int var host string var driver string var cacheSize int flag.IntVar(&port, "port", 6379, "port to bind") flag.StringVar(&driver, "driver", "leveldb", "type of database (leveldb, sqlite)") flag.StringVar(&host, "host", "", "host to bind") flag.IntVar(&cacheSize, "cache", 32, "cache size in MB") flag.Parse() args := flag.Args() if len(args) < 1 { log.Fatal("Missing path to world") } var err error var backend Backend if driver == "sqlite" { if backend, err = NewSqliteBackend(args[0]); err != nil { log.Fatal(err) } } else { if backend, err = NewLeveDBBackend(args[0], cacheSize); err != nil { log.Fatal(err) } } defer backend.Shutdown() var listener net.Listener listener, err = net.Listen("tcp", fmt.Sprintf("%s:%d", host, port)) if err != nil { log.Fatal(err) } defer listener.Close() log.Printf("Server started at %s", listener.Addr()) connChan := make(chan net.Conn) defer close(connChan) sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, os.Interrupt, os.Kill) go func() { for { conn, err := listener.Accept() if err != nil { log.Fatal(err) } log.Printf("Client accepted from: %s", conn.RemoteAddr()) connChan <- conn } }() for { select { case conn := <-connChan: go NewConnection(conn, backend).Run() case <-sigChan: log.Println("Shutting down") return } } }