micro/router/handler/table.go

116 lines
2.8 KiB
Go
Raw Normal View History

2019-07-31 16:10:04 +01:00
package handler
import (
"context"
"github.com/micro/go-micro/errors"
2019-08-05 17:44:33 +01:00
"github.com/micro/go-micro/router"
pb "github.com/micro/go-micro/router/proto"
2019-07-31 16:10:04 +01:00
)
type Table struct {
Router router.Router
}
func (t *Table) Create(ctx context.Context, route *pb.Route, resp *pb.CreateResponse) error {
err := t.Router.Table().Create(router.Route{
Service: route.Service,
Address: route.Address,
Gateway: route.Gateway,
Network: route.Network,
2019-08-28 08:41:19 +01:00
Router: route.Router,
2019-07-31 16:10:04 +01:00
Link: route.Link,
Metric: route.Metric,
2019-07-31 16:10:04 +01:00
})
if err != nil {
return errors.InternalServerError("go.micro.router", "failed to create route: %s", err)
}
return nil
}
func (t *Table) Update(ctx context.Context, route *pb.Route, resp *pb.UpdateResponse) error {
err := t.Router.Table().Update(router.Route{
Service: route.Service,
Address: route.Address,
Gateway: route.Gateway,
Network: route.Network,
2019-08-28 08:41:19 +01:00
Router: route.Router,
2019-07-31 16:10:04 +01:00
Link: route.Link,
Metric: route.Metric,
2019-07-31 16:10:04 +01:00
})
if err != nil {
return errors.InternalServerError("go.micro.router", "failed to update route: %s", err)
}
return nil
}
func (t *Table) Delete(ctx context.Context, route *pb.Route, resp *pb.DeleteResponse) error {
err := t.Router.Table().Delete(router.Route{
Service: route.Service,
Address: route.Address,
Gateway: route.Gateway,
Network: route.Network,
2019-08-28 08:41:19 +01:00
Router: route.Router,
2019-07-31 16:10:04 +01:00
Link: route.Link,
Metric: route.Metric,
2019-07-31 16:10:04 +01:00
})
if err != nil {
return errors.InternalServerError("go.micro.router", "failed to delete route: %s", err)
}
return nil
}
// List returns all routes in the routing table
func (t *Table) List(ctx context.Context, req *pb.Request, resp *pb.ListResponse) error {
routes, err := t.Router.Table().List()
if err != nil {
return errors.InternalServerError("go.micro.router", "failed to list routes: %s", err)
}
var respRoutes []*pb.Route
for _, route := range routes {
respRoute := &pb.Route{
Service: route.Service,
Address: route.Address,
Gateway: route.Gateway,
Network: route.Network,
2019-08-28 08:41:19 +01:00
Router: route.Router,
2019-07-31 16:10:04 +01:00
Link: route.Link,
Metric: route.Metric,
2019-07-31 16:10:04 +01:00
}
respRoutes = append(respRoutes, respRoute)
}
resp.Routes = respRoutes
return nil
}
func (t *Table) Query(ctx context.Context, req *pb.QueryRequest, resp *pb.QueryResponse) error {
routes, err := t.Router.Table().Query(router.QueryService(req.Query.Service))
2019-07-31 16:10:04 +01:00
if err != nil {
return errors.InternalServerError("go.micro.router", "failed to lookup routes: %s", err)
}
var respRoutes []*pb.Route
for _, route := range routes {
respRoute := &pb.Route{
Service: route.Service,
Address: route.Address,
Gateway: route.Gateway,
Network: route.Network,
2019-08-28 08:41:19 +01:00
Router: route.Router,
2019-07-31 16:10:04 +01:00
Link: route.Link,
Metric: route.Metric,
2019-07-31 16:10:04 +01:00
}
respRoutes = append(respRoutes, respRoute)
}
resp.Routes = respRoutes
return nil
}