77 "log"
88 "net/http"
99 "math"
10+ "firebase.google.com/go/auth"
11+ "google.golang.org/api/iterator"
12+ "github.com/google/uuid"
1013 "strconv"
1114)
1215
@@ -95,11 +98,11 @@ type RouteResponse struct{
9598 Routes []RouteDetails `json:"routes"`
9699}
97100
98- func getWaypoints (lat float64 , lang float64 ) []string {
101+ func getWaypoints (lat float64 , lang float64 , dist float64 ) []string {
99102 var waypoints string
100103 var wayPointsArray []string
101- latDiff := 0.3 / 111.0
102- langDiff := 0.3 / (111.0 * math .Cos (lat * math .Pi / 180.0 ))
104+ latDiff := ( 0.12 * dist ) / 111.0
105+ langDiff := ( 0.12 * dist ) / (111.0 * math .Cos (lat * math .Pi / 180.0 ))
103106
104107 waypoints = fmt .Sprintf ("%f,%f|%f,%f|%f,%f" ,
105108 lat + latDiff , lang ,
@@ -109,9 +112,9 @@ func getWaypoints(lat float64, lang float64) []string{
109112 wayPointsArray = append (wayPointsArray , waypoints )
110113
111114 waypoints = fmt .Sprintf ("%f,%f|%f,%f|%f,%f" ,
112- lat - latDiff , lang ,
115+ lat , lang - langDiff ,
113116 lat , lang + langDiff ,
114- lat - latDiff , lang + langDiff ,
117+ lat + latDiff , lang - langDiff ,
115118 )
116119 wayPointsArray = append (wayPointsArray , waypoints )
117120
@@ -149,9 +152,9 @@ func sphericalDistance(lat1, lng1, lat2, lng2 float64) float64 {
149152 return distance
150153}
151154
152- func fetchRoutes (lat float64 , lng float64 ) []RouteDetails {
155+ func fetchRoutes (lat float64 , lng float64 , dist float64 ) []RouteDetails {
153156 var paths []RouteDetails
154- wayPointsArray := getWaypoints (lat , lng )
157+ wayPointsArray := getWaypoints (lat , lng , dist )
155158
156159 for pi := range (wayPointsArray ){
157160 url := getDirectionsURL (lat , lng , wayPointsArray [pi ]);
@@ -173,6 +176,9 @@ func fetchRoutes(lat float64, lng float64) []RouteDetails {
173176 }
174177
175178 var coords []Coordinates
179+ if (len (result .Routes ) == 0 ){
180+ continue ;
181+ }
176182 for j := range (result .Routes [0 ].Legs ){
177183 for s := range (result .Routes [0 ].Legs [j ].Steps ){
178184 pos := result .Routes [0 ].Legs [j ].Steps [s ].EndLocation
@@ -192,26 +198,114 @@ func fetchRoutes(lat float64, lng float64) []RouteDetails {
192198 return paths
193199}
194200
195- func getCenter (routes []RouteDetails ){
201+ func storeRoutes (routes []RouteDetails ){
196202 for i := range (routes ){
197- for j := 0 ; j < len (routes [i ].Coord ); j += 5 {
198- var avgCoord Coordinates
199- avgCoord .Lat = 0.0
200- avgCoord .Lng = 0.0
201- nearCoords := routes [i ].Coord [j :j + 5 ]
203+ routeId := uuid .New ().String ()
204+ for j := 0 ; j < len (routes [i ].Coord ); j += 10 {
205+ var avgCoord struct {
206+ Coord Coordinates
207+ }
208+ avgCoord .Coord .Lat = 0.0
209+ avgCoord .Coord .Lng = 0.0
210+
211+ var nearCoords []Coordinates
212+ if (j + 10 >= len (routes [i ].Coord )){
213+ nearCoords = routes [i ].Coord [j :]
214+ }else {
215+ nearCoords = routes [i ].Coord [j :j + 10 ]
216+ }
202217 for k := range (nearCoords ){
203- avgCoord .Lat = (avgCoord .Lat * float64 (k ) + nearCoords [k ].Lat )/ float64 (k + 1 )
204- avgCoord .Lng = (avgCoord .Lng * float64 (k ) + nearCoords [k ].Lng )/ float64 (k + 1 )
218+ avgCoord .Coord . Lat = (avgCoord . Coord .Lat * float64 (k ) + nearCoords [k ].Lat )/ float64 (k + 1 )
219+ avgCoord .Coord . Lng = (avgCoord . Coord .Lng * float64 (k ) + nearCoords [k ].Lng )/ float64 (k + 1 )
205220 }
206- _ , _ , err := fsClient .Collection ("routes" ).Add (ctx , map [string ]interface {}{
207- "avgCoord" : avgCoord ,
208- "routes" : routes ,
221+
222+ _ , _ , err := fsClient .Collection ("avgCoords" ).Add (ctx , map [string ]interface {}{
223+ "avgCoord" : map [string ]float64 {
224+ "lat" : avgCoord .Coord .Lat ,
225+ "lng" : avgCoord .Coord .Lng ,
226+ },
227+ "routeId" : routeId ,
209228 })
210229 if (err != nil ){
211230 log .Fatalf ("Failed adding routes: %v" , err )
212231 }
232+
233+ }
234+ routeData , err := json .Marshal (routes [i ])
235+ if err != nil {
236+ log .Fatalf ("Failed to marshal routes: %v" , err )
237+ }
238+ _ , _ , err = fsClient .Collection ("routes" ).Add (ctx , map [string ]interface {}{
239+ "routeId" : routeId ,
240+ "route" : string (routeData ),
241+ })
242+ if (err != nil ){
243+ log .Fatalf ("Failed adding routes: %v" , err )
244+ }
245+ }
246+ }
247+
248+ func cachedRoutes (lat float64 , lng float64 ) []RouteDetails {
249+ var routes []RouteDetails
250+
251+ const offset = 0.001
252+
253+ upLat := lat + offset
254+ downLat := lat - offset
255+ leftLng := lng - offset
256+ rightLng := lng + offset
257+
258+ coordinates := []struct {
259+ Lat float64
260+ Lng float64
261+ }{
262+ {upLat , lng },
263+ {downLat , lng },
264+ {lat , rightLng },
265+ {lat , leftLng },
266+ }
267+
268+ iter := fsClient .Collection ("avgCoords" ).
269+ Where ("avgCoord.lat" , "<=" , coordinates [0 ].Lat ).
270+ Where ("avgCoord.lat" , ">=" , coordinates [1 ].Lat ).
271+ Where ("avgCoord.lng" , "<=" , coordinates [2 ].Lng ).
272+ Where ("avgCoord.lng" , ">=" , coordinates [3 ].Lng ).
273+ Documents (ctx )
274+
275+ routeIds := map [string ]bool {}
276+
277+ for {
278+ doc , err := iter .Next ()
279+ if err == iterator .Done {
280+ break
281+ }
282+ if err != nil {
283+ log .Fatalf ("Failed to fetch avgCoords: %v" , err )
213284 }
285+
286+ routeId := doc .Data ()["routeId" ].(string )
287+ routeIds [routeId ] = true
214288 }
289+
290+ for routeId := range routeIds {
291+ routeDoc , err := fsClient .Collection ("routes" ).Where ("routeId" , "==" , routeId ).Documents (ctx ).Next ()
292+ if err == iterator .Done {
293+ continue
294+ }
295+ if err != nil {
296+ log .Fatalf ("Failed to fetch routes for routeId %s: %v" , routeId , err )
297+ }
298+
299+ var route RouteDetails
300+ routeData := routeDoc .Data ()["route" ].(string )
301+ if err := json .Unmarshal ([]byte (routeData ), & route ); err != nil {
302+ log .Fatalf ("Failed to unmarshal route data: %v" , err )
303+ }
304+
305+ routes = append (routes , route )
306+ }
307+
308+ return routes
215309}
216310
217311func setCheckpoints (routes []RouteDetails ) {
@@ -245,18 +339,49 @@ func setCheckpoints(routes []RouteDetails) {
245339 }
246340}
247341
342+ func verifyIDToken (idToken string ) (* auth.Token , error ) {
343+ token , err := authClient .VerifyIDToken (ctx , idToken )
344+ if err != nil {
345+ return nil , err
346+ }
347+ return token , nil
348+ }
349+
248350func getRoutes (w http.ResponseWriter , req * http.Request ){
249351
352+ authHeader := req .Header .Get ("Authorization" )
353+ if authHeader == "" {
354+ http .Error (w , "Missing Authorization Header" , http .StatusUnauthorized )
355+ return
356+ }
357+
358+ token := authHeader [len ("Bearer " ):]
359+
360+ verifiedToken , err := verifyIDToken (token )
361+ if err != nil {
362+ http .Error (w , "Invalid Token" , http .StatusUnauthorized )
363+ return
364+ }
365+
366+ fmt .Printf ("Authenticated user UID: %s\n " , verifiedToken .UID )
367+
250368 queryParams := req .URL .Query ()
251369
252- fmt .Println (GOOGLE_API_KEY )
253-
254370 lat := queryParams .Get ("lat" )
255- latfloat ,_ := strconv .ParseFloat (lat , 64 );
256371 lng := queryParams .Get ("lng" )
372+ dist := queryParams .Get ("dist" )
373+ latfloat ,_ := strconv .ParseFloat (lat , 64 );
257374 lngfloat ,_ := strconv .ParseFloat (lng , 64 );
258-
259- routes := fetchRoutes (latfloat , lngfloat )
375+ distfloat ,_ := strconv .ParseFloat (dist , 64 );
376+
377+
378+ var routes []RouteDetails
379+ isCached := true
380+ routes = cachedRoutes (latfloat , lngfloat )
381+ if (len (routes ) == 0 ){
382+ isCached = false
383+ routes = fetchRoutes (latfloat , lngfloat , distfloat )
384+ }
260385
261386 fmt .Println ("No of paths" , len (routes ))
262387 for i := range (routes ){
@@ -284,7 +409,9 @@ func getRoutes(w http.ResponseWriter, req *http.Request){
284409 return
285410 }
286411
287- //getCenter(routes)
412+ if (isCached == false ){
413+ storeRoutes (routes )
414+ }
288415
289416 w .Header ().Set ("Content-Type" , "application/json" )
290417 w .Write (jsonRes )
0 commit comments