micro/go-micro

web service handler with []*api.Endpoint option

hb-chen opened this issue · 4 comments

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like
Some times we may use one micro api gateway for rpc and http at the same time.
Can we add a endpoints ...*api.Endpoint option for go-micro/web/Service interface Handle() and HandleFunc() func.
My implement

So that user can use web/Service start a http service for micro api gateway. Otherwise users need to implement it themselves.
Just an idea to see if it's necessary to add to the go-micro/web

e.g.

package main

import (
	"fmt"
	"net/http"

	"github.com/hb-go/micro-plugins/web"
	"github.com/micro/go-micro/api"
	"github.com/micro/go-micro/util/log"
)

func main() {
	// create new web service
	service := web.NewService(
		web.Name("go.micro.api.console.web"),
		web.Version("latest"),
	)

	// initialise service
	if err := service.Init(); err != nil {
		log.Fatal(err)
	}

	service.Handle("/console/", &handler{}, &api.Endpoint{
		Name:    "console",
		Host:    []string{"localhost:8080"},
		Path:    []string{"^/console"},
		Method:  []string{"POST", "GET", "DELETE"},
		Handler: "proxy",
	})

	// run service
	if err := service.Run(); err != nil {
		log.Fatal(err)
	}
}

type handler struct {
}

func (*handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte(fmt.Sprintf("request success, path: %v", r.URL.Path)))
}

Additional context
Add any other context or screenshots about the feature request here.

@asim what you think? imho it may be useful

deserve to be discussed.

this is already covered by handler rpc, does we need also proxy handler support for this?

asim commented

web service != api service

we're working on a model of consolidation, we just don't have an answer yet