api

package
v0.0.0-...-2193872 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 5, 2016 License: Apache-2.0 Imports: 17 Imported by: 0

README

Micro API

This is a lightweight proxy for Micro based microservices. It conforms to the API Gateway pattern and can be used in conjuction with go-micro based apps or any future language implementation of the Micro toolchain.

Currently a work in progress.

Run API
$ go get github.com/micro/micro
$ micro api
I0523 12:23:23.413940   81384 api.go:131] API Rpc handler /rpc
I0523 12:23:23.414238   81384 api.go:143] Listening on [::]:8080
I0523 12:23:23.414272   81384 server.go:113] Starting server go.micro.api id go.micro.api-1f951765-013e-11e5-9273-68a86d0d36b6
I0523 12:23:23.414355   81384 rpc_server.go:112] Listening on [::]:51938
I0523 12:23:23.414399   81384 server.go:95] Registering node: go.micro.api-1f951765-013e-11e5-9273-68a86d0d36b6
Testing API

Let's start the example go-micro based server.

$ go get github.com/micro/go-micro/examples/server
$ $GOPATH/bin/server 
I0525 18:17:57.574457   84421 server.go:117] Starting server go.micro.srv.example id go.micro.srv.example-fccbb6fb-0301-11e5-9f1f-68a86d0d36b6
I0525 18:17:57.574748   84421 rpc_server.go:126] Listening on [::]:62421
I0525 18:17:57.574779   84421 server.go:99] Registering node: go.micro.srv.example-fccbb6fb-0301-11e5-9f1f-68a86d0d36b6

The example server has a handler registered called Example with a method named Call. Now let's query this through the API.

$ curl -d 'service=go.micro.srv.example' -d 'method=Example.Call' -d 'request={"name": "Asim Aslam"}' http://localhost:8080/rpc
{"msg":"go.micro.srv.example-fccbb6fb-0301-11e5-9f1f-68a86d0d36b6: Hello Asim Aslam"}

Alternatively let's try 'Content-Type: application/json'

$ curl -H 'Content-Type: application/json' -d '{"service": "go.micro.srv.example", "method": "Example.Call", "request": {"name": "Asim Aslam"}}' http://localhost:8080/rpc
{"msg":"go.micro.srv.example-fccbb6fb-0301-11e5-9f1f-68a86d0d36b6: Hello Asim Aslam"}
Testing using REST based API Services

Micro allows you to handle REST based paths using rpc by providing built in handling for API Services. An API service is like any other micro service except each method signature takes an *api.Request and *api.Response which can be found in github.com/micro/micro/api/proto.

The default namespace for these services are: go.micro.api

Translation of URLs are as follows:

/foo/bar => service: go.micro.api.foo method: Foo.Bar

/foo/bar/baz => service: go.micro.api.foo method: Bar.Baz

/foo/bar/baz/cat => service: go.micro.api.foo.bar method: Baz.Cat

A working example can be found here Greeter Service

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Address      = ":8080"
	RpcPath      = "/rpc"
	HttpPath     = "/"
	Namespace    = "go.micro.api"
	HeaderPrefix = "X-Micro-"
)

Functions

func Commands

func Commands() []cli.Command

Types

type API

type API interface {
	Address() string
	Init() error
	Start() error
	Stop() error
}

API interface represents our API server. It should provide the facility to server HTTP requests, forward on to the appropriate services and return a response.

func New

func New(address string) API

type ApiServer

type ApiServer struct {
	// contains filtered or unexported fields
}

func (*ApiServer) Address

func (s *ApiServer) Address() string

func (*ApiServer) Init

func (s *ApiServer) Init() error

func (*ApiServer) ServeHTTP

func (s *ApiServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*ApiServer) Start

func (s *ApiServer) Start() error

func (*ApiServer) Stop

func (s *ApiServer) Stop() error

Directories

Path Synopsis
Package api is a generated protocol buffer package.
Package api is a generated protocol buffer package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL