Skip to content

Commit abaac4e

Browse files
authored
Merge pull request #11 from speedscale/fix/nasa-env-fallback
fix NASA client env fallback
2 parents 90d0c04 + ca11b4d commit abaac4e

2 files changed

Lines changed: 23 additions & 17 deletions

File tree

lib/handlers.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ func HandleLaunchesSummary(client SpaceXClientInterface) http.HandlerFunc {
116116
func HandleRoot() http.HandlerFunc {
117117
return LoggingMiddleware(func(w http.ResponseWriter, r *http.Request) {
118118
endpoints := map[string]string{
119-
"/": "Shows this list of available endpoints",
120-
"/api/latest-launch": "Get the latest SpaceX launch",
121-
"/api/rocket": "Get a specific rocket by ID (use ?id=[rocket_id])",
122-
"/api/rockets": "Get a list of all SpaceX rockets",
123-
"/api/numbers": "Get a random math fact",
124-
"/api/nasa": "Get NASA's Astronomy Picture of the Day",
125-
"/api/launches-summary": "Get summary of launches by year and ship over the last 3 years",
119+
"/": "Shows this list of available endpoints",
120+
"/api/latest-launch": "Get the latest SpaceX launch",
121+
"/api/rocket": "Get a specific rocket by ID (use ?id=[rocket_id])",
122+
"/api/rockets": "Get a list of all SpaceX rockets",
123+
"/api/numbers": "Get a random math fact",
124+
"/api/nasa": "Get NASA's Astronomy Picture of the Day",
125+
"/api/launches-summary": "Get summary of launches by year and ship over the last 3 years",
126126
}
127127

128128
w.Header().Set("Content-Type", "application/json")

lib/nasa.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7+
"os"
78
"time"
89

910
"github.com/rs/zerolog/log"
@@ -18,22 +19,27 @@ type NASAClient struct {
1819

1920
// Response structures for NASA API
2021
type APOD struct {
21-
Title string `json:"title"`
22-
Date string `json:"date"`
23-
Explanation string `json:"explanation"`
24-
URL string `json:"url"`
25-
MediaType string `json:"media_type"`
22+
Title string `json:"title"`
23+
Date string `json:"date"`
24+
Explanation string `json:"explanation"`
25+
URL string `json:"url"`
26+
MediaType string `json:"media_type"`
2627
ServiceVersion string `json:"service_version"`
2728
}
2829

29-
// NewNASAClient creates a new NASA API client
30+
// NewNASAClient creates a new NASA API client.
31+
// Uses NASA_API_KEY env if set; otherwise falls back to DEMO_KEY (strict rate limits).
3032
func NewNASAClient() *NASAClient {
33+
apiKey := os.Getenv("NASA_API_KEY")
34+
if apiKey == "" {
35+
apiKey = "DEMO_KEY"
36+
}
3137
return &NASAClient{
3238
baseURL: "https://api.nasa.gov",
3339
httpClient: &http.Client{
3440
Timeout: time.Second * 10,
3541
},
36-
apiKey: "DEMO_KEY", // Using demo key for simplicity
42+
apiKey: apiKey,
3743
}
3844
}
3945

@@ -93,12 +99,12 @@ func (c *NASAClient) GetAPOD() (*APOD, error) {
9399
return nil, fmt.Errorf("NASA API rate limit exceeded")
94100
}
95101
}
96-
102+
97103
// Also check for 429 status code (Too Many Requests)
98104
if resp.StatusCode == http.StatusTooManyRequests {
99105
return nil, fmt.Errorf("NASA API rate limit exceeded (429)")
100106
}
101-
107+
102108
// Check for other non-success status codes
103109
if resp.StatusCode != http.StatusOK {
104110
return nil, fmt.Errorf("NASA API error: HTTP %d", resp.StatusCode)
@@ -109,4 +115,4 @@ func (c *NASAClient) GetAPOD() (*APOD, error) {
109115
return nil, err
110116
}
111117
return &apod, nil
112-
}
118+
}

0 commit comments

Comments
 (0)