|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "strconv" |
| 10 | + |
| 11 | + "github.com/gorilla/mux" |
| 12 | + "github.com/mtepenner/uav-flight-optimizer/environment_service/internal/airspace" |
| 13 | + "github.com/mtepenner/uav-flight-optimizer/environment_service/internal/topography" |
| 14 | + "github.com/mtepenner/uav-flight-optimizer/environment_service/internal/weather" |
| 15 | +) |
| 16 | + |
| 17 | +func main() { |
| 18 | + port := os.Getenv("PORT") |
| 19 | + if port == "" { |
| 20 | + port = "8081" |
| 21 | + } |
| 22 | + |
| 23 | + windService := weather.NewWindVectorService() |
| 24 | + elevationService := topography.NewElevationService() |
| 25 | + geofenceService := airspace.NewGeofenceService() |
| 26 | + |
| 27 | + r := mux.NewRouter() |
| 28 | + |
| 29 | + // Health check |
| 30 | + r.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { |
| 31 | + w.Header().Set("Content-Type", "application/json") |
| 32 | + json.NewEncoder(w).Encode(map[string]string{"status": "healthy"}) |
| 33 | + }).Methods("GET") |
| 34 | + |
| 35 | + // Wind vectors endpoint |
| 36 | + r.HandleFunc("/api/v1/wind", func(w http.ResponseWriter, r *http.Request) { |
| 37 | + lat, _ := strconv.ParseFloat(r.URL.Query().Get("lat"), 64) |
| 38 | + lon, _ := strconv.ParseFloat(r.URL.Query().Get("lon"), 64) |
| 39 | + alt, _ := strconv.ParseFloat(r.URL.Query().Get("alt"), 64) |
| 40 | + radius, _ := strconv.ParseFloat(r.URL.Query().Get("radius"), 64) |
| 41 | + if radius == 0 { |
| 42 | + radius = 10.0 |
| 43 | + } |
| 44 | + |
| 45 | + grid := windService.GetWindGrid(lat, lon, alt, radius) |
| 46 | + w.Header().Set("Content-Type", "application/json") |
| 47 | + json.NewEncoder(w).Encode(grid) |
| 48 | + }).Methods("GET") |
| 49 | + |
| 50 | + // Elevation endpoint |
| 51 | + r.HandleFunc("/api/v1/elevation", func(w http.ResponseWriter, r *http.Request) { |
| 52 | + lat, _ := strconv.ParseFloat(r.URL.Query().Get("lat"), 64) |
| 53 | + lon, _ := strconv.ParseFloat(r.URL.Query().Get("lon"), 64) |
| 54 | + radius, _ := strconv.ParseFloat(r.URL.Query().Get("radius"), 64) |
| 55 | + resolution, _ := strconv.ParseFloat(r.URL.Query().Get("resolution"), 64) |
| 56 | + if resolution == 0 { |
| 57 | + resolution = 30.0 |
| 58 | + } |
| 59 | + if radius == 0 { |
| 60 | + radius = 10.0 |
| 61 | + } |
| 62 | + |
| 63 | + grid := elevationService.GetElevationGrid(lat, lon, radius, resolution) |
| 64 | + w.Header().Set("Content-Type", "application/json") |
| 65 | + json.NewEncoder(w).Encode(grid) |
| 66 | + }).Methods("GET") |
| 67 | + |
| 68 | + // Geofence endpoint |
| 69 | + r.HandleFunc("/api/v1/geofences", func(w http.ResponseWriter, r *http.Request) { |
| 70 | + lat, _ := strconv.ParseFloat(r.URL.Query().Get("lat"), 64) |
| 71 | + lon, _ := strconv.ParseFloat(r.URL.Query().Get("lon"), 64) |
| 72 | + radius, _ := strconv.ParseFloat(r.URL.Query().Get("radius"), 64) |
| 73 | + if radius == 0 { |
| 74 | + radius = 50.0 |
| 75 | + } |
| 76 | + |
| 77 | + zones := geofenceService.GetGeofences(lat, lon, radius) |
| 78 | + w.Header().Set("Content-Type", "application/json") |
| 79 | + json.NewEncoder(w).Encode(zones) |
| 80 | + }).Methods("GET") |
| 81 | + |
| 82 | + // Combined environment data for a bounding box |
| 83 | + r.HandleFunc("/api/v1/environment", func(w http.ResponseWriter, r *http.Request) { |
| 84 | + lat, _ := strconv.ParseFloat(r.URL.Query().Get("lat"), 64) |
| 85 | + lon, _ := strconv.ParseFloat(r.URL.Query().Get("lon"), 64) |
| 86 | + alt, _ := strconv.ParseFloat(r.URL.Query().Get("alt"), 64) |
| 87 | + radius, _ := strconv.ParseFloat(r.URL.Query().Get("radius"), 64) |
| 88 | + if radius == 0 { |
| 89 | + radius = 10.0 |
| 90 | + } |
| 91 | + if alt == 0 { |
| 92 | + alt = 100.0 |
| 93 | + } |
| 94 | + |
| 95 | + result := map[string]interface{}{ |
| 96 | + "wind_grid": windService.GetWindGrid(lat, lon, alt, radius), |
| 97 | + "elevation_grid": elevationService.GetElevationGrid(lat, lon, radius, 30.0), |
| 98 | + "geofences": geofenceService.GetGeofences(lat, lon, radius), |
| 99 | + } |
| 100 | + |
| 101 | + w.Header().Set("Content-Type", "application/json") |
| 102 | + json.NewEncoder(w).Encode(result) |
| 103 | + }).Methods("GET") |
| 104 | + |
| 105 | + // CORS middleware |
| 106 | + handler := corsMiddleware(r) |
| 107 | + |
| 108 | + log.Printf("Environment Service starting on port %s", port) |
| 109 | + if err := http.ListenAndServe(fmt.Sprintf(":%s", port), handler); err != nil { |
| 110 | + log.Fatalf("Failed to start server: %v", err) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func corsMiddleware(next http.Handler) http.Handler { |
| 115 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 116 | + w.Header().Set("Access-Control-Allow-Origin", "*") |
| 117 | + w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS") |
| 118 | + w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") |
| 119 | + if r.Method == "OPTIONS" { |
| 120 | + w.WriteHeader(http.StatusOK) |
| 121 | + return |
| 122 | + } |
| 123 | + next.ServeHTTP(w, r) |
| 124 | + }) |
| 125 | +} |
0 commit comments