Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/routing-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ func apiHandler(cfg config.Config, uaaClient uaaclient.TokenValidator, database
tcpMappingsHandler := handlers.NewTcpRouteMappingsHandler(uaaClient, validator, database, int(cfg.MaxTTL.Seconds()), logger)

actions := rata.Handlers{
routing_api.CheckHealth: route(routesHandler.Health),
routing_api.UpsertRoute: route(routesHandler.Upsert),
routing_api.DeleteRoute: route(routesHandler.Delete),
routing_api.ListRoute: route(routesHandler.List),
Expand Down
10 changes: 10 additions & 0 deletions db/db_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

//go:generate counterfeiter -o fakes/fake_db.go . DB
type DB interface {
CheckHealth() error
ReadRoutes() ([]models.Route, error)
SaveRoute(route models.Route) error
DeleteRoute(route models.Route) error
Expand Down Expand Up @@ -361,6 +362,15 @@ func notImplementedError() error {
return fmt.Errorf("function not implemented: %s", fnName)
}

func (s *SqlDB) CheckHealth() error {
var routes []models.Route
err := s.Client.First(&routes)
if err != nil {
return fmt.Errorf("checking health error: %v", err)
}
return nil
}

func (s *SqlDB) ReadRoutes() ([]models.Route, error) {
var routes []models.Route
now := time.Now()
Expand Down
5 changes: 3 additions & 2 deletions handlers/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ func LogWrap(handler http.Handler, logger lager.Logger) http.HandlerFunc {

return func(w http.ResponseWriter, r *http.Request) {
requestLog := logger.Session("request", lager.Data{
"method": r.Method,
"request": r.URL.String(),
"remoteAddr": r.RemoteAddr,
"method": r.Method,
"request": r.URL.String(),
})

requestLog.Info("serving", lager.Data{"request-headers": filter(r.Header)})
Expand Down
21 changes: 20 additions & 1 deletion handlers/routes_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"encoding/json"
"fmt"
"net/http"

"code.cloudfoundry.org/lager/v3"
Expand All @@ -28,11 +29,29 @@ func NewRoutesHandler(uaaClient uaaclient.TokenValidator, maxTTL int, validator
}
}

func (h *RoutesHandler) Health(w http.ResponseWriter, req *http.Request) {
log := h.logger.Session("health-check")

err := h.db.CheckHealth()
if err != nil {
handleDBCommunicationError(w, err, log)
return
}
w.WriteHeader(http.StatusOK)
}

func (h *RoutesHandler) List(w http.ResponseWriter, req *http.Request) {
log := h.logger.Session("list-routes")

err := h.uaaClient.ValidateToken(req.Header.Get("Authorization"), RoutingRoutesReadScope)
authHeader := req.Header.Get("Authorization")
if authHeader == "" {
err := fmt.Errorf("authorization header can't be empty (remoteAddr: %s)", req.RemoteAddr)
handleUnauthorizedError(w, err, log)
return
}
err := h.uaaClient.ValidateToken(authHeader, RoutingRoutesReadScope)
if err != nil {
err := fmt.Errorf("%v (remoteAddr: %s)", err, req.RemoteAddr)
handleUnauthorizedError(w, err, log)
return
}
Expand Down
5 changes: 4 additions & 1 deletion routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package routing_api
import "github.com/tedsuo/rata"

const (
CheckHealth = "Health"
UpsertRoute = "UpsertRoute"
DeleteRoute = "Delete"
ListRoute = "List"
Expand All @@ -17,7 +18,9 @@ const (
EventStreamTcpRoute = "TcpRouteEventStream"
)

var RoutesMap = map[string]rata.Route{UpsertRoute: {Path: "/routing/v1/routes", Method: "POST", Name: UpsertRoute},
var RoutesMap = map[string]rata.Route{
CheckHealth: {Path: "/routing/v1/health", Method: "GET", Name: CheckHealth},
UpsertRoute: {Path: "/routing/v1/routes", Method: "POST", Name: UpsertRoute},
DeleteRoute: {Path: "/routing/v1/routes", Method: "DELETE", Name: DeleteRoute},
ListRoute: {Path: "/routing/v1/routes", Method: "GET", Name: ListRoute},
EventStreamRoute: {Path: "/routing/v1/events", Method: "GET", Name: EventStreamRoute},
Expand Down