2021-01-19 12:43:37 +03:00
|
|
|
// Code generated by protoc-gen-micro
|
2021-02-02 17:52:14 +03:00
|
|
|
// source: {{.File.Name}}
|
2021-02-02 07:59:50 +03:00
|
|
|
package {{goPkgLastElement .File | splitArray ";" | last | replace "." "_"}}
|
2021-01-19 12:43:37 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
micro_api "github.com/unistack-org/micro/v3/api"
|
|
|
|
)
|
|
|
|
|
2021-02-02 11:13:29 +03:00
|
|
|
func {{.Service.Name | trimSuffix "Service"}}ServiceRegister(r *mux.Router, h interface{}, eps []*micro_api.Endpoint) error {
|
2021-01-19 12:43:37 +03:00
|
|
|
v := reflect.ValueOf(h)
|
|
|
|
|
|
|
|
if v.NumMethod() < 1 {
|
2021-01-30 19:34:30 +03:00
|
|
|
return fmt.Errorf("handler has no methods: %T", h)
|
2021-01-19 12:43:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
r.HandleFunc(ep.Path[0], rh).Methods(ep.Method...).Name(ep.Name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|