Add flags and shutdown command to simpleserver
This commit is contained in:
		@@ -19,7 +19,9 @@ type ReadableDiskStorage struct {
 | 
				
			|||||||
func NewReadableDiskStorage(storagePath string) Storage {
 | 
					func NewReadableDiskStorage(storagePath string) Storage {
 | 
				
			||||||
	indexesPath := path.Join(storagePath, "indexes")
 | 
						indexesPath := path.Join(storagePath, "indexes")
 | 
				
			||||||
	globalIndexPath := path.Join(indexesPath, "global")
 | 
						globalIndexPath := path.Join(indexesPath, "global")
 | 
				
			||||||
	os.MkdirAll(indexesPath, 0777)
 | 
						if err := os.MkdirAll(indexesPath, 0777); err != nil {
 | 
				
			||||||
 | 
							panic(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	return &ReadableDiskStorage{storagePath, indexesPath, globalIndexPath};
 | 
						return &ReadableDiskStorage{storagePath, indexesPath, globalIndexPath};
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,14 +5,17 @@ import (
 | 
				
			|||||||
	"github.com/pebbe/zmq4"
 | 
						"github.com/pebbe/zmq4"
 | 
				
			||||||
	"github.com/satori/go.uuid"
 | 
						"github.com/satori/go.uuid"
 | 
				
			||||||
	"bitbucket.org/nicdex/adaptech-goes"
 | 
						"bitbucket.org/nicdex/adaptech-goes"
 | 
				
			||||||
	"os"
 | 
					 | 
				
			||||||
	"path"
 | 
					 | 
				
			||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
 | 
						"flag"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"path"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Serializer struct {
 | 
					var port = flag.Int("port", 12345, "port to listen to")
 | 
				
			||||||
}
 | 
					var db = flag.String("db", fmt.Sprintf(".%cevents", os.PathSeparator), "path for storage")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Serializer struct {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewSerializer() goes.Serializer {
 | 
					func NewSerializer() goes.Serializer {
 | 
				
			||||||
	return &Serializer{}
 | 
						return &Serializer{}
 | 
				
			||||||
@@ -44,13 +47,26 @@ func (me Serializer) Deserialize(input []byte, typeId string) (interface{}, erro
 | 
				
			|||||||
	return output, nil
 | 
						return output, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func PathIsAbsolute(s string) bool {
 | 
				
			||||||
 | 
						if len(s) > 1 && s[1] == ':' {
 | 
				
			||||||
 | 
							return true
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return path.IsAbs(s)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
	fmt.Println("Simple ZeroMQ server for goes.")
 | 
						fmt.Println("Simple ZeroMQ server for goes.")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	//TODO: config/flag
 | 
						flag.Parse()
 | 
				
			||||||
	storagePath := path.Join(os.TempDir(), uuid.NewV4().String())
 | 
					 | 
				
			||||||
	storagePath = "c:\\dev\\go\\events"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						storagePath := *db
 | 
				
			||||||
 | 
						if !PathIsAbsolute(storagePath) {
 | 
				
			||||||
 | 
							wd, _ := os.Getwd()
 | 
				
			||||||
 | 
							storagePath = path.Join(wd, storagePath)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fmt.Println("Listening on port:", *port)
 | 
				
			||||||
 | 
						fmt.Println("Storage path:", storagePath)
 | 
				
			||||||
	goes.SetStorage(goes.NewReadableDiskStorage(storagePath))
 | 
						goes.SetStorage(goes.NewReadableDiskStorage(storagePath))
 | 
				
			||||||
	goes.SetSerializer(NewSerializer())
 | 
						goes.SetSerializer(NewSerializer())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -66,8 +82,7 @@ func main() {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	defer replySocket.Close()
 | 
						defer replySocket.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	//TODO: config/flag
 | 
						listenAddr := fmt.Sprintf("tcp://*:%d", *port)
 | 
				
			||||||
	listenAddr := "tcp://*:12345"
 | 
					 | 
				
			||||||
	err = replySocket.Bind(listenAddr)
 | 
						err = replySocket.Bind(listenAddr)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		panic(err)
 | 
							panic(err)
 | 
				
			||||||
@@ -120,6 +135,9 @@ func main() {
 | 
				
			|||||||
				break
 | 
									break
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			sendEvents(replySocket, events)
 | 
								sendEvents(replySocket, events)
 | 
				
			||||||
 | 
							case "Shutdown":
 | 
				
			||||||
 | 
								fmt.Println("->", command)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user