Example #1
0
File: toys.go Project: nvcnvn/glog
// Handler returns a http.Handler
func Handler(path string, dbsess *mgo.Session, tmpl *view.View) *handler {
	h := &handler{}
	h.dbsess = dbsess
	h.tmpl = tmpl
	h.path = path
	h.initSubRoutes()

	h.notifer = membership.NewSMTPNotificater(
		CONFIG.Get("smtp_email").(string),
		CONFIG.Get("smtp_pass").(string),
		CONFIG.Get("smtp_addr").(string),
		int(CONFIG.Get("smtp_port").(float64)),
	)

	dbsess.DB(DBNAME).C("toysSession").EnsureIndex(mgo.Index{
		Key:         []string{"lastactivity"},
		ExpireAfter: 7200 * time.Second,
	})

	dbsess.DB(DBNAME).C("toysUser").EnsureIndex(mgo.Index{
		Key:    []string{"email"},
		Unique: true,
	})

	return h
}
Example #2
0
func main() {
	var (
		mongoSession *mgo.Session
		database     *mgo.Database
		collection   *mgo.Collection
		changeInfo   *mgo.ChangeInfo
		err          error
	)

	if mongoSession, err = mgo.Dial("localhost"); err != nil {
		panic(err)
	}

	database = mongoSession.DB("mgo_examples_03")
	collection = database.C("todos")

	// START OMIT
	var todo = Todo{
		Id:      bson.NewObjectId(),
		Task:    "Demo mgo",
		Created: time.Now(),
	}

	// This is a shortcut to collection.Upsert(bson.M{"_id": todo.id}, &todo)
	if changeInfo, err = collection.UpsertId(todo.Id, &todo); err != nil {
		panic(err)
	}
	// END OMIT

	fmt.Printf("Todo: %# v", pretty.Formatter(todo))
	fmt.Printf("Change Info: %# v", pretty.Formatter(changeInfo))

}
Example #3
0
func read_mongodb_static_size(f *IOFilterProtocol, session *mgo.Session) bool {
	disk_usage := &f.disk_usage

	var stats bson.M
	var temp int

	admindb := session.DB("admin")
	// NOTE: admindb.Login is not necessary if we connect to mongodb
	// through 'localhost'
	err := admindb.Run(bson.D{{"dbStats", 1}, {"scale", 1}}, &stats)
	if err != nil {
		logger.Error("Failed to get database %s stats [%s].", "admin", err)
		return false
	}

	if !parse_dbstats(stats["nsSizeMB"], &temp) {
		logger.Error("Failed to read admin_namespace_size.")
		return false
	}
	admin_namespace_size := uint64(temp * 1024 * 1024)
	disk_usage.static_size += admin_namespace_size

	if !parse_dbstats(stats["fileSize"], &temp) {
		logger.Error("Failed to read admin_data_file_size.")
		return false
	}
	admin_data_file_size := uint64(temp)
	disk_usage.static_size += admin_data_file_size

	logger.Debug("Get static disk files size %d.", disk_usage.static_size)
	return true
}
Example #4
0
func processServerSendUsers(m map[string]interface{}, session *mgo.Session) {

	log.Println("PARSING server_send_users")

	// wholesale replace users collection
	log.Println("wholesale replace users collection")
	c := session.DB("landline").C("Users")
	c.RemoveAll(bson.M{})

	for k, v := range m["users"].([]interface{}) {

		log.Println(k)
		object := v.(map[string]interface{})

		// create object
		object_map := make(map[string]interface{})
		object_map["encrypted_kek"] = object["encrypted_kek"].(string)
		object_map["encrypted_rsa_private"] = object["encrypted_rsa_private"].(string)
		object_map["hashed_password"] = object["hashed_password"].(string)
		object_map["rsa_public"] = object["rsa_public"].(string)
		object_map["username"] = object["username"].(string)

		err := c.Insert(object_map)
		if err != nil {
			panic(err)
		}
	}
}
Example #5
0
func Router(session *mgo.Session) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, req *http.Request) {
		user_agent := new(ua.UserAgent)
		user_agent.Parse(req.Header.Get("User-Agent"))

		url := req.FormValue("url")

		engine_name, engine_version := user_agent.Engine()
		browser_name, browser_version := user_agent.Browser()

		url_document := &URL{
			Date:           time.Now(),
			Uri:            url,
			Mobile:         user_agent.Mobile(),
			Bot:            user_agent.Bot(),
			Mozilla:        user_agent.Mozilla(),
			Platform:       user_agent.Platform(),
			Os:             user_agent.OS(),
			EngineName:     engine_name,
			EngineVersion:  engine_version,
			BrowserName:    browser_name,
			BrowserVersion: browser_version,
		}

		c := session.DB("goanywhere").C("urls")

		err := c.Insert(url_document)

		if err != nil {
			panic(err)
		}

		http.Redirect(w, req, url, http.StatusFound)
	}
}
Example #6
0
File: mgo.go Project: jkary/core
// resetAdminPasswordAndFetchDBNames logs into the database with a
// plausible password and returns all the database's db names. We need
// to try several passwords because we don't know what state the mongo
// server is in when Reset is called. If the test has set a custom
// password, we're out of luck, but if they are using
// DefaultStatePassword, we can succeed.
func resetAdminPasswordAndFetchDBNames(session *mgo.Session) ([]string, bool) {
	// First try with no password
	dbnames, err := session.DatabaseNames()
	if err == nil {
		return dbnames, true
	}
	if !isUnauthorized(err) {
		panic(err)
	}
	// Then try the two most likely passwords in turn.
	for _, password := range []string{
		DefaultMongoPassword,
		utils.UserPasswordHash(DefaultMongoPassword, utils.CompatSalt),
	} {
		admin := session.DB("admin")
		if err := admin.Login("admin", password); err != nil {
			logger.Infof("failed to log in with password %q", password)
			continue
		}
		dbnames, err := session.DatabaseNames()
		if err == nil {
			if err := admin.RemoveUser("admin"); err != nil {
				panic(err)
			}
			return dbnames, true
		}
		if !isUnauthorized(err) {
			panic(err)
		}
		logger.Infof("unauthorized access when getting database names; password %q", password)
	}
	return nil, false
}
Example #7
0
// Save the DataModel to DataStore
func SaveModel(mgo_db string, m DataModel, conn *mgo.Session) (err error) {
	if conn == nil {
		conn, err = MgoConnGet(mgo_db)
		if err != nil {
			return
		}
		defer conn.Close()
	}
	if conn != nil {
		bsonMid := m.MidGet()
		c := conn.DB(mgo_db).C(m.Type())
		//Debug("SAVING ", mgo_db, " type=", m.Type(), " Mid=", bsonMid)
		if len(bsonMid) < 5 {
			m.MidSet(bson.NewObjectId())
			if err = c.Insert(m); err != nil {
				Log(ERROR, "MGOU ERROR on insert ", err, " TYPE=", m.Type(), " ", m.MidGet())
			} else {
				//Log(DEBUG, "successfully inserted!!!!!!  ", m.MidGet(), " oid=", m.OidGet())
			}
		} else {
			// YOU MUST NOT SEND Mid  "_id" to Mongo
			mid := m.MidGet()
			m.MidSet("") // omitempty means it doesn't get sent
			if err = c.Update(bson.M{"_id": bson.ObjectId(bsonMid)}, m); err != nil {
				Log(ERROR, "MGOU ERROR on update ", err, " ", bsonMid, " MID=?", m.MidGet())
			}
			m.MidSet(mid)
		}
	} else {
		Log(ERROR, "MGOU Nil connection")
		return errors.New("no db connection")
	}
	return
}
Example #8
0
// Save the DataModel to DataStore
func Insert(mgo_db string, m DataModel, conn *mgo.Session) (err error) {
	if conn == nil {
		conn, err = MgoConnGet(mgo_db)
		if err != nil {
			return
		}
		defer conn.Close()
	}
	if conn != nil {
		c := conn.DB(mgo_db).C(m.Type())
		if len(m.MidGet()) == 0 {
			m.MidSet(bson.NewObjectId())
		}
		if err = c.Insert(m); err != nil {
			Log(ERROR, "MGOU ERROR on insert ", err, " TYPE=", m.Type(), " ", m.MidGet())
		} else {
			//Log(DEBUG, "successfully inserted!!!!!!  ", m.MidGet(), " oid=", m.OidGet())
		}

	} else {
		Log(ERROR, "MGOU Nil connection")
		return errors.New("no db connection")
	}
	return
}
Example #9
0
func lastDocument(session *mgo.Session) {
	var m bson.M
	coll := session.DB(mongoDb).C(mongoColl)
	coll.Find(nil).Sort("-_id").One(&m)
	js, _ := json.MarshalIndent(&m, "", "  ")
	fmt.Println(string(js))
}
Example #10
0
func clientDiffRequest(wsConn *websocket.Conn, session *mgo.Session) {

	// init client_diff_request
	response_map := make(map[string]interface{})
	response_map["action"] = "client_diff_request"

	// find syncable object uuids
	cb := session.DB("landline").C("SyncableObjects")
	var m_result []map[string]interface{}
	err := cb.Find(bson.M{}).All(&m_result)
	if err != nil {
		log.Println(err)
	} else {

		// objects that the clients knows, but the server may not
		object_uuids := make(map[string]interface{})

		// loop results
		for u, result := range m_result {
			_ = u
			object_uuids[result["uuid"].(string)] = result["time_modified_since_creation"]
		}
		response_map["object_uuids"] = object_uuids

		// send it over websocket
		wsConn.WriteJSON(response_map)

		log.Println("Wrote message:")
		jsonString, _ := json.Marshal(response_map)
		log.Println(string(jsonString))
	}
}
Example #11
0
// startSession will create a new document in the sessions collection and return the _id
func startSession(session *mgo.Session, username string) (*SessionId, error) {
	c := session.DB(database).C(sessions)
	sessionId := &SessionId{Id: bson.NewObjectId(), Username: username}
	if err := c.Insert(sessionId); err != nil {
		return nil, err
	}
	return sessionId, nil
}
Example #12
0
func getDB(session *mgo.Session) *mgo.Database {
	var db *mgo.Database
	if fDev {
		db = session.DB("dev")
	} else {
		db = session.DB("prod")
	}
	return db
}
Example #13
0
func collection(m interface{}, s *mgo.Session) *mgo.Collection {
	typ := reflect.TypeOf(m)
	i := strings.LastIndex(typ.String(), ".") + 1
	n := typ.String()[i:len(typ.String())]

	db := revel.Config.StringDefault("db.name", "helix")
	c := revel.Config.StringDefault("db.collection."+n, n)
	return s.DB(db).C(c)
}
Example #14
0
File: dal_mgo.go Project: jsli/cms
func UpdateUserByM(session *mgo.Session, m bson.M, obj interface{}) error {
	uc := session.DB(DbName).C(CollectionName)
	err := uc.Update(m, obj)
	if err == nil {
		fmt.Println("update ", obj, " ok")
	} else {
		fmt.Println("update ", obj, "failed :", err)
	}
	return err
}
Example #15
0
File: dal_mgo.go Project: jsli/cms
func DeleteUserByM(session *mgo.Session, host *User, m bson.M) error {
	uc := session.DB(DbName).C(CollectionName)
	err := uc.Remove(m)
	if err != nil {
		fmt.Println(fmt.Sprintf("Cannot delete user by %s", m))
	} else {
		fmt.Println(fmt.Sprintf("delete user by %s", m))
	}
	return err
}
func _ensureDbIndexes(session *mgo.Session) {

	sessions := session.DB(_cfg.Database).C(SESSIONS_COLLECTION)
	expireSessionsIndex := mgo.Index{}
	//TODO: should read from the sessions config
	//or be invoked from the bootstrapper
	expireSessionsIndex.ExpireAfter = 1 * time.Hour
	expireSessionsIndex.Key = []string{"expiry"}
	sessions.EnsureIndex(expireSessionsIndex)
}
Example #17
0
File: dal_mgo.go Project: jsli/cms
func saveUser(session *mgo.Session, obj interface{}) error {
	uc := session.DB(DbName).C(CollectionName)
	err := uc.Insert(obj)
	if err != nil {
		fmt.Println("cannot insert ", obj.(string), ", cause of ", err)
		return errors.New(fmt.Sprintf("Save User failed: %s\n", obj.(string)))
	} else {
		return nil
	}
}
Example #18
0
// newUser creates a new user in the database
func newUser(session *mgo.Session, username, password, email string) (*User, error) {
	pHash, err := bcrypt.GenerateFromPassword([]byte(password), 10)
	if err != nil {
		return nil, err
	}
	user := &User{Id: username, Password: pHash, Email: email}
	c := session.DB(database).C(users)
	err = c.Insert(user)
	return user, err
}
Example #19
0
func main() {
	var (
		mongoSession *mgo.Session
		database     *mgo.Database
		collection   *mgo.Collection
		err          error
	)

	if mongoSession, err = mgo.Dial("localhost"); err != nil {
		panic(err)
	}

	database = mongoSession.DB("mgo_examples_05")
	collection = database.C("todos")

	var todos []Todo
	todos = append(todos, Todo{Id: bson.NewObjectId(), Task: "First task for today", Created: time.Now(), Due: time.Now().Add(time.Hour * 24)})
	todos = append(todos, Todo{Id: bson.NewObjectId(), Task: "Second task for today", Created: time.Now(), Due: time.Now()})
	todos = append(todos, Todo{Id: bson.NewObjectId(), Task: "Third task for today", Created: time.Now(), Due: time.Now()})
	todos = append(todos, Todo{Id: bson.NewObjectId(), Task: "Fourth task for today", Created: time.Now(), Due: time.Now()})
	todos = append(todos, Todo{Id: bson.NewObjectId(), Task: "Fifth task for today", Created: time.Now(), Due: time.Now()})

	for _, todo := range todos {
		if _, err = collection.UpsertId(todo.Id, &todo); err != nil {
			panic(err)
		}
	}
	// START OMIT
	pipeline := []bson.M{
		{"$group": bson.M{
			"_id":   bson.M{"$dayOfYear": "$d"},
			"count": bson.M{"$sum": 1},
		}},
	}

	var (
		result  TodoDueCounts
		results []TodoDueCounts
	)

	iter := collection.Pipe(pipeline).Iter()
	for {
		if iter.Next(&result) {
			results = append(results, result)
		} else {
			break
		}
	}
	err = iter.Err()
	if err != nil {
		panic(err)
	}
	// END OMIT
	fmt.Printf("%# v", pretty.Formatter(results))
}
Example #20
0
func (r GameSessionResult) getSessionDetails(m *mgo.Session) GameSessionResult {
	sessionDetails := MongoCreatorName{}
	selection := bson.M{"emailLower": 1}
	c := m.DB(lib.DatabaseName).C("users")
	err := c.FindId(bson.ObjectIdHex(r.Session.Creator)).Select(selection).One(&sessionDetails)
	if err != nil {
		panic(err)
	}
	r.Email = string(sessionDetails.Email)
	return r
}
Example #21
0
// returns true if the collection already exists. Does so
// by querying db.system.namespaces to see if the collection
// is listed
func collectionExists(s *mgo.Session, dbname string, collname string) bool {
	db := s.DB(dbname)
	sysNamespaces := db.C("system.namespaces")
	coll := db.C(collname)
	q := sysNamespaces.Find(bson.M{"name": coll.FullName})
	n, err := q.Count()
	if err != nil {
		log.Fatal("Received error ", err, " when querying system.namespaces, exiting")
	}
	return n > 0
}
Example #22
0
func listColls(session *mgo.Session) {
	db := session.DB(mongoDb)

	cNames, err := db.CollectionNames()
	if err != nil {
		panic(err)
	}
	for i := 0; i < len(cNames); i++ {
		fmt.Println(cNames[i])
	}
}
Example #23
0
func Collection(m interface{}, s *mgo.Session) *mgo.Collection {
	typ := reflect.TypeOf(m).Elem()
	n := typ.Name()

	var found bool
	var c string
	if c, found = revel.Config.String("bloggo.db.collection." + n); !found {
		c = n
	}
	return s.DB(app.DB).C(c)
}
Example #24
0
func processDidMonthlyAnalytics(session *mgo.Session, cdr RawCall) (err error) {
	//
	var collectionName = ""
	var dst = cdr.Did
	collectionName = "monthlydid_incomming"
	//
	var id = fmt.Sprintf("%04d%02d-%s-%d", cdr.Calldate.Year(),
		cdr.Calldate.Month(), dst, cdr.Disposition)
	//
	var metaDate = time.Date(cdr.Calldate.Year(), cdr.Calldate.Month(),
		1, 1, 0, 0, 0, time.UTC)
	//

	var collection = session.DB(config.MongoDbName).C(collectionName)
	metaDoc := MetaData{Dst: dst, Disposition: cdr.Disposition, Dt: metaDate}

	doc := MonthlyCall{Id: id, Meta: metaDoc, AnswereWaitTime: 0,
		CallMonthly: 0, DurationMonthly: 0}
	//
	var selector = bson.M{"_id": id, "metadata": metaDoc}
	//
	var callsDailyInc = fmt.Sprintf("calls_per_days.%d", cdr.Calldate.Day())
	var durationsDailyInc = fmt.Sprintf("durations_per_days.%d", cdr.Calldate.Day())
	//
	var change = mgo.Change{
		Update: bson.M{"$inc": bson.M{"calls": 1, "duration": cdr.Billsec,
			"answer_wait_time": cdr.AnswerWaitTime, callsDailyInc: 1, durationsDailyInc: cdr.Billsec},
		},
		ReturnNew: false,
	}
	//
	var info = new(mgo.ChangeInfo)
	info, err = collection.Find(selector).Apply(change, &doc)
	//check if the can execute changes
	if info == nil || info.Updated == 0 {
		log.Debugf("Monthly update can't be executed , get the error: [ %v], Try execute insert.", err)
		err = collection.Insert(doc)
		if err != nil {
			log.Errorf("[did] Monthly insert failed with error : [%v].", err)
			return err
		}
		info, err = collection.Find(selector).Apply(change, &doc)
		if info != nil {
			log.Debugf("Monthly did new record inserted : %s.", doc.Id)
		} else {
			if err != nil {
				log.Debugf("Document [%s] was updated, the update numbers: [%s].\n", doc.Id, info.Updated)
				return err
			}
		}
	}

	return nil
}
Example #25
0
// insertworker calls func insertdoc for specified insert count
func insertworker(s *mgo.Session, ch chan int) {
	c := s.DB("test").C("mongotest")
	defer s.Close()
	for i := 0; i < Insert_Count; i++ {
		err := insertdoc(c, i)
		if err != nil {
			panic(err)
		}
	}
	ch <- 1
}
Example #26
0
File: dal_mgo.go Project: jsli/cms
func GetUserByM(session *mgo.Session, m bson.M) (*User, error) {
	uc := session.DB(DbName).C(CollectionName)
	result := User{}
	err := uc.Find(m).One(&result)
	if err == nil {
		fmt.Println("find user : "******"Cannot find user by %s", m))
	return nil, errors.New(fmt.Sprintf("Cannot find user by %s", m))
}
Example #27
0
func isDid(session *mgo.Session, value string) (err error) {
	collection := session.DB(config.MongoDbName).C("dids")
	var selector = bson.M{"did": value}
	//
	var did Did
	err = collection.Find(selector).One(&did)
	//
	if err == nil && did.Did == value {
		return nil
	}
	return err
}
Example #28
0
func newhandler(w http.ResponseWriter, r *http.Request, session *mgo.Session) {
	c := session.DB("quotewall").C("quotes")
	quote := Quote{}
	quote.Author = r.FormValue("author")
	quote.Text = r.FormValue("text")
	err := c.Insert(quote)

	if err != nil {
		panic(err)
	}
	http.Redirect(w, r, "/", http.StatusFound)
}
Example #29
0
func Collection(m interface{}, s *mgo.Session) *mgo.Collection {
	typ := reflect.TypeOf(m)
	i := strings.LastIndex(typ.String(), ".") + 1
	n := typ.String()[i:len(typ.String())]

	var found bool
	var c string
	if c, found = revel.Config.String("hufen.db.collection." + n); !found {
		c = n
	}
	return s.DB(app.DB).C(c)
}
Example #30
0
func createMongoCdr(session *mgo.Session, cdr RawCall) (err error) {
	collection := session.DB(config.MongoDbName).C("cdrs")
	//
	err = collection.Insert(cdr)
	if err != nil {
		log.Criticalf("Can't insert document: %v", err)
		os.Exit(1)
	} else {
		log.Debugf("Row inserted into mongo database for %s from asterisk [%s]", cdr.ClidName, cdr.AsteriskId)
	}
	return
}