func Init() { // Read configuration. h := revel.Config.StringDefault("rmgo.host", "localhost") fmt.Println(h) m := revel.Config.StringDefault("rmgo.method", "clone") d := revel.Config.StringDefault("rmgo.database", "test") Config = &MongoConfig{h, m, d} // Let's try to connect to Mongo DB right upon starting revel but don't // raise an error. Errors will be handled if there is actually a request err := Dial() if err != nil { revel.WARN.Printf("Could not connect to Mongo DB. Error: %s", err) } // register the custom bson.ObjectId binder revel.TypeBinders[reflect.TypeOf(bson.NewObjectId())] = revel.Binder{ // Make a ObjectId from a request containing it in string format. Bind: revel.ValueBinder(func(val string, typ reflect.Type) reflect.Value { if len(val) == 0 { return reflect.Zero(typ) } if bson.IsObjectIdHex(val) { objId := bson.ObjectIdHex(val) return reflect.ValueOf(objId) } else { revel.ERROR.Print("Invalid ObjectId") return reflect.Zero(typ) } }), // Turns ObjectId back to hexString for reverse routing Unbind: func(output map[string]string, name string, val interface{}) { hexStr := fmt.Sprintf("%s", val.(bson.ObjectId).Hex()) if bson.IsObjectIdHex(hexStr) { output[name] = hexStr } else { revel.ERROR.Print("Invalid ObjectId") output[name] = "" } }, } }
case "clone", "copy", "new": return nil } return fmt.Errorf("revmgo: Invalid session instantiation method '%s'", m) } // Custom TypeBinder for bson.ObjectId // Makes additional Id parameters in actions obsolete var ObjectIdBinder = revel.Binder{ // Make a ObjectId from a request containing it in string format. Bind: revel.ValueBinder(func(val string, typ reflect.Type) reflect.Value { if len(val) == 0 { return reflect.Zero(typ) } if bson.IsObjectIdHex(val) { objId := bson.ObjectIdHex(val) return reflect.ValueOf(objId) } else { revel.ERROR.Print("ObjectIdBinder.Bind - invalid ObjectId!") return reflect.Zero(typ) } }), // Turns ObjectId back to hexString for reverse routing Unbind: func(output map[string]string, name string, val interface{}) { var hexStr string hexStr = fmt.Sprintf("%s", val.(bson.ObjectId).Hex()) // not sure if this is too carefull but i wouldn't want invalid ObjectIds in my App if bson.IsObjectIdHex(hexStr) { output[name] = hexStr } else { revel.ERROR.Print("ObjectIdBinder.Unbind - invalid ObjectId!") output[name] = ""
type Page struct { No int64 Size int64 Head bool Tail bool Normal bool Selected bool } var ( TimeLocation *time.Location TimeLocationBinder = revel.Binder{ Bind: revel.ValueBinder(func(val string, typ reflect.Type) reflect.Value { for _, f := range revel.TimeFormats { if r, err := time.ParseInLocation(f, val, TimeLocation); err == nil { return reflect.ValueOf(r) } } return reflect.Zero(typ) }), Unbind: func(output map[string]string, name string, val interface{}) { var ( t = val.(time.Time) format = revel.DateTimeFormat h, m, s = t.Clock() ) if h == 0 && m == 0 && s == 0 { format = revel.DateFormat } output[name] = t.Format(format) }, }