From 654509ff48ff40a50061a1108a95769493d61fff Mon Sep 17 00:00:00 2001 From: Michele Fadda Date: Thu, 16 Jul 2015 15:52:42 +0200 Subject: [PATCH] sostituito INSERT con UPSERT aggiunto numero versione --- llmongo.go | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/llmongo.go b/llmongo.go index 4042d77..2b021a2 100644 --- a/llmongo.go +++ b/llmongo.go @@ -24,6 +24,7 @@ type Options struct { ll *mgo.Collection RedisTTL time.Duration LogFile string + Version bool } type MongoLogin struct { @@ -33,19 +34,28 @@ type MongoLogin struct { Date time.Time `json:"date"` } +type Index struct { + User string `json:"user"` + Date time.Time `json:"date"` +} + +const ( + _VERSION = "v1.0.2" +) + var ( opts = Options{ - MongoUri: "mongodb://127.0.0.1:27018", - RedisUri: "redis-ll.mail.tiscali.sys:6379", - //MongoUri: "mongodb://10.39.81.85:27018", - //RedisUri: "127.0.0.1:6379", + //MongoUri: "mongodb://127.0.0.1:27018", + //RedisUri: "redis-ll.mail.tiscali.sys:6379", + MongoUri: "mongodb://10.39.81.85:27018", + RedisUri: "127.0.0.1:6379", RedisTTL: time.Hour * 11688, // 16 mesi LogFile: "log/llmongo.log", } ) func usage() { - fmt.Println("Usage: llmongo -m -r -t -l \n") + fmt.Println("Usage: llmongo -m -r -t -l -v\n") os.Exit(0) } @@ -63,6 +73,7 @@ func init() { flag.StringVar(&opts.RedisUri, "r", opts.RedisUri, "Redis") flag.StringVar(&opts.LogFile, "l", opts.LogFile, "Logs filename") flag.DurationVar(&opts.RedisTTL, "t", opts.RedisTTL, "Redis TTL") + flag.BoolVar(&opts.Version, "v", false, "Version") } func connectRedis() { @@ -88,6 +99,11 @@ func main() { flag.Usage = usage flag.Parse() + if opts.Version { + fmt.Println(os.Args[0], _VERSION) + os.Exit(0) + } + fs, err := os.OpenFile(opts.LogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) if err != nil { fmt.Println("Log file error: ", err.Error()) @@ -158,8 +174,12 @@ func main() { Ip: sval[2], Date: time.Unix(date, 0), } + ind := Index{ + User: user, + Date: time.Unix(date, 0), + } // inserisce il login su Mongodb - err = opts.ll.Insert(ml) + _, err = opts.ll.Upsert(ind, ml) if err != nil { log.Printf("Insert error: %+v\n", err) // se l'errore non e' "duplicate key error" salta al prossimo senza cancellare niente