add gorilla mux registration
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
60d65b507d
commit
5d85ade30c
File diff suppressed because one or more lines are too long
4
main.go
4
main.go
@ -159,6 +159,10 @@ func main() {
|
||||
g.Error(err, "failed to get assets files")
|
||||
}
|
||||
|
||||
if debug {
|
||||
log.Printf("components to generate: %v", components)
|
||||
}
|
||||
|
||||
for _, f := range fi {
|
||||
skip := true
|
||||
for _, component := range components {
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
micro_client "github.com/unistack-org/micro/v3/client"
|
||||
micro_server "github.com/unistack-org/micro/v3/server"
|
||||
micro_client_http "github.com/unistack-org/micro-client-http"
|
||||
@ -232,3 +233,31 @@ func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) Recv() (*{{$reqMethod}},
|
||||
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
|
||||
func Register(r *mux.Router, h interface{}, eps []*api.Endpoint) error {
|
||||
v := reflect.ValueOf(h)
|
||||
|
||||
methods := v.NumMethod()
|
||||
if methods < 1 {
|
||||
return ErrInvalidHandler
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user