summaryrefslogtreecommitdiff
path: root/test/main.go
blob: 1cb0380c76dd6c52e41b33a9b530d1ce87bd8329 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main

import (
	"fmt"
	whois "github.com/likexian/whois-go"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"time"
)

const (
	dburl     = "localhost"
	dropOldDB = true

	whoisServer = "whois.cymru.com"
)

type Person struct {
	ID        bson.ObjectId `bson:"_id,omitempty"`
	Name      string
	Phone     string
	Timestamp time.Time
}

func main() {
	whoistest()
}

func whoistest() {
	result, err := whois.Whois("123.123.123.123", whoisServer)
	if err != nil {
		panic(err)
	}

	fmt.Println(result)
}

func mongodbtest() {
	session, err := mgo.Dial(dburl)
	if err != nil {
		panic(err)
	}
	defer session.Close()

	session.SetMode(mgo.Monotonic, true)

	if dropOldDB {
		err = session.DB("test").DropDatabase()
		if err != nil {
			panic(err)
		}
	}

	// Collection people
	c := session.DB("test").C("people")

	index := mgo.Index{
		Key:        []string{"name", "phone"},
		Unique:     true,
		DropDups:   true,
		Background: true,
		Sparse:     true,
	}

	err = c.EnsureIndex(index)
	if err != nil {
		panic(err)
	}

	err = c.Insert(&Person{Name: "Person 1", Phone: "+46 123 45 67", Timestamp: time.Now()},
		&Person{Name: "Person 2", Phone: "+46 765 43 21", Timestamp: time.Now()})

	if err != nil {
		panic(err)
	}

	// Query one
	result := Person{}
	err = c.Find(bson.M{"name": "Person 1"}).Select(bson.M{"phone": 0}).One(&result)

	if err != nil {
		panic(err)
	}

	fmt.Println("Phone", result)
}