Ejemplo n.º 1
0
// 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
}
Ejemplo n.º 2
0
// 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
}
Ejemplo n.º 3
0
// 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")
}
Ejemplo n.º 4
0
// Finish is called once the controller method completes
func Finish(service *services.Service) {
	defer func() {
		if service.MongoSession != nil {
			mongo.CloseSession(service.UserId, service.MongoSession)
			service.MongoSession = nil
		}
	}()

	tracelog.COMPLETED(service.UserId, "Finish")
}
Ejemplo n.º 5
0
// 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
}
Ejemplo n.º 6
0
// CreateSession creates a connection pool for use
func CreateSession(sessionId string, mode string, sessionName string, hosts []string, databaseName string, username string, password string) (err error) {
	defer helper.CatchPanic(nil, sessionId, "CreateSession")

	tracelog.STARTEDf(sessionId, "CreateSession", "Mode[%s] SessionName[%s] Hosts[%s] DatabaseName[%s] Username[%s]", mode, sessionName, hosts, databaseName, username)

	// Create the database object
	mongoSession := &mongoSession{
		mongoDBDialInfo: &mgo.DialInfo{
			Addrs:    hosts,
			Timeout:  60 * time.Second,
			Database: databaseName,
			Username: username,
			Password: password,
		},
	}

	// Establish the master session
	mongoSession.mongoSession, err = mgo.DialWithInfo(mongoSession.mongoDBDialInfo)
	if err != nil {
		tracelog.COMPLETED_ERROR(err, sessionId, "CreateSession")
		return err
	}

	switch mode {
	case "strong":
		// Reads and writes will always be made to the master server using a
		// unique connection so that reads and writes are fully consistent,
		// ordered, and observing the most up-to-date data.
		// http://godoc.org/labix.org/v2/mgo#Session.SetMode
		mongoSession.mongoSession.SetMode(mgo.Strong, true)
		break

	case "monotonic":
		// Reads may not be entirely up-to-date, but they will always see the
		// history of changes moving forward, the data read will be consistent
		// across sequential queries in the same session, and modifications made
		// within the session will be observed in following queries (read-your-writes).
		// http://godoc.org/labix.org/v2/mgo#Session.SetMode
		mongoSession.mongoSession.SetMode(mgo.Monotonic, true)
	}

	// Have the session check for errors
	// http://godoc.org/labix.org/v2/mgo#Session.SetSafe
	mongoSession.mongoSession.SetSafe(&mgo.Safe{})

	// Add the database to the map
	_This.sessions[sessionName] = mongoSession

	tracelog.COMPLETED(sessionId, "CreateSession")
	return err
}
Ejemplo n.º 7
0
// Station handles the higher level business processing for this API Call
func Station(controller *bc.BaseController, stationId string) {
	defer bc.CatchPanic(controller, "Station")

	tracelog.STARTEDf(controller.UserId, "Station", "StationId[%s]", stationId)

	buoyStation, err := buoyService.FindStation(&controller.Service, stationId)
	if err != nil {
		tracelog.COMPLETED_ERRORf(err, controller.UserId, "Station", "StationId[%s]", stationId)
		controller.ServeError(err)
		return
	}

	controller.Data["json"] = &buoyStation
	controller.ServeJson()

	tracelog.COMPLETED(controller.UserId, "Station")
}
Ejemplo n.º 8
0
// Region handles the higher level business processing for this API Call
func Region(controller *bc.BaseController, region string) {
	defer bc.CatchPanic(controller, "Region")

	tracelog.STARTEDf(controller.UserId, "Region", "Region[%s]", region)

	buoyStations, err := buoyService.FindRegion(&controller.Service, region)
	if err != nil {
		tracelog.COMPLETED_ERRORf(err, controller.UserId, "Region", "Region[%s]", region)
		controller.ServeError(err)
		return
	}

	controller.Data["json"] = &buoyStations
	controller.ServeJson()

	tracelog.COMPLETED(controller.UserId, "Region")
}
Ejemplo n.º 9
0
// Init initializes the local environment
func Init(defaultLocale string) error {
	tracelog.STARTEDf("localize", "Init", "DefaultLocal[%s]", defaultLocale)

	switch defaultLocale {
	default:
		LoadJSON(defaultLocale, loc.En_US)
	}

	// Obtain the default translation function for use
	var err error
	T, err = NewTranslation(defaultLocale)
	if err != nil {
		return err
	}

	tracelog.COMPLETED("localize", "Init")
	return nil
}
Ejemplo n.º 10
0
// 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
}
Ejemplo n.º 11
0
// Init initializes the local environment
func Init(defaultLocale string) error {
	tracelog.STARTEDf("localize", "Init", "DefaultLocal[%s]", defaultLocale)

	switch defaultLocale {
	case "en-US":
		LoadJSON(defaultLocale, En_US)
	default:
		return fmt.Errorf("Unsupported Locale: %s", defaultLocale)
	}

	// Obtain the default translation function for use
	var err error
	T, err = NewTranslation(defaultLocale, defaultLocale)
	if err != nil {
		return err
	}

	tracelog.COMPLETED("localize", "Init")
	return nil
}
Ejemplo n.º 12
0
// CopySession makes a clone of the specified session for client use
func CloneSession(sessionId string, useSession string) (mongoSession *mgo.Session, err error) {
	defer helper.CatchPanic(nil, sessionId, "CopySession")

	tracelog.STARTEDf(sessionId, "CloneSession", "UseSession[%s]", useSession)

	// Find the session object
	session := _This.sessions[useSession]

	if session == nil {
		err = fmt.Errorf("Unable To Locate Session %s", useSession)
		tracelog.COMPLETED_ERROR(err, sessionId, "CloneSession")
		return mongoSession, err
	}

	// Clone the master session
	mongoSession = session.mongoSession.Clone()

	tracelog.COMPLETED(sessionId, "CloneSession")
	return mongoSession, err
}
Ejemplo n.º 13
0
// 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
}
Ejemplo n.º 14
0
// Execute the MongoDB literal function
func Execute(sessionId string, mongoSession *mgo.Session, databaseName string, collectionName string, mongoCall MongoCall) (err error) {
	tracelog.STARTEDf(sessionId, "Execute", "Database[%s] Collection[%s]", databaseName, collectionName)

	// Capture the specified collection
	collection, err := GetCollection(mongoSession, databaseName, collectionName)
	if err != nil {

		tracelog.COMPLETED_ERROR(err, sessionId, "Execute")
		return err
	}

	// Execute the mongo call
	err = mongoCall(collection)
	if err != nil {
		tracelog.COMPLETED_ERROR(err, sessionId, "Execute")
		return err
	}

	tracelog.COMPLETED(sessionId, "Execute")

	return err
}
Ejemplo n.º 15
0
// 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
}
Ejemplo n.º 16
0
// LoadJSON takes a json document of translations and manually
// loads them into the system
func LoadJSON(userLocale string, translationDocument string) error {
	tracelog.STARTEDf("localize", "LoadJSON", "UserLocale[%s] Length[%d]", userLocale, len(translationDocument))

	tranDocuments := []map[string]interface{}{}
	err := json.Unmarshal([]byte(translationDocument), &tranDocuments)
	if err != nil {
		tracelog.COMPLETED_ERRORf(err, "localize", "LoadJSON", "**************>")
		return err
	}

	for _, tranDocument := range tranDocuments {
		tran, err := translation.NewTranslation(tranDocument)
		if err != nil {
			tracelog.COMPLETED_ERROR(err, "localize", "LoadJSON")
			return err
		}

		i18n.AddTranslation(locale.MustNew(userLocale), tran)
	}

	tracelog.COMPLETED("localize", "LoadJSON")
	return nil
}
Ejemplo n.º 17
0
// 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
}
Ejemplo n.º 18
0
// 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
}
Ejemplo n.º 19
0
func Finish(service *services.Service) {
	service.Finish()

	tracelog.COMPLETED(service.UserId, "Finish")
}