package service import ( "context" "git.chuangxin1.com/csacred/toolkit/example/cuser/define" "git.chuangxin1.com/csacred/toolkit/http" "github.com/go-kit/kit/log" consulsd "github.com/go-kit/kit/sd/consul" ) // API gateway factory func factory(ctx context.Context, instancer *consulsd.Instancer, logger log.Logger) Endpoints { e := Endpoints{} var lb http.LoadBalancer lb.Logger = logger lb.Ctx = ctx lb.Inst = instancer lb.Dec = http.DecodeJSONResponse lb.Route = define.RouteUserGetProfile e.UserProfileEndpoint = http.GetLoadBalancer(lb) lb.Route = define.RouteUserGetList e.UserListEndpoint = http.GetLoadBalancer(lb) lb.Route = define.RouteUserPostSignIn e.UserSignInEndpoint = http.PostLoadBalancer(lb) lb.Route = define.RouteUserPostChangePwd e.UserChangePwdEndpoint = http.PostLoadBalancer(lb) return e } func endpointHanders(endpoints Endpoints) []http.EndpointRouter { var handers []http.EndpointRouter handers = append(handers, http.GetEndpointRouter(define.RouteUserGetProfile, DecodeGetUserProfileRequest, http.WriteCtxJSON, endpoints.UserProfileEndpoint)) handers = append(handers, http.GetEndpointRouter(define.RouteUserGetList, DecodeGetUserListRequest, http.WriteCtxJSON, endpoints.UserListEndpoint)) handers = append(handers, http.PostEndpointRouter(define.RouteUserPostSignIn, DecodePostUserSignInRequest, http.WriteCtxJSON, endpoints.UserSignInEndpoint)) handers = append(handers, http.PostEndpointRouter(define.RouteUserPostChangePwd, DecodePostUserChangePwdRequest, http.WriteCtxJSON, endpoints.UserChangePwdEndpoint)) return handers } // GenServiceHandler service handler func GenServiceHandler(s UserServicer, logger log.Logger) []http.RouterHandler { var handers []http.RouterHandler e := GenServerEndpoints(s) ehanders := endpointHanders(e) for i := range ehanders { handers = append(handers, http.EndpointRouterHandler(ehanders[i], logger)) } return handers } // GenGatewayHandlers gateway handlers func GenGatewayHandlers(ctx context.Context, instancer *consulsd.Instancer, logger log.Logger) []http.RouterHandler { var handers []http.RouterHandler e := factory(ctx, instancer, logger) ehanders := endpointHanders(e) for i := range ehanders { handers = append(handers, http.EndpointRouterHandler(ehanders[i], logger)) } return handers }