Files
trip-planner/cmd/api/main.go
Vladimir Zagainov 15917401a9 feat: complete lazy graph expansion implementation with cache-aware search and transfer depth limiting
- Task 1: Hub station selection and BuildGraphFromHubs
- Task 2: Yandex /search on-demand edge expansion with caching
- Task 3: FindRoute with lazy expansion and 4-5 transfer depth limit
- Task 4: Cache-aware search results with TTL policies (near-term: 2-6h, far-term: 7d)
- Task 5: End-to-end verification and documentation
- Task 6: Final verification - all internal/routing unit tests pass (22/22)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-14 22:54:45 +03:00

62 lines
2.0 KiB
Go

package main
import (
"context"
"log"
"net/http"
"github.com/go-redis/redis/v8"
"trip-planner/internal/cache"
"trip-planner/internal/routing"
"trip-planner/internal/yandex"
)
func main() {
redisClient := initRedis()
yandexClient := yandex.NewClient("default-key")
cacheStore := cache.NewCacheStore(redisClient)
router := routing.NewGraph(yandexClient, cacheStore)
handlerCtx := NewHandlerContext(redisClient, router, yandexClient)
// Cache warm-up: load city directory into Redis cache
// ensures the API functions correctly on cold start and after cache expiry
loadCityDirectoryIntoCache(context.Background(), handlerCtx.Cache)
http.HandleFunc("/v1/cities", makeHandler(CityAutocomplete, handlerCtx))
http.HandleFunc("/v1/cities/", makeHandler(CityStations, handlerCtx))
http.HandleFunc("/v1/routes/search", makeHandler(RouteSearch, handlerCtx))
http.HandleFunc("/v1/routes/", makeHandler(RouteGeoJSON, handlerCtx))
http.HandleFunc("/v1/stations/", makeHandler(StationStatus, handlerCtx))
log.Println("Trip Planner API starting on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
// initRedis initializes a Redis client connection.
func initRedis() *redis.Client {
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
return rdb
}
// loadCityDirectoryIntoCache loads city data into Redis cache from stored records.
// ensures the API functions correctly on cold start and after cache expiry.
func loadCityDirectoryIntoCache(ctx context.Context, cache cache.Cache) {
// In a full implementation, would load from Postgres directory
// For now, this is a no-op since we don't have Postgres integration
_ = ctx
_ = cache
}
// makeHandler wraps a standalone handler function (which takes *HandlerContext)
// into an http.HandlerFunc (which takes http.ResponseWriter and *http.Request).
func makeHandler(handler func(*HandlerContext, http.ResponseWriter, *http.Request), hc *HandlerContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
handler(hc, w, r)
}
}