func init() { // Pull in the configuration err := envconfig.Process("buoy", &Config) if err != nil { tracelog.COMPLETED_ERROR(err, helper.MAIN_GO_ROUTINE, "Init") } }
// searchDirectory recurses through the specified directory looking // for i18n folders. If found it will load the translations files func searchDirectory(directory string, pwd string) { // Read the directory fileInfos, err := ioutil.ReadDir(directory) if err != nil { tracelog.COMPLETED_ERROR(err, "localize", "searchDirectory") return } // Look for i18n folders for _, fileInfo := range fileInfos { if fileInfo.IsDir() == true { fullPath := fmt.Sprintf("%s/%s", directory, fileInfo.Name()) // If this directory is the current directory, ignore it if fullPath == pwd { continue } // Is this an i18n folder if fileInfo.Name() == "i18n" { loadTranslationFiles(fullPath) continue } // Look for more sub-directories searchDirectory(fullPath, pwd) continue } } }
// 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 }
// 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 } }
// 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 }
// 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 }
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() }
// loadTranslationFiles loads the found translation files into the i18n // messaging system for use by the application func loadTranslationFiles(directory string) { // Read the directory fileInfos, err := ioutil.ReadDir(directory) if err != nil { tracelog.COMPLETED_ERROR(err, "localize", "loadTranslationFiles") return } // Look for JSON files for _, fileInfo := range fileInfos { if path.Ext(fileInfo.Name()) != ".json" { continue } fileName := fmt.Sprintf("%s/%s", directory, fileInfo.Name()) tracelog.INFO("localize", "loadTranslationFiles", "Loading %s", fileName) i18n.MustLoadTranslationFile(fileName) } }
// 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 }
// 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 }
// 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 }
// 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 }
// LoadFiles looks for i18n folders inside the current directory and the GOPATH // to find translation files to load func LoadFiles(userLocale string, defaultLocal string) error { gopath := os.Getenv("GOPATH") pwd, err := os.Getwd() if err != nil { tracelog.COMPLETED_ERROR(err, "localize", "LoadFiles") return err } tracelog.INFO("localize", "LoadFiles", "PWD[%s] GOPATH[%s]", pwd, gopath) // Load any translation files we can find searchDirectory(pwd, pwd) if gopath != "" { searchDirectory(gopath, pwd) } // Create a translation function for use T, err = i18n.Tfunc(userLocale, defaultLocal) if err != nil { return err } return 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") // 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 }