// Code generated by protoc-gen-micro // source: {{.File.Name}} package {{goPkgLastElement .File | splitArray ";" | last | replace "." "_"}} import ( "context" "fmt" "net/http" "reflect" "strings" "github.com/go-chi/chi/v4" middleware "github.com/go-chi/chi/v4/middleware" micro_api "github.com/unistack-org/micro/v3/api" ) type routeKey struct{} func RouteName(ctx context.Context) (string, bool) { value, ok := ctx.Value(routeKey{}).(string) return value, ok } func {{.Service.Name | trimSuffix "Service"}}ServiceRegister(r *chi.Mux, h interface{}, eps []*micro_api.Endpoint) error { v := reflect.ValueOf(h) if v.NumMethod() < 1 { return fmt.Errorf("handler has no methods: %T", h) } for _, ep := range eps { idx := strings.Index(ep.Name, ".") if idx < 1 || len(ep.Name) <= idx { return fmt.Errorf("invalid api.Endpoint name: %s", ep.Name) } name := ep.Name[idx+1:] m := v.MethodByName(name) if !m.IsValid() || m.IsZero() { return fmt.Errorf("invalid handler, method %s not found", name) } rh, ok := m.Interface().(func(http.ResponseWriter, *http.Request)) if !ok { return fmt.Errorf("invalid handler: %#+v", m.Interface()) } for _, method := range ep.Method { r.With(middleware.WithValue(routeKey{}, ep.Name)).MethodFunc(method, ep.Path[0], rh) } } return nil }