fix: address code review findings
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/hmac"
|
"crypto/subtle"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -435,7 +435,7 @@ func adminAuth(hc *HandlerContext, w http.ResponseWriter, r *http.Request) bool
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
providedAPIKey := r.Header.Get("X-Admin-Api-Key")
|
providedAPIKey := r.Header.Get("X-Admin-Api-Key")
|
||||||
if !hmac.Equal([]byte(providedAPIKey), []byte(expectedAPIKey)) {
|
if subtle.ConstantTimeCompare([]byte(providedAPIKey), []byte(expectedAPIKey)) != 1 {
|
||||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,35 @@ func main() {
|
|||||||
http.HandleFunc("/internal/admin/stations/", makeHandler(AdminStationStatus, handlerCtx))
|
http.HandleFunc("/internal/admin/stations/", makeHandler(AdminStationStatus, handlerCtx))
|
||||||
http.HandleFunc("/metrics", makeHandler(MetricsHandler, handlerCtx))
|
http.HandleFunc("/metrics", makeHandler(MetricsHandler, handlerCtx))
|
||||||
|
|
||||||
|
// User preferences routes
|
||||||
|
http.HandleFunc("/v1/preferences/saved-cities", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.Method {
|
||||||
|
case http.MethodGet:
|
||||||
|
GetSavedCities(handlerCtx, w, r)
|
||||||
|
case http.MethodPost:
|
||||||
|
AddSavedCity(handlerCtx, w, r)
|
||||||
|
default:
|
||||||
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
http.HandleFunc("/v1/preferences/saved-cities/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method == http.MethodDelete {
|
||||||
|
RemoveSavedCity(handlerCtx, w, r)
|
||||||
|
} else {
|
||||||
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
http.HandleFunc("/v1/preferences/search-history", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.Method {
|
||||||
|
case http.MethodGet:
|
||||||
|
GetSearchHistory(handlerCtx, w, r)
|
||||||
|
case http.MethodPost:
|
||||||
|
AddSearchHistory(handlerCtx, w, r)
|
||||||
|
default:
|
||||||
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
log.Println("Trip Planner API starting on :8080")
|
log.Println("Trip Planner API starting on :8080")
|
||||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||||
}
|
}
|
||||||
|
|||||||
7
internal/cache/preferences.go
vendored
7
internal/cache/preferences.go
vendored
@@ -6,13 +6,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PreferenceKey defines the structure for preference cache keys.
|
|
||||||
type PreferenceKey struct {
|
|
||||||
UserID string // user identifier
|
|
||||||
Kind string // "saved_city" or "search_history"
|
|
||||||
CityCode string // city code for saved_city
|
|
||||||
}
|
|
||||||
|
|
||||||
// PreferenceSavedCity represents a user's saved city preference.
|
// PreferenceSavedCity represents a user's saved city preference.
|
||||||
type PreferenceSavedCity struct {
|
type PreferenceSavedCity struct {
|
||||||
CityCode string `json:"city_code"`
|
CityCode string `json:"city_code"`
|
||||||
|
|||||||
@@ -1038,8 +1038,6 @@ func SelectHubStations(stations []StationInfo, minOutgoingFlights int) []*Node {
|
|||||||
return hubs
|
return hubs
|
||||||
}
|
}
|
||||||
|
|
||||||
// getStationNeighbors returns neighboring stations for a given station ID in the same city.
|
|
||||||
|
|
||||||
// RouteStatus represents the current status of a route leg.
|
// RouteStatus represents the current status of a route leg.
|
||||||
type RouteStatus int32
|
type RouteStatus int32
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user