From ac64cac9c567879e960c2a6a55c2269db60478ef Mon Sep 17 00:00:00 2001 From: Rick Spurgeon Date: Mon, 19 Aug 2024 09:17:52 -0500 Subject: [PATCH] Adds vip routes to routes service --- flight-data/routes/api/routes.go | 67 ++++++++++++++---------- flight-data/routes/dev/kong/patches.yaml | 25 +++++++++ 2 files changed, 65 insertions(+), 27 deletions(-) diff --git a/flight-data/routes/api/routes.go b/flight-data/routes/api/routes.go index c6f4d82..778503a 100644 --- a/flight-data/routes/api/routes.go +++ b/flight-data/routes/api/routes.go @@ -6,50 +6,63 @@ package api import ( "github.com/Kong/KongAir/flight-data/routes/api/models" "github.com/labstack/echo/v4" - "net/http" + "net/http" ) type RouteService struct { - Routes []models.Route + Routes []models.Route + PrivateRoutes []models.Route } func NewRouteService() *RouteService { rv := RouteService{} rv.Routes = []models.Route{ - {Id: "LHR-JFK", Origin: "LHR", Destination: "JFK", AvgDuration: 470}, - {Id: "LHR-SFO", Origin: "LHR", Destination: "SFO", AvgDuration: 660}, - {Id: "LHR-DXB", Origin: "LHR", Destination: "DXB", AvgDuration: 420}, - {Id: "LHR-HKG", Origin: "LHR", Destination: "HKG", AvgDuration: 745}, - {Id: "LHR-BOM", Origin: "LHR", Destination: "BOM", AvgDuration: 540}, - {Id: "LHR-HND", Origin: "LHR", Destination: "HND", AvgDuration: 830}, - {Id: "LHR-CPT", Origin: "LHR", Destination: "CPT", AvgDuration: 700}, - {Id: "LHR-SYD", Origin: "LHR", Destination: "SYD", AvgDuration: 1320}, - {Id: "LHR-SIN", Origin: "LHR", Destination: "SIN", AvgDuration: 800}, - {Id: "LHR-LAX", Origin: "LHR", Destination: "LAX", AvgDuration: 675}, + {Id: "LHR-JFK", Origin: "LHR", Destination: "JFK", AvgDuration: 470}, + {Id: "LHR-SFO", Origin: "LHR", Destination: "SFO", AvgDuration: 660}, + {Id: "LHR-DXB", Origin: "LHR", Destination: "DXB", AvgDuration: 420}, + {Id: "LHR-HKG", Origin: "LHR", Destination: "HKG", AvgDuration: 745}, + {Id: "LHR-BOM", Origin: "LHR", Destination: "BOM", AvgDuration: 540}, + {Id: "LHR-HND", Origin: "LHR", Destination: "HND", AvgDuration: 830}, + {Id: "LHR-CPT", Origin: "LHR", Destination: "CPT", AvgDuration: 700}, + {Id: "LHR-SYD", Origin: "LHR", Destination: "SYD", AvgDuration: 1320}, + {Id: "LHR-SIN", Origin: "LHR", Destination: "SIN", AvgDuration: 800}, + {Id: "LHR-LAX", Origin: "LHR", Destination: "LAX", AvgDuration: 675}, + } + rv.PrivateRoutes = []models.Route{ + {Id: "VIP-LHR-JFK", Origin: "LHR", Destination: "VIP-JFK", AvgDuration: 430}, + {Id: "VIP-LHR-SFO", Origin: "LHR", Destination: "VIP-SFO", AvgDuration: 620}, + {Id: "VIP-LHR-DXB", Origin: "LHR", Destination: "VIP-DXB", AvgDuration: 390}, + {Id: "VIP-LHR-HKG", Origin: "LHR", Destination: "VIP-HKG", AvgDuration: 645}, } return &rv } func (s *RouteService) GetHealth(ctx echo.Context) error { - return ctx.JSON(http.StatusOK, map[string]string{"status": "OK"}) + return ctx.JSON(http.StatusOK, map[string]string{"status": "OK"}) } func (s *RouteService) GetRoutes(ctx echo.Context, params models.GetRoutesParams) error { - err := ctx.JSON(200, s.Routes) - if err != nil { - return err + if ctx.Request().Header.Get("x-vip") == "true" { + allRoutes := append(s.Routes, s.PrivateRoutes...) + return ctx.JSON(200, allRoutes) } - return nil + + return ctx.JSON(200, s.Routes) } + func (s *RouteService) GetRoute(ctx echo.Context, id string) error { - for _, route := range s.Routes { - if route.Id == id { - err := ctx.JSON(200, route) - if err != nil { - return err - } - return nil - } - } - return ctx.JSON(404, nil) + routes := s.Routes + if ctx.Request().Header.Get("x-vip") == "true" { + routes = append(routes, s.PrivateRoutes...) + } + for _, route := range routes { + if route.Id == id { + err := ctx.JSON(200, route) + if err != nil { + return err + } + return nil + } + } + return ctx.JSON(404, nil) } diff --git a/flight-data/routes/dev/kong/patches.yaml b/flight-data/routes/dev/kong/patches.yaml index c3efae5..3227061 100644 --- a/flight-data/routes/dev/kong/patches.yaml +++ b/flight-data/routes/dev/kong/patches.yaml @@ -1,9 +1,34 @@ _format_version: "1.0" patches: + - selectors: + - $ + values: + consumers: + - username: anonymous + - username: daliya + keyauth_credentials: + - key: kongftw + consumer_groups: + - name: vip + plugins: + - name: request-transformer + config: + add: + headers: + x-vip: true + consumers: + - username: daliya - selectors: - $.services[?(@.name=="routes-service")] values: host: routes.kong-air.dev port: 443 protocol: https + plugins: + - name: key-auth + config: + key_names: + - apikey + hide_credentials: true + anonymous: "anonymous"