cursor nil check, cleaning
All checks were successful
Build Pogdark API / Build Pogdark API (pull_request) Successful in 29s
All checks were successful
Build Pogdark API / Build Pogdark API (pull_request) Successful in 29s
This commit is contained in:
parent
974fc95f2d
commit
1908134133
24
main.go
24
main.go
@ -68,7 +68,7 @@ type MongoConn struct {
|
||||
conn *mongo.Client
|
||||
}
|
||||
|
||||
func getConfig(configPath string) (Config, error) {
|
||||
func getConfig() (Config, error) {
|
||||
viper.SetConfigName("config") // name of config file (without extension)
|
||||
viper.SetConfigType("yaml")
|
||||
viper.AddConfigPath(".") // optionally look for config in the working directory
|
||||
@ -87,7 +87,7 @@ func getConfig(configPath string) (Config, error) {
|
||||
}
|
||||
|
||||
func loadConfig() Config {
|
||||
cfg, err := getConfig("config.yaml")
|
||||
cfg, err := getConfig()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@ -114,11 +114,15 @@ func newServer() *Server {
|
||||
}
|
||||
|
||||
func newMongo(cfg Config) *MongoConn {
|
||||
clientOpts := options.Client().ApplyURI("mongodb://" + cfg.Mongo.Host + ":" + cfg.Mongo.Port)
|
||||
if !(cfg.Mongo.User == "" && cfg.Mongo.Pass == "") {
|
||||
log.Println("WARNING: MongoDB user/pass is set")
|
||||
credential := options.Credential{
|
||||
Username: cfg.Mongo.User,
|
||||
Password: cfg.Mongo.Pass,
|
||||
}
|
||||
clientOpts := options.Client().ApplyURI("mongodb://" + cfg.Mongo.Host + ":" + cfg.Mongo.Port).SetAuth(credential)
|
||||
clientOpts.SetAuth(credential)
|
||||
}
|
||||
client, err := mongo.Connect(ctx, clientOpts)
|
||||
//mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
|
||||
if err != nil {
|
||||
@ -391,9 +395,9 @@ func parkLookup(w http.ResponseWriter, r *http.Request, mongo *MongoConn) {
|
||||
Lon float32 `json:"Lon"`
|
||||
Dist float32 `json:"Dist"`
|
||||
}
|
||||
err := json.NewDecoder(r.Body).Decode(&request)
|
||||
if err != nil {
|
||||
if json.NewDecoder(r.Body).Decode(&request) != nil {
|
||||
http.Error(w, "Cannot decode request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Only POST method is allowed", http.StatusMethodNotAllowed)
|
||||
@ -405,10 +409,7 @@ func parkLookup(w http.ResponseWriter, r *http.Request, mongo *MongoConn) {
|
||||
Find(ctx, bson.D{{"location", bson.D{{"$near", bson.D{{"$geometry", bson.D{
|
||||
{"type", "Point"}, {"coordinates", bson.A{request.Lat, request.Lon}}}},
|
||||
{"$minDistance", 0}, {"$maxDistance", request.Dist * 1609.344}}}}}})
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to find parks", http.StatusInternalServerError)
|
||||
}
|
||||
if cursor.Err() != nil {
|
||||
if err != nil || cursor.Err() != nil {
|
||||
http.Error(w, "Failed to find parks", http.StatusInternalServerError)
|
||||
}
|
||||
type Result struct {
|
||||
@ -423,12 +424,13 @@ func parkLookup(w http.ResponseWriter, r *http.Request, mongo *MongoConn) {
|
||||
} `json:"location"`
|
||||
}
|
||||
var totalResult []Result
|
||||
if cursor.RemainingBatchLength() > 0 {
|
||||
|
||||
if cursor != nil && cursor.RemainingBatchLength() > 0 {
|
||||
log.Printf("Found %d parks", cursor.RemainingBatchLength())
|
||||
|
||||
for cursor.Next(ctx) {
|
||||
var result Result
|
||||
log.Printf(cursor.Current.String())
|
||||
//log.Printf(cursor.Current.String())
|
||||
if err := cursor.Decode(&result); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user