first commit
way too late.
This commit is contained in:
commit
d4f7dc57fc
53 files changed
+3518
No files matched your search
@@ -0,0 +1,5 @@
|
||||
package shared
|
||||
|
||||
type AsDoormanUpdatePayloader interface {
|
||||
AsDoormanUpdatePayload() ([]byte, error)
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
goyaml "gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
var PARAM_FILE string = "params.yml"
|
||||
|
||||
var params *Params
|
||||
|
||||
type MongoParams struct {
|
||||
Url string `yaml:"url,omitempty"`
|
||||
DatabaseName string `yaml:"database_name,omitempty"`
|
||||
DoormanDefinitionCollection string `yaml:"doorman_definition_collection,omitempty"`
|
||||
}
|
||||
|
||||
type NSQParams struct {
|
||||
NSQLookupdUrl string `yaml:"nsq_lookupd_url,omitempty"`
|
||||
}
|
||||
|
||||
type NanoMsgParams struct {
|
||||
NanoMsgUrl string `yaml:"nanomsg_url,omitempty"`
|
||||
}
|
||||
|
||||
type Params struct {
|
||||
Port int `yaml:"port,omitempty"`
|
||||
Host string `yaml:"host,omitempty"`
|
||||
Mongo *MongoParams `yaml:"mongo,omitempty"`
|
||||
NSQ *NSQParams `yaml:"nsq,omitempty"`
|
||||
NanoMsg *NanoMsgParams `yaml:"nanomsg,omitempty"`
|
||||
|
||||
Database string `yaml:"database,omitempty"`
|
||||
MessageQueue string `yaml:"message_queue,omitempty"`
|
||||
}
|
||||
|
||||
func (p *Params) UseMongoDb() bool {
|
||||
db := strings.ToLower(p.Database)
|
||||
return db == "mongo" || db == "mongodb"
|
||||
}
|
||||
|
||||
func (p *Params) UseNSQ() bool {
|
||||
m := strings.ToLower(p.MessageQueue)
|
||||
return m == "nsq"
|
||||
}
|
||||
|
||||
func (p *Params) UseNanomsg() bool {
|
||||
m := strings.ToLower(p.MessageQueue)
|
||||
return m == "nanomsg"
|
||||
}
|
||||
|
||||
func (p *Params) InsertDefaultValues() *Params {
|
||||
if p.Host == "" {
|
||||
p.Host = "localhost"
|
||||
}
|
||||
if p.Port == 0 {
|
||||
p.Port = 1999
|
||||
}
|
||||
if p.Mongo == nil {
|
||||
p.Mongo = new(MongoParams)
|
||||
}
|
||||
if p.NSQ == nil {
|
||||
p.NSQ = new(NSQParams)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func LoadFromYamlFile(fileName string) (*Params, error) {
|
||||
if !Exists(fileName) {
|
||||
return new(Params).InsertDefaultValues(), nil
|
||||
}
|
||||
data, err := ioutil.ReadFile(fileName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return LoadFromYamlData(data)
|
||||
}
|
||||
|
||||
func LoadFromYamlData(data []byte) (*Params, error) {
|
||||
var err error
|
||||
p := new(Params)
|
||||
if err = goyaml.Unmarshal(data, p); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p.InsertDefaultValues(), nil
|
||||
}
|
||||
|
||||
func GetParams() *Params {
|
||||
return params
|
||||
}
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
if params, err = LoadFromYamlFile(PARAM_FILE); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
const yaml_params = `
|
||||
---
|
||||
port: 9999
|
||||
host: 0.0.0.0 #localhost
|
||||
# all the params relative to mongodb
|
||||
mongo:
|
||||
# the url where to get mongodb
|
||||
url: localhost:1234
|
||||
|
||||
# the name of the database to use
|
||||
database_name: doorman
|
||||
|
||||
# the name of the collection where all the street can be found
|
||||
doorman_definition_collection: doorman_definitions
|
||||
|
||||
# all the parameters relative to nsq message-queue
|
||||
nsq:
|
||||
# the url where to find nsq lookupd url
|
||||
nsq_lookupd_url: 127.0.0.1:4161
|
||||
|
||||
# all the parameters relative to nanomsg message-queue
|
||||
nanomsg:
|
||||
nanomsg_url: ipc:///tmp/doorman.ipc
|
||||
|
||||
|
||||
`
|
||||
|
||||
func TestLoadFromYamlData(t *testing.T) {
|
||||
p, err := LoadFromYamlData([]byte(yaml_params))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if p.Port != 9999 {
|
||||
t.Error("port")
|
||||
}
|
||||
if p.Host != "0.0.0.0" {
|
||||
t.Error("host")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMongoParams(t *testing.T) {
|
||||
p, err := LoadFromYamlData([]byte(yaml_params))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if p.Mongo.Url != "localhost:1234" {
|
||||
t.Error("mongo.url")
|
||||
}
|
||||
if p.Mongo.DatabaseName != "doorman" {
|
||||
t.Error("mongo.DatabaseName")
|
||||
}
|
||||
if p.Mongo.DoormanDefinitionCollection != "doorman_definitions" {
|
||||
t.Error("Mongo.DoormanDefinitionCollection", p.Mongo.DoormanDefinitionCollection)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNSQParams(t *testing.T) {
|
||||
p, err := LoadFromYamlData([]byte(yaml_params))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if p.NSQ.NSQLookupdUrl != "127.0.0.1:4161" {
|
||||
t.Error("Nsq.NSQLookupdUrl", p.NSQ.NSQLookupdUrl)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNanoMsgParams(t *testing.T) {
|
||||
p, err := LoadFromYamlData([]byte(yaml_params))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if p.NanoMsg.NanoMsgUrl != "ipc:///tmp/doorman.ipc" {
|
||||
t.Error("NanoMsgParams.NanoMsgUrl", p.NanoMsg.NanoMsgUrl)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMinimalParams(t *testing.T) {
|
||||
params := "---\n"
|
||||
p, err := LoadFromYamlData([]byte(params))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if p.Mongo.Url != "" {
|
||||
t.Error()
|
||||
}
|
||||
if p.Port != 1999 {
|
||||
t.Error("bad default port", p.Port)
|
||||
}
|
||||
if p.Host != "localhost" {
|
||||
t.Error("bad default host")
|
||||
}
|
||||
if p.MessageQueue != "" {
|
||||
t.Error("message queue")
|
||||
}
|
||||
|
||||
if p.Database != "" {
|
||||
t.Error("database")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUseMongoDb(t *testing.T) {
|
||||
p := &Params{}
|
||||
if p.Database = ""; p.UseMongoDb() {
|
||||
t.Error()
|
||||
}
|
||||
if p.Database = "mOnGo"; !p.UseMongoDb() {
|
||||
t.Error()
|
||||
}
|
||||
if p.Database = "monGoDb"; !p.UseMongoDb() {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestUseNSQ(t *testing.T) {
|
||||
p := &Params{}
|
||||
if p.MessageQueue = ""; p.UseNSQ() {
|
||||
t.Error()
|
||||
}
|
||||
if p.MessageQueue = "nSq"; !p.UseNSQ() {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestUseNanoMsg(t *testing.T) {
|
||||
p := &Params{}
|
||||
if p.MessageQueue = ""; p.UseNanomsg() {
|
||||
t.Error()
|
||||
}
|
||||
if p.MessageQueue = "nAnOmSg"; !p.UseNanomsg() {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package shared
|
||||
|
||||
import "os"
|
||||
|
||||
func Exists(name string) bool {
|
||||
if _, err := os.Stat(name); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in new issue
Block a user