cursor nil check, cleaning
All checks were successful
Build Pogdark API / Build Pogdark API (pull_request) Successful in 29s

This commit is contained in:
whysman 2025-04-28 23:15:03 -04:00
parent 974fc95f2d
commit 1908134133

30
main.go
View File

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