added park lookup api

This commit is contained in:
whysman 2025-04-28 00:10:56 -04:00
parent e22082d152
commit 974fc95f2d

75
main.go
View File

@ -16,6 +16,7 @@ import (
"github.com/gorilla/websocket"
"github.com/spf13/viper"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
@ -353,15 +354,13 @@ func getLocByZip(w http.ResponseWriter, r *http.Request, mongo *MongoConn) {
Zip string `json:"Zip"`
}
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
http.Error(w, "Invalid JSON format", http.StatusBadRequest)
}
if r.Method != http.MethodPost {
http.Error(w, "Only POST method is allowed", http.StatusMethodNotAllowed)
return
}
if err != nil {
http.Error(w, "Invalid JSON format", http.StatusBadRequest)
}
fmt.Println(request)
if request.Zip == "" {
fmt.Println("Missing or empty zip field")
@ -386,6 +385,69 @@ func getLocByZip(w http.ResponseWriter, r *http.Request, mongo *MongoConn) {
}
}
func parkLookup(w http.ResponseWriter, r *http.Request, mongo *MongoConn) {
var request struct {
Lat float32 `json:"Lat"`
Lon float32 `json:"Lon"`
Dist float32 `json:"Dist"`
}
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
http.Error(w, "Cannot decode request", http.StatusBadRequest)
}
if r.Method != http.MethodPost {
http.Error(w, "Only POST method is allowed", http.StatusMethodNotAllowed)
return
}
log.Println(request.Lat, request.Lon, request.Dist)
cursor, err := mongo.conn.Database("geodb").Collection("parks").
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 {
http.Error(w, "Failed to find parks", http.StatusInternalServerError)
}
type Result struct {
Id primitive.ObjectID `bson:"_id"`
Name string `json:"name"`
Address string `json:"address"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
Location struct {
Coordinates []float64 `json:"coordinates"`
} `json:"location"`
}
var totalResult []Result
if cursor.RemainingBatchLength() > 0 {
log.Printf("Found %d parks", cursor.RemainingBatchLength())
for cursor.Next(ctx) {
var result Result
log.Printf(cursor.Current.String())
if err := cursor.Decode(&result); err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", result)
totalResult = append(totalResult, result)
}
if err := cursor.Err(); err != nil {
log.Fatal(err)
}
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(totalResult)
if err != nil {
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
}
}
func enableCORS(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*") // Allow all origins
@ -418,6 +480,9 @@ func main() {
router.HandleFunc("/zipLookup", func(w http.ResponseWriter, r *http.Request) {
getLocByZip(w, r, mongoConn)
}).Methods("POST")
router.HandleFunc("/parkLookup", func(w http.ResponseWriter, r *http.Request) {
parkLookup(w, r, mongoConn)
}).Methods("POST")
corsRouter := enableCORS(router)
// Start server and other necessary goroutines