func main() { tracelog.Start(tracelog.LEVEL_TRACE) // Init mongo tracelog.STARTED("main", "Initializing Mongo") err := mongo.Startup(helper.MAIN_GO_ROUTINE) if err != nil { tracelog.COMPLETED_ERROR(err, helper.MAIN_GO_ROUTINE, "initApp") os.Exit(1) } beego.Run() tracelog.STARTED(helper.MAIN_GO_ROUTINE, "Website Shutdown") tracelog.Stop() }
// DecodeSlice decodes a JSON document array into a slice of Go native objects func DecodeSlice(doc []byte, sliceObj interface{}, obj interface{}) (bool, error) { tracelog.STARTED("utils", "DecodeSlice") if IsArrayResponse(doc) == false { //decode as struct if err := Decode(doc, obj); err != nil { tracelog.ERROR(err, "utils", "DecodeSlice, Item Not Array, Unable to decode as struct") return false, err } //return false since not an array return false, nil } sliceMap := []map[string]interface{}{} if err := json.Unmarshal(doc, &sliceMap); err != nil { tracelog.ERROR(err, "utils", "DecodeSlice") return false, err } if err := mapstructure.DecodeSlicePath(sliceMap, sliceObj); err != nil { tracelog.ERROR(err, "utils", "DecodeSlice, Decoding Slice Object") return false, err } tracelog.COMPLETED("utils", "DecodeSlice") return true, nil }
// FindStation retrieves the specified station func FindStation(service *services.Service, stationId string) (buoyStation *buoyModels.BuoyStation, err error) { defer helper.CatchPanic(&err, service.UserId, "FindStation") tracelog.STARTED(service.UserId, "FindStation") queryMap := bson.M{"station_id": stationId} buoyStation = &buoyModels.BuoyStation{} err = service.DBAction(Config.Database, "buoy_stations", func(collection *mgo.Collection) error { tracelog.TRACE(service.UserId, "FindStation", "Query : %s", mongo.ToString(queryMap)) return collection.Find(queryMap).One(buoyStation) }) if err != nil { if strings.Contains(err.Error(), "not found") == false { tracelog.COMPLETED_ERROR(err, service.UserId, "FindStation") return buoyStation, err } err = nil } tracelog.COMPLETED(service.UserId, "FindStation") return buoyStation, err }
// 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") }
// Decode takes a JSON document and decodes that into a Go native object func Decode(doc []byte, obj interface{}) error { tracelog.STARTED("utils", "Decode") docMap := map[string]interface{}{} if err := json.Unmarshal(doc, &docMap); err != nil { tracelog.ERROR(err, "utils", "Decode, Building Mapped Doc") return err } return DecodeMap(docMap, obj) }
// init initializes all required packages and systems func init() { tracelog.Start(tracelog.LEVEL_TRACE) // Init mongo tracelog.STARTED("main", "Initializing Mongo") err := mongo.Startup(helper.MAIN_GO_ROUTINE) if err != nil { tracelog.COMPLETED_ERROR(err, helper.MAIN_GO_ROUTINE, "initTesting") return } }
// Shutdown systematically brings the manager down gracefully func Shutdown(sessionId string) (err error) { defer helper.CatchPanic(&err, sessionId, "Shutdown") tracelog.STARTED(sessionId, "Shutdown") // Close the databases for _, session := range _This.sessions { CloseSession(sessionId, session.mongoSession) } tracelog.COMPLETED(sessionId, "Shutdown") return err }
// IsArrayResponse validates if the specified JSON document // is an array or slice func IsArrayResponse(doc []byte) bool { tracelog.STARTED("utils", "IsArrayResponse") docString := string(doc) docSlice := strings.TrimLeft(docString, " ") if len(docSlice) > 0 && string(docSlice[0]) == "[" { tracelog.COMPLETEDf("utils", "IsArrayReponse", "Doc is Array") return true } tracelog.COMPLETEDf("utils", "IsArrayResponse", "Doc is not Array") return false }
// loadResponse parse a response. func loadResponse(resp *http.Response) ([]byte, error) { tracelog.STARTED("http_client", "loadResponse") contents, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } defer resp.Body.Close() tracelog.INFO("yodlee_api", "loadResponse", "Api Response => \n\n %s \n\n", contents) if resp.StatusCode != 200 { return nil, errors.New(string(contents)) } tracelog.COMPLETED("http_client", "loadResponse") return contents, err }
// FindRegion retrieves the stations for the specified region func FindRegion(service *services.Service, region string) (buoyStations []*buoyModels.BuoyStation, err error) { defer helper.CatchPanic(&err, service.UserId, "FindRegion") tracelog.STARTED(service.UserId, "FindRegion") queryMap := bson.M{"region": region} tracelog.TRACE(service.UserId, "FindRegion", "Query : %s", mongo.ToString(queryMap)) buoyStations = []*buoyModels.BuoyStation{} err = service.DBAction(Config.Database, "buoy_stations", func(collection *mgo.Collection) error { return collection.Find(queryMap).All(&buoyStations) }) if err != nil { tracelog.COMPLETED_ERROR(err, service.UserId, "FindRegion") return buoyStations, err } tracelog.COMPLETED(service.UserId, "FindRegion") return buoyStations, err }
// IsTokenValid checks whether a token is valid for a Secure Entity. func IsTokenValid(secureEntity SecureEntity, token string) error { tracelog.STARTED("Utils", "IsValidToken") decodedToken, err := base64.StdEncoding.DecodeString(token) if err != nil { tracelog.ERRORf(err, "Utils", "Utils.IsValidToken", "Error Decoding Passed In Token, %s", token) return err } entityToken, tErr := secureEntity.TokenBytes() if tErr != nil { tracelog.ERRORf(tErr, "Utils", "Utils.IsValidToken", "Error Generating Token for Entity") return tErr } if hmac.Equal(decodedToken, entityToken) == false { tracelog.ERRORf(err, "Utils", "Utils.IsValidToken", "Invalid Token Comparison,Tokens Are not the same, Invalid Token, entity[%s], decoded[%s]", string(entityToken), string(decodedToken)) return errors.New("Invalid Token") } tracelog.COMPLETED("Utils", "IsValidToken, Token Is Valid") return nil }
// Startup brings the manager to a running state func Startup(sessionId string) (err error) { defer helper.CatchPanic(&err, sessionId, "Startup") // If the system has already been started ignore the call if _This != nil { return err } tracelog.STARTED(sessionId, "Startup") // Pull in the configuration config := mongoConfiguration{} err = envconfig.Process("mgo", &config) if err != nil { tracelog.COMPLETED_ERROR(err, sessionId, "Startup") return err } // Create the Mongo Manager _This = &mongoManager{ sessions: map[string]*mongoSession{}, } // Log the mongodb connection straps tracelog.TRACE(sessionId, "Startup", "MongoDB : Hosts[%s]", config.Hosts) tracelog.TRACE(sessionId, "Startup", "MongoDB : Database[%s]", config.Database) tracelog.TRACE(sessionId, "Startup", "MongoDB : Username[%s]", config.UserName) hosts := strings.Split(config.Hosts, ",") // Create the strong and monotonic sessions err = CreateSession(sessionId, "strong", MASTER_SESSION, hosts, config.Database, config.UserName, config.Password) err = CreateSession(sessionId, "monotonic", MONOTONIC_SESSION, hosts, config.Database, config.UserName, config.Password) tracelog.COMPLETED(sessionId, "Startup") return err }
// FindStation retrieves the specified station func FindStation(service *services.Service, stationId string) (buoyStation *buoyModels.BuoyStation, err error) { defer helper.CatchPanic(&err, service.UserId, "FindStation") tracelog.STARTED(service.UserId, "FindStation") // Find the specified station id queryMap := bson.M{"station_id": stationId} tracelog.TRACE(service.UserId, "FindStation", "Query : %s", mongo.ToString(queryMap)) // Execute the query buoyStation = &buoyModels.BuoyStation{} err = service.DBAction(Config.Database, "buoy_stations", func(collection *mgo.Collection) error { return collection.Find(queryMap).One(buoyStation) }) if err != nil { tracelog.COMPLETED_ERROR(err, service.UserId, "FindStation") return buoyStation, err } tracelog.COMPLETED(service.UserId, "FindStation") return buoyStation, err }