// Get the list of all SSH keys allowed to access a box. func getKeys(session *mgo.Session, boxname string) (boxUsers, usernames []string, keys string) { this_session := session.Clone() defer this_session.Close() db := this_session.DB("") staff := getStaff(db) boxUsers = usersFromBox(db, boxname) usernames = combineUsers(staff, boxUsers) // returned // Looks through `canBeReally`. keySlice := allKeysFromUsernames(db, usernames) for _, k := range keySlice { k = strings.Replace(k, "\n", "", -1) if !strings.HasPrefix(k, "ssh-") && !strings.HasPrefix(k, "#") { keys += "# NOT VAILD: " } keys += k + "\n" } return }
// CloseSession puts the connection back into the pool func CloseSession(sessionId string, mongoSession *mgo.Session) { defer helper.CatchPanic(nil, sessionId, "CloseSession") tracelog.Started(sessionId, "CloseSession") mongoSession.Close() tracelog.Completed(sessionId, "CloseSession") }
// New returns a new mongo store func New(session mgo.Session, database string, collection string, maxAge int, ensureTTL bool, keyPairs ...[]byte) nSessions.Store { if ensureTTL { conn := session.Clone() defer conn.Close() db := conn.DB(database) c := db.C(collection) c.EnsureIndex(mgo.Index{ Key: []string{"modified"}, Background: true, Sparse: true, ExpireAfter: time.Duration(maxAge) * time.Second, }) } return &mongoStore{ Codecs: securecookie.CodecsFromPairs(keyPairs...), Token: nSessions.NewCookieToken(), session: session, database: database, collection: collection, options: &gSessions.Options{ MaxAge: maxAge, }, } }
// 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 }
// 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 }
// 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 }
// Initiate sets up a replica set with the given replica set name with the // single given member. It need be called only once for a given mongo replica // set. The tags specified will be added as tags on the member that is created // in the replica set. // // Note that you must set DialWithInfo and set Direct = true when dialing into a // specific non-initiated mongo server. // // See http://docs.mongodb.org/manual/reference/method/rs.initiate/ for more // details. func Initiate(session *mgo.Session, address, name string, tags map[string]string) error { monotonicSession := session.Clone() defer monotonicSession.Close() monotonicSession.SetMode(mgo.Monotonic, true) cfg := Config{ Name: name, Version: 1, Members: []Member{{ Id: 1, Address: address, Tags: tags, }}, } logger.Infof("Initiating replicaset with config %#v", cfg) var err error for i := 0; i < maxInitiateAttempts; i++ { err = monotonicSession.Run(bson.D{{"replSetInitiate", cfg}}, nil) if err != nil && err.Error() == rsMembersUnreachableError { time.Sleep(initiateAttemptDelay) continue } break } return err }
/* * makeHandler warps around handlers providing them with a session, and automatically closing that session * to prevent loosing memory. */ func makeHandler(fn func(http.ResponseWriter, *http.Request, *mgo.Session), session *mgo.Session) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { s := session.Copy() // Create a copy of the session fn(w, r, s) s.Close() // Close the copy } }
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)) }
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) } } }
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 }
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)) } }
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) } }
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)) }
// RunQuery is a function that is launched as a goroutine to perform // the MongoDB work. func RunQuery(query int, waitGroup *sync.WaitGroup, mongoSession *mgo.Session) { // Decrement the wait group count so the program knows this // has been completed once the goroutine exits. defer waitGroup.Done() // Request a socket connection from the session to process our query. // Close the session when the goroutine exits and put the connection back // into the pool. sessionCopy := mongoSession.Copy() defer sessionCopy.Close() // Get a collection to execute the query against. collection := sessionCopy.DB(TestDatabase).C("buoy_stations") log.Printf("RunQuery : %d : Executing\n", query) // Retrieve the list of stations. var buoyStations []BuoyStation err := collection.Find(nil).All(&buoyStations) if err != nil { log.Printf("RunQuery : ERROR : %s\n", err) return } log.Printf("RunQuery : %d : Count[%d]\n", query, len(buoyStations)) }
func FetchStatus(s *Status, ms *mgo.Session, enc encoder.Encoder, r *http.Request) (int, []byte) { s.Log["MongoDB"] = NewLog(true, "") if err := ms.Ping(); err != nil { s.Log["MongoDB"] = NewLog(false, err.Error()) } return http.StatusOK, encoder.Must(enc.Encode(s)) }
func CloseSession(s *mgo.Session) { defer func() { if err := recover(); err != nil { log.Print("[MGO2_CLOSE_SESSION_RECOVER] close session panic", err) } }() s.Close() }
// CurrentStatus returns the status of the replica set for the given session. func CurrentStatus(session *mgo.Session) (*Status, error) { status := &Status{} err := session.Run("replSetGetStatus", status) if err != nil { return nil, fmt.Errorf("cannot get replica set status: %v", err) } return status, nil }
// 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 }
// IsMaster returns information about the configuration of the node that // the given session is connected to. func IsMaster(session *mgo.Session) (*IsMasterResults, error) { results := &IsMasterResults{} err := session.Run("isMaster", results) if err != nil { return nil, err } return results, nil }
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) }
// CloseSession puts the connection back into the pool func CloseSession(sessionId string, mongoSession *mgo.Session) { defer helper.CatchPanic(nil, sessionId, "CloseSession") tracelog.STARTED(sessionId, "CloseSession") mongoSession.Close() tracelog.COMPLETED(sessionId, "CloseSession") }
func listDbs(session *mgo.Session) { dbNames, err := session.DatabaseNames() if err != nil { panic(err) } for i := 0; i < len(dbNames); i++ { fmt.Println(dbNames[i]) } }
func getDB(session *mgo.Session) *mgo.Database { var db *mgo.Database if fDev { db = session.DB("dev") } else { db = session.DB("prod") } return db }
func db(session *mgo.Session) martini.Handler { // each request will have its own session return func(c martini.Context) { s := session.Clone() c.Map(s.DB("GoThere")) defer s.Close() c.Next() } }
// test whether session closed // // PS: sometimes it's not corrected func IsSessionClosed(s *mgo.Session) (res bool) { defer func() { if err := recover(); err != nil { log.Print("[MGO2_IS_SESSION_CLOSED] check session closed panic:", err) } }() res = true return s.Ping() != nil }
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 }
// 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 }
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 } }
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 }