Skip to content

Commit

Permalink
web service interface Handle() and HandleFunc() add endpoints option.
Browse files Browse the repository at this point in the history
  • Loading branch information
hb-chen committed Jan 9, 2020
1 parent a90a74c commit 02b746d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
37 changes: 29 additions & 8 deletions web/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/micro/cli"
"github.com/micro/go-micro"
"github.com/micro/go-micro/api"
"github.com/micro/go-micro/registry"
maddr "github.com/micro/go-micro/util/addr"
mhttp "github.com/micro/go-micro/util/http"
Expand Down Expand Up @@ -268,7 +269,7 @@ func (s *service) Client() *http.Client {
}
}

func (s *service) Handle(pattern string, handler http.Handler) {
func (s *service) Handle(pattern string, handler http.Handler, endpoints ...*api.Endpoint) {
var seen bool
for _, ep := range s.srv.Endpoints {
if ep.Name == pattern {
Expand All @@ -279,9 +280,19 @@ func (s *service) Handle(pattern string, handler http.Handler) {

// if its unseen then add an endpoint
if !seen {
s.srv.Endpoints = append(s.srv.Endpoints, &registry.Endpoint{
Name: pattern,
})
if len(endpoints) > 0 {
for _, e := range endpoints {
s.srv.Endpoints = append(s.srv.Endpoints, &registry.Endpoint{
Name: pattern,
Metadata: api.Encode(e),
})
}

} else {
s.srv.Endpoints = append(s.srv.Endpoints, &registry.Endpoint{
Name: pattern,
})
}
}

// disable static serving
Expand All @@ -295,7 +306,7 @@ func (s *service) Handle(pattern string, handler http.Handler) {
s.mux.Handle(pattern, handler)
}

func (s *service) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) {
func (s *service) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request), endpoints ...*api.Endpoint) {
var seen bool
for _, ep := range s.srv.Endpoints {
if ep.Name == pattern {
Expand All @@ -304,9 +315,19 @@ func (s *service) HandleFunc(pattern string, handler func(http.ResponseWriter, *
}
}
if !seen {
s.srv.Endpoints = append(s.srv.Endpoints, &registry.Endpoint{
Name: pattern,
})
if len(endpoints) > 0 {
for _, e := range endpoints {
s.srv.Endpoints = append(s.srv.Endpoints, &registry.Endpoint{
Name: pattern,
Metadata: api.Encode(e),
})
}

} else {
s.srv.Endpoints = append(s.srv.Endpoints, &registry.Endpoint{
Name: pattern,
})
}
}

s.mux.HandleFunc(pattern, handler)
Expand Down
5 changes: 3 additions & 2 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (
"time"

"github.com/google/uuid"
"github.com/micro/go-micro/api"
)

// Service is a web service with service discovery built in
type Service interface {
Client() *http.Client
Init(opts ...Option) error
Options() Options
Handle(pattern string, handler http.Handler)
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
Handle(pattern string, handler http.Handler, endpoints ...*api.Endpoint)
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request), endpoints ...*api.Endpoint)
Run() error
}

Expand Down

0 comments on commit 02b746d

Please sign in to comment.