// 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 }
// TestInvalidStation is a sample to run an endpoint test that returns // an empty result set func TestInvalidStation(t *testing.T) { r, _ := http.NewRequest("GET", "/buoy/station/000000", nil) w := httptest.NewRecorder() beego.BeeApp.Handlers.ServeHTTP(w, r) tracelog.TRACE("testing", "TestStation", "Code[%d]\n%s", w.Code, w.Body.String()) response := struct { StationId string `json:"station_id"` Name string `json:"name"` LocDesc string `json:"location_desc"` Condition struct { Type string `json:"type"` Coordinates []float64 `json:"coordinates"` } `json:"condition"` Location struct { WindSpeed float64 `json:"wind_speed_milehour"` WindDirection int `json:"wind_direction_degnorth"` WindGust float64 `json:"gust_wind_speed_milehour"` } `json:"location"` }{} json.Unmarshal(w.Body.Bytes(), &response) Convey("Subject: Test Station Endpoint\n", t, func() { Convey("Status Code Should Be 200", func() { So(w.Code, ShouldEqual, 200) }) Convey("The Result Should Not Be Empty", func() { So(w.Body.Len(), ShouldBeGreaterThan, 0) }) Convey("The Result Should Be Empty For Station 00000", func() { So(response.StationId, ShouldBeBlank) }) }) }
// LogModel writes a specified model object into the logs // as a formatted JSON document func LogModel(obj interface{}, useTrace bool) { bArray, _ := json.MarshalIndent(obj, "", " ") if useTrace { tracelog.TRACE("utils", "LogModel", "Obj => \n\n%s\n\n", string(bArray)) } else { tracelog.INFO("utils", "LogModel", "Obj => \n\n%s\n\n", string(bArray)) } }
// SignedHash generates a Signed SHA256 Hash func SignedHash(message string, keyToSign string) []byte { if keyToSign == "" { tracelog.TRACE("go-common/crypto", "SignedHash", "KeyString is Blank") keyToSign = key } h := hmac.New(sha256.New, []byte(keyToSign)) h.Write([]byte(message)) return h.Sum(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 }
func Prepare() *services.Service { service := &services.Service{} // TODO: Add Test User To Environment service.UserId = "testing" err := service.Prepare() if err != nil { tracelog.ERROR(err, service.UserId, "Prepare") return nil } tracelog.TRACE(service.UserId, "Before", "UserId[%s]", service.UserId) return service }
// Prepare is called create a service object func Prepare() *services.Service { service := &services.Service{} service.UserId = "testing" // TODO: Deal With This Later tracelog.TRACE(service.UserId, "Before", "UserId[%s]", service.UserId) var err error service.MongoSession, err = mongo.CopyMonotonicSession(service.UserId) if err != nil { tracelog.ERROR(err, service.UserId, "Before") return nil } return service }
// Prepare is called prior to the baseController method func (baseController *BaseController) Prepare() { baseController.UserId = baseController.GetString("userId") if baseController.UserId == "" { baseController.UserId = baseController.GetString(":userId") } if baseController.UserId == "" { baseController.UserId = "Unknown" } err := baseController.Service.Prepare() if err != nil { tracelog.ERRORf(err, baseController.UserId, "BaseController.Prepare", baseController.Ctx.Request.URL.Path) baseController.ServeError(err) return } tracelog.TRACE(baseController.UserId, "BaseController.Prepare", "UserId[%s] Path[%s]", baseController.UserId, baseController.Ctx.Request.URL.Path) }
// Prepare is called prior to the controller method func (this *BaseController) Prepare() { this.UserId = this.GetString("userId") if this.UserId == "" { this.UserId = this.GetString(":userId") } if this.UserId == "" { this.UserId = "Unknown" } err := this.Service.Prepare() if err != nil { tracelog.ERRORf(err, this.UserId, "BaseController.Prepare", this.Ctx.Request.URL.Path) this.ServeError(err) return } tracelog.TRACE(this.UserId, "BaseController.Prepare", "UserId[%s] Path[%s]", this.UserId, this.Ctx.Request.URL.Path) }
// 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 }
// TestRegion is a sample to run an endpoint test func TestRegion(t *testing.T) { r, _ := http.NewRequest("GET", "/region/Gulf%20Of%20Mexico", nil) w := httptest.NewRecorder() beego.BeeApp.Handlers.ServeHTTP(w, r) tracelog.TRACE("testing", "TestRegion", "Code[%d]\n%s", w.Code, w.Body.String()) err := struct { Error string }{} json.Unmarshal(w.Body.Bytes(), &err) Convey("Subject: Test Region Endpoint\n", t, func() { Convey("Status Code Should Be 200", func() { So(w.Code, ShouldEqual, 200) }) Convey("The Result Should Not Be Empty", func() { So(w.Body.Len(), ShouldBeGreaterThan, 0) }) Convey("The Should Be No Error In The Result", func() { So(len(err.Error), ShouldEqual, 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 }
// TestInvalidStation is a sample to run an endpoint test that returns // an empty result set func TestMissingStation(t *testing.T) { r, _ := http.NewRequest("GET", "/buoy/station/420", nil) w := httptest.NewRecorder() beego.BeeApp.Handlers.ServeHTTP(w, r) tracelog.TRACE("testing", "TestStation", "Code[%d]\n%s", w.Code, w.Body.String()) err := struct { Errors []string `json:"errors"` }{} json.Unmarshal(w.Body.Bytes(), &err) Convey("Subject: Test Station Endpoint\n", t, func() { Convey("Status Code Should Be 409", func() { So(w.Code, ShouldEqual, 409) }) Convey("The Result Should Not Be Empty", func() { So(w.Body.Len(), ShouldBeGreaterThan, 0) }) Convey("The Should Be An Error In The Result", func() { So(len(err.Errors), ShouldEqual, 1) }) }) }