api: remove and regen
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
ce9f896287
commit
56efccc4cf
182
api/api.go
182
api/api.go
@ -1,182 +0,0 @@
|
||||
package api // import "go.unistack.org/micro/v3/api"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"go.unistack.org/micro/v3/metadata"
|
||||
"go.unistack.org/micro/v3/register"
|
||||
"go.unistack.org/micro/v3/server"
|
||||
)
|
||||
|
||||
// nolint: revive
|
||||
// Api interface
|
||||
type Api interface {
|
||||
// Initialise options
|
||||
Init(...Option) error
|
||||
// Get the options
|
||||
Options() Options
|
||||
// Register a http handler
|
||||
Register(*Endpoint) error
|
||||
// Register a route
|
||||
Deregister(*Endpoint) error
|
||||
// Implementation of api
|
||||
String() string
|
||||
}
|
||||
|
||||
// Options holds the options
|
||||
type Options struct{}
|
||||
|
||||
// Option func signature
|
||||
type Option func(*Options) error
|
||||
|
||||
// Endpoint is a mapping between an RPC method and HTTP endpoint
|
||||
type Endpoint struct {
|
||||
// Name Greeter.Hello
|
||||
Name string
|
||||
// Desciption for endpoint
|
||||
Description string
|
||||
// Handler e.g rpc, proxy
|
||||
Handler string
|
||||
// Body destination
|
||||
// "*" or "" - top level message value
|
||||
// "string" - inner message value
|
||||
Body string
|
||||
// Host e.g example.com
|
||||
Host []string
|
||||
// Method e.g GET, POST
|
||||
Method []string
|
||||
// Path e.g /greeter. Expect POSIX regex
|
||||
Path []string
|
||||
// Stream flag
|
||||
Stream bool
|
||||
}
|
||||
|
||||
// Service represents an API service
|
||||
type Service struct {
|
||||
// Name of service
|
||||
Name string
|
||||
// Endpoint for this service
|
||||
Endpoint *Endpoint
|
||||
// Services that provides service
|
||||
Services []*register.Service
|
||||
}
|
||||
|
||||
// Encode encodes an endpoint to endpoint metadata
|
||||
func Encode(e *Endpoint) map[string]string {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// endpoint map
|
||||
ep := make(map[string]string)
|
||||
|
||||
// set vals only if they exist
|
||||
set := func(k, v string) {
|
||||
if len(v) == 0 {
|
||||
return
|
||||
}
|
||||
ep[k] = v
|
||||
}
|
||||
|
||||
set("endpoint", e.Name)
|
||||
set("description", e.Description)
|
||||
set("handler", e.Handler)
|
||||
set("method", strings.Join(e.Method, ","))
|
||||
set("path", strings.Join(e.Path, ","))
|
||||
set("host", strings.Join(e.Host, ","))
|
||||
set("body", e.Body)
|
||||
|
||||
return ep
|
||||
}
|
||||
|
||||
// Decode decodes endpoint metadata into an endpoint
|
||||
func Decode(e metadata.Metadata) *Endpoint {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
ep := &Endpoint{}
|
||||
ep.Name, _ = e.Get("endpoint")
|
||||
ep.Description, _ = e.Get("description")
|
||||
epmethod, _ := e.Get("method")
|
||||
ep.Method = []string{epmethod}
|
||||
eppath, _ := e.Get("path")
|
||||
ep.Path = []string{eppath}
|
||||
ephost, _ := e.Get("host")
|
||||
ep.Host = []string{ephost}
|
||||
ep.Handler, _ = e.Get("handler")
|
||||
ep.Body, _ = e.Get("body")
|
||||
|
||||
return ep
|
||||
}
|
||||
|
||||
// Validate validates an endpoint to guarantee it won't blow up when being served
|
||||
func Validate(e *Endpoint) error {
|
||||
if e == nil {
|
||||
return errors.New("endpoint is nil")
|
||||
}
|
||||
|
||||
if len(e.Name) == 0 {
|
||||
return errors.New("name required")
|
||||
}
|
||||
|
||||
for _, p := range e.Path {
|
||||
ps := p[0]
|
||||
pe := p[len(p)-1]
|
||||
|
||||
switch {
|
||||
case ps == '^' && pe == '$':
|
||||
if _, err := regexp.CompilePOSIX(p); err != nil {
|
||||
return err
|
||||
}
|
||||
case ps == '^' && pe != '$':
|
||||
return errors.New("invalid path")
|
||||
case ps != '^' && pe == '$':
|
||||
return errors.New("invalid path")
|
||||
}
|
||||
}
|
||||
|
||||
if len(e.Handler) == 0 {
|
||||
return errors.New("invalid handler")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
Design ideas
|
||||
|
||||
// Gateway is an api gateway interface
|
||||
type Gateway interface {
|
||||
// Register a http handler
|
||||
Handle(pattern string, http.Handler)
|
||||
// Register a route
|
||||
RegisterRoute(r Route)
|
||||
// Init initialises the command line.
|
||||
// It also parses further options.
|
||||
Init(...Option) error
|
||||
// Run the gateway
|
||||
Run() error
|
||||
}
|
||||
|
||||
// NewGateway returns a new api gateway
|
||||
func NewGateway() Gateway {
|
||||
return newGateway()
|
||||
}
|
||||
*/
|
||||
|
||||
// WithEndpoint returns a server.HandlerOption with endpoint metadata set
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// proto.RegisterHandler(service.Server(), new(Handler), api.WithEndpoint(
|
||||
// &api.Endpoint{
|
||||
// Name: "Greeter.Hello",
|
||||
// Path: []string{"/greeter"},
|
||||
// },
|
||||
// ))
|
||||
func WithEndpoint(e *Endpoint) server.HandlerOption {
|
||||
return server.EndpointMetadata(e.Name, Encode(e))
|
||||
}
|
245
api/api_test.go
245
api/api_test.go
@ -1,245 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.unistack.org/micro/v3/metadata"
|
||||
"go.unistack.org/micro/v3/server"
|
||||
)
|
||||
|
||||
func TestDecode(t *testing.T) {
|
||||
md := metadata.New(0)
|
||||
md.Set("host", "localhost", "method", "GET", "path", "/")
|
||||
ep := Decode(md)
|
||||
if md == nil {
|
||||
t.Fatalf("failed to decode md %#+v", md)
|
||||
} else if len(ep.Host) != 1 || len(ep.Method) != 1 || len(ep.Path) != 1 {
|
||||
t.Fatalf("ep invalid after decode %#+v", ep)
|
||||
}
|
||||
if ep.Host[0] != "localhost" || ep.Method[0] != "GET" || ep.Path[0] != "/" {
|
||||
t.Fatalf("ep invalid after decode %#+v", ep)
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:gocyclo
|
||||
func TestEncode(t *testing.T) {
|
||||
testData := []*Endpoint{
|
||||
nil,
|
||||
{
|
||||
Name: "Foo.Bar",
|
||||
Description: "A test endpoint",
|
||||
Handler: "meta",
|
||||
Host: []string{"foo.com"},
|
||||
Method: []string{"GET"},
|
||||
Path: []string{"/test"},
|
||||
},
|
||||
}
|
||||
|
||||
compare := func(expect, got []string) bool {
|
||||
// no data to compare, return true
|
||||
if len(expect) == 0 && len(got) == 0 {
|
||||
return true
|
||||
}
|
||||
// no data expected but got some return false
|
||||
if len(expect) == 0 && len(got) > 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
// compare expected with what we got
|
||||
for _, e := range expect {
|
||||
var seen bool
|
||||
for _, g := range got {
|
||||
if e == g {
|
||||
seen = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !seen {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// we're done, return true
|
||||
return true
|
||||
}
|
||||
|
||||
for _, d := range testData {
|
||||
// encode
|
||||
e := Encode(d)
|
||||
// decode
|
||||
de := Decode(e)
|
||||
|
||||
// nil endpoint returns nil
|
||||
if d == nil {
|
||||
if e != nil {
|
||||
t.Fatalf("expected nil got %v", e)
|
||||
}
|
||||
if de != nil {
|
||||
t.Fatalf("expected nil got %v", de)
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
// check encoded map
|
||||
name := e["endpoint"]
|
||||
desc := e["description"]
|
||||
method := strings.Split(e["method"], ",")
|
||||
path := strings.Split(e["path"], ",")
|
||||
host := strings.Split(e["host"], ",")
|
||||
handler := e["handler"]
|
||||
|
||||
if name != d.Name {
|
||||
t.Fatalf("expected %v got %v", d.Name, name)
|
||||
}
|
||||
if desc != d.Description {
|
||||
t.Fatalf("expected %v got %v", d.Description, desc)
|
||||
}
|
||||
if handler != d.Handler {
|
||||
t.Fatalf("expected %v got %v", d.Handler, handler)
|
||||
}
|
||||
if ok := compare(d.Method, method); !ok {
|
||||
t.Fatalf("expected %v got %v", d.Method, method)
|
||||
}
|
||||
if ok := compare(d.Path, path); !ok {
|
||||
t.Fatalf("expected %v got %v", d.Path, path)
|
||||
}
|
||||
if ok := compare(d.Host, host); !ok {
|
||||
t.Fatalf("expected %v got %v", d.Host, host)
|
||||
}
|
||||
|
||||
if de.Name != d.Name {
|
||||
t.Fatalf("expected %v got %v", d.Name, de.Name)
|
||||
}
|
||||
if de.Description != d.Description {
|
||||
t.Fatalf("expected %v got %v", d.Description, de.Description)
|
||||
}
|
||||
if de.Handler != d.Handler {
|
||||
t.Fatalf("expected %v got %v", d.Handler, de.Handler)
|
||||
}
|
||||
if ok := compare(d.Method, de.Method); !ok {
|
||||
t.Fatalf("expected %v got %v", d.Method, de.Method)
|
||||
}
|
||||
if ok := compare(d.Path, de.Path); !ok {
|
||||
t.Fatalf("expected %v got %v", d.Path, de.Path)
|
||||
}
|
||||
if ok := compare(d.Host, de.Host); !ok {
|
||||
t.Fatalf("expected %v got %v", d.Host, de.Host)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate(t *testing.T) {
|
||||
epPcre := &Endpoint{
|
||||
Name: "Foo.Bar",
|
||||
Description: "A test endpoint",
|
||||
Handler: "meta",
|
||||
Host: []string{"foo.com"},
|
||||
Method: []string{"GET"},
|
||||
Path: []string{"^/test/?$"},
|
||||
}
|
||||
if err := Validate(epPcre); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
epGpath := &Endpoint{
|
||||
Name: "Foo.Bar",
|
||||
Description: "A test endpoint",
|
||||
Handler: "meta",
|
||||
Host: []string{"foo.com"},
|
||||
Method: []string{"GET"},
|
||||
Path: []string{"/test/{id}"},
|
||||
}
|
||||
if err := Validate(epGpath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
epPcreInvalid := &Endpoint{
|
||||
Name: "Foo.Bar",
|
||||
Description: "A test endpoint",
|
||||
Handler: "meta",
|
||||
Host: []string{"foo.com"},
|
||||
Method: []string{"GET"},
|
||||
Path: []string{"/test/?$"},
|
||||
}
|
||||
if err := Validate(epPcreInvalid); err == nil {
|
||||
t.Fatalf("invalid pcre %v", epPcreInvalid.Path[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithEndpoint(t *testing.T) {
|
||||
ep := &Endpoint{
|
||||
Name: "Foo.Bar",
|
||||
Description: "A test endpoint",
|
||||
Handler: "meta",
|
||||
Host: []string{"foo.com"},
|
||||
Method: []string{"GET"},
|
||||
Path: []string{"/test/{id}"},
|
||||
}
|
||||
o := WithEndpoint(ep)
|
||||
opts := server.NewHandlerOptions(o)
|
||||
if opts.Metadata == nil {
|
||||
t.Fatalf("WithEndpoint not works %#+v", opts)
|
||||
}
|
||||
md, ok := opts.Metadata[ep.Name]
|
||||
if !ok {
|
||||
t.Fatalf("WithEndpoint not works %#+v", opts)
|
||||
}
|
||||
if v, ok := md.Get("Endpoint"); !ok || v != "Foo.Bar" {
|
||||
t.Fatalf("WithEndpoint not works %#+v", md)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateNilErr(t *testing.T) {
|
||||
var ep *Endpoint
|
||||
if err := Validate(ep); err == nil {
|
||||
t.Fatalf("Validate not works")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateMissingNameErr(t *testing.T) {
|
||||
ep := &Endpoint{}
|
||||
if err := Validate(ep); err == nil {
|
||||
t.Fatalf("Validate not works")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateMissingHandlerErr(t *testing.T) {
|
||||
ep := &Endpoint{Name: "test"}
|
||||
if err := Validate(ep); err == nil {
|
||||
t.Fatalf("Validate not works")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRegexpStartErr(t *testing.T) {
|
||||
ep := &Endpoint{Name: "test", Handler: "test"}
|
||||
ep.Path = []string{"^/"}
|
||||
if err := Validate(ep); err == nil {
|
||||
t.Fatalf("Validate not works")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRegexpEndErr(t *testing.T) {
|
||||
ep := &Endpoint{Name: "test", Handler: "test", Path: []string{""}}
|
||||
ep.Path[0] = "/$"
|
||||
if err := Validate(ep); err == nil {
|
||||
t.Fatalf("Validate not works")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRegexpNonErr(t *testing.T) {
|
||||
ep := &Endpoint{Name: "test", Handler: "test", Path: []string{""}}
|
||||
ep.Path[0] = "^/(.*)$"
|
||||
if err := Validate(ep); err != nil {
|
||||
t.Fatalf("Validate not works")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRegexpErr(t *testing.T) {
|
||||
ep := &Endpoint{Name: "test", Handler: "test", Path: []string{""}}
|
||||
ep.Path[0] = "^/(.$"
|
||||
if err := Validate(ep); err == nil {
|
||||
t.Fatalf("Validate not works")
|
||||
}
|
||||
}
|
@ -1,32 +1,29 @@
|
||||
// Code generated by protoc-gen-go-micro. DO NOT EDIT.
|
||||
// protoc-gen-go-micro version: v3.5.3
|
||||
// versions:
|
||||
// - protoc-gen-go-micro v3.5.3
|
||||
// - protoc v3.21.12
|
||||
// source: handler.proto
|
||||
|
||||
package handler
|
||||
|
||||
import (
|
||||
context "context"
|
||||
api "go.unistack.org/micro/v3/api"
|
||||
codec "go.unistack.org/micro/v3/codec"
|
||||
http "net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
MeterName = "Meter"
|
||||
|
||||
MeterEndpoints = []api.Endpoint{
|
||||
{
|
||||
Name: "Meter.Metrics",
|
||||
Path: []string{"/metrics"},
|
||||
Method: []string{"GET"},
|
||||
Handler: "rpc",
|
||||
)
|
||||
var (
|
||||
MeterServerEndpoints = map[string]map[string]string{
|
||||
"/metrics": map[string]string{
|
||||
"Method": http.MethodGet,
|
||||
"Stream": "false",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func NewMeterEndpoints() []api.Endpoint {
|
||||
return MeterEndpoints
|
||||
}
|
||||
|
||||
type MeterServer interface {
|
||||
Metrics(ctx context.Context, req *codec.Frame, rsp *codec.Frame) error
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ package handler
|
||||
|
||||
import (
|
||||
context "context"
|
||||
api "go.unistack.org/micro/v3/api"
|
||||
v3 "go.unistack.org/micro-server-http/v3"
|
||||
codec "go.unistack.org/micro/v3/codec"
|
||||
server "go.unistack.org/micro/v3/server"
|
||||
)
|
||||
@ -21,15 +21,12 @@ func (h *meterServer) Metrics(ctx context.Context, req *codec.Frame, rsp *codec.
|
||||
|
||||
func RegisterMeterServer(s server.Server, sh MeterServer, opts ...server.HandlerOption) error {
|
||||
type meter interface {
|
||||
Metrics(ctx context.Context, req *codec.Frame, rsp *codec.Frame) error
|
||||
}
|
||||
type Meter struct {
|
||||
meter
|
||||
}
|
||||
h := &meterServer{sh}
|
||||
var nopts []server.HandlerOption
|
||||
for _, endpoint := range MeterEndpoints {
|
||||
nopts = append(nopts, api.WithEndpoint(&endpoint))
|
||||
}
|
||||
nopts = append(nopts, v3.HandlerMetadata(MeterServerEndpoints))
|
||||
return s.Handle(s.NewHandler(&Meter{h}, append(nopts, opts...)...))
|
||||
}
|
||||
|
@ -1,44 +1,37 @@
|
||||
// Code generated by protoc-gen-go-micro. DO NOT EDIT.
|
||||
// protoc-gen-go-micro version: v3.5.3
|
||||
// versions:
|
||||
// - protoc-gen-go-micro v3.5.3
|
||||
// - protoc v3.21.12
|
||||
// source: health.proto
|
||||
|
||||
package health
|
||||
|
||||
import (
|
||||
context "context"
|
||||
api "go.unistack.org/micro/v3/api"
|
||||
codec "go.unistack.org/micro/v3/codec"
|
||||
http "net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
HealthName = "Health"
|
||||
|
||||
HealthEndpoints = []api.Endpoint{
|
||||
{
|
||||
Name: "Health.Live",
|
||||
Path: []string{"/live"},
|
||||
Method: []string{"GET"},
|
||||
Handler: "rpc",
|
||||
)
|
||||
var (
|
||||
HealthServerEndpoints = map[string]map[string]string{
|
||||
"/live": map[string]string{
|
||||
"Method": http.MethodGet,
|
||||
"Stream": "false",
|
||||
},
|
||||
{
|
||||
Name: "Health.Ready",
|
||||
Path: []string{"/ready"},
|
||||
Method: []string{"GET"},
|
||||
Handler: "rpc",
|
||||
"/ready": map[string]string{
|
||||
"Method": http.MethodGet,
|
||||
"Stream": "false",
|
||||
},
|
||||
{
|
||||
Name: "Health.Version",
|
||||
Path: []string{"/version"},
|
||||
Method: []string{"GET"},
|
||||
Handler: "rpc",
|
||||
"/version": map[string]string{
|
||||
"Method": http.MethodGet,
|
||||
"Stream": "false",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func NewHealthEndpoints() []api.Endpoint {
|
||||
return HealthEndpoints
|
||||
}
|
||||
|
||||
type HealthServer interface {
|
||||
Live(ctx context.Context, req *codec.Frame, rsp *codec.Frame) error
|
||||
Ready(ctx context.Context, req *codec.Frame, rsp *codec.Frame) error
|
||||
|
@ -6,7 +6,7 @@ package health
|
||||
|
||||
import (
|
||||
context "context"
|
||||
api "go.unistack.org/micro/v3/api"
|
||||
v3 "go.unistack.org/micro-server-http/v3"
|
||||
codec "go.unistack.org/micro/v3/codec"
|
||||
server "go.unistack.org/micro/v3/server"
|
||||
)
|
||||
@ -29,17 +29,12 @@ func (h *healthServer) Version(ctx context.Context, req *codec.Frame, rsp *codec
|
||||
|
||||
func RegisterHealthServer(s server.Server, sh HealthServer, opts ...server.HandlerOption) error {
|
||||
type health interface {
|
||||
Live(ctx context.Context, req *codec.Frame, rsp *codec.Frame) error
|
||||
Ready(ctx context.Context, req *codec.Frame, rsp *codec.Frame) error
|
||||
Version(ctx context.Context, req *codec.Frame, rsp *codec.Frame) error
|
||||
}
|
||||
type Health struct {
|
||||
health
|
||||
}
|
||||
h := &healthServer{sh}
|
||||
var nopts []server.HandlerOption
|
||||
for _, endpoint := range HealthEndpoints {
|
||||
nopts = append(nopts, api.WithEndpoint(&endpoint))
|
||||
}
|
||||
nopts = append(nopts, v3.HandlerMetadata(HealthServerEndpoints))
|
||||
return s.Handle(s.NewHandler(&Health{h}, append(nopts, opts...)...))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user