fix: address code review findings

This commit is contained in:
2026-08-18 14:25:34 +03:00
parent 48650d96cf
commit 0d603ad15f
6 changed files with 56 additions and 24 deletions

View File

@@ -205,6 +205,18 @@ func RouteSearch(hc *HandlerContext, w http.ResponseWriter, r *http.Request) {
RankingMode: rankingMode,
}
// Determine if this is a far-term search (date is more than 7 days in the future)
if req.Date != "" {
requestDate, err := time.Parse("2006-01-02", req.Date)
if err == nil {
now := time.Now()
daysDiff := int(requestDate.Sub(now).Hours() / 24)
if daysDiff >= 7 {
opts.FarTerm = true
}
}
}
// Detect closed stations and get neighbors for fallback
closedStationsMap := make(map[string]bool)
neighborsMap := make(map[string][]storage.StationNeighbor)
@@ -431,7 +443,7 @@ func StationStatus(hc *HandlerContext, w http.ResponseWriter, r *http.Request) {
func adminAuth(hc *HandlerContext, w http.ResponseWriter, r *http.Request) bool {
expectedAPIKey := os.Getenv("TRIP_PLANNER_ADMIN_API_KEY")
if expectedAPIKey == "" {
http.Error(w, "unauthorized", http.StatusUnauthorized)
http.Error(w, "server configuration error: TRIP_PLANNER_ADMIN_API_KEY is not set", http.StatusInternalServerError)
return false
}
providedAPIKey := r.Header.Get("X-Admin-Api-Key")

View File

@@ -131,12 +131,17 @@ func (sm *StationMonitor) updateStationStatus(ctx context.Context, tripCount int
zeroSinceData, err := sm.Cache.Get(ctx, zeroSinceKey)
var zeroSince time.Time
if err == nil && zeroSinceData != nil {
var zeroSinceUnix int64
_, parseErr := fmt.Sscanf(string(zeroSinceData), "%d", &zeroSinceUnix)
if parseErr == nil {
zeroSince = time.Unix(zeroSinceUnix, 0)
} else {
// Handle "0" marker for time.Time{} (no zero-since)
if string(zeroSinceData) == "0" {
zeroSince = time.Time{}
} else {
var zeroSinceUnix int64
_, parseErr := fmt.Sscanf(string(zeroSinceData), "%d", &zeroSinceUnix)
if parseErr == nil {
zeroSince = time.Unix(zeroSinceUnix, 0)
} else {
zeroSince = time.Time{}
}
}
}
@@ -184,7 +189,12 @@ func (sm *StationMonitor) updateStationStatus(ctx context.Context, tripCount int
}
// Write updated zero-since timestamp to cache with 24h TTL
if err := sm.Cache.Set(ctx, zeroSinceKey, []byte(fmt.Sprintf("%d", zeroSince.Unix())), 24*time.Hour); err != nil {
// Use "0" marker for time.Time{} to indicate no zero-since
zeroSinceStr := "0"
if !zeroSince.IsZero() {
zeroSinceStr = fmt.Sprintf("%d", zeroSince.Unix())
}
if err := sm.Cache.Set(ctx, zeroSinceKey, []byte(zeroSinceStr), 24*time.Hour); err != nil {
return newStatus, fmt.Errorf("cache set zero since: %w", err)
}