Coder Social home page Coder Social logo

go-api's Introduction

Go API License GoDoc Travis CI Go Report Card

Go API is a pluggable API framework

It builds on go-micro and includes a set of packages for composing HTTP based APIs

Note: This is a WIP

Getting Started

  • Handlers - The http handlers supported
  • Endpoints - Defining custom routes using endpoints

Handlers

Handlers are HTTP handlers which provide a single entry point and act as a gateway to backend services. Handlers manage dynamic routing from a http request to unique microservices.

Current handlers implemented

  • api - Handles any HTTP request. Gives full control over the http request/response via RPC.
  • event - Handles any HTTP request and publishes to a message bus.
  • http - Handles any HTTP request and forwards as a reverse proxy.
  • rpc - Handles json and protobuf POST requests. Forwards as RPC.
  • web - The HTTP handler with web socket support included.

API Handler

The API handler is the default handler. It serves any HTTP requests and forwards on as an RPC request with a specific format.

  • Content-Type: Any
  • Body: Any
  • Forward Format: api.Request/api.Response
  • Path: /[service]/[method]
  • Resolver: Path is used to resolve service and method

Event Handler

The event handler serves HTTP and forwards the request as a message over a message bus using the go-micro broker.

  • Content-Type: Any
  • Body: Any
  • Forward Format: Request is formatted as go-api/proto.Event
  • Path: /[topic]/[event]
  • Resolver: Path is used to resolve topic and event name

HTTP Handler

The http handler is a http reserve proxy with built in service discovery.

  • Content-Type: Any
  • Body: Any
  • Forward Format: HTTP Reverse proxy
  • Path: /[service]
  • Resolver: Path is used to resolve service name

RPC Handler

The RPC handler serves json or protobuf HTTP POST requests and forwards as an RPC request.

  • Content-Type: application/json or application/protobuf
  • Body: JSON or Protobuf
  • Forward Format: json-rpc or proto-rpc based on content
  • Path: /[service]/[method]
  • Resolver: Path is used to resolve service and method

Web Handler

The web handler is a http reserve proxy with built in service discovery and web socket support.

  • Content-Type: Any
  • Body: Any
  • Forward Format: HTTP Reverse proxy including web sockets
  • Path: /[service]
  • Resolver: Path is used to resolve service name

Endpoints

Endpoints allow a service to dynamically configure the micro api handler

When defining your service also include the endpoint mapping

Example

This example serves /greeter with http methods GET and POST to the Greeter.Hello RPC handler.

type Greeter struct 

// Define the handler
func (g *Greeter) Hello(ctx context.Context, req *proto.Request, rsp *proto.Response) error {
	log.Print("Received Greeter.Hello API request")

	// make the request
	response, err := g.Client.Hello(ctx, &hello.Request{Name: req.Name})
	if err != nil {
		return err
	}

	// set api response
	rsp.Msg = response.Msg
	return nil
}

// A greeter service
service := micro.NewService(
	micro.Name("go.micro.api.greeter"),
)
// Parse command line flags
service.Init()

// Register handler and the endpoint mapping
proto.RegisterGreeterHandler(service.Server(), new(Greeter), api.WithEndpoint(&api.Endpoint{
	// The RPC method
	Name: "Greeter.Hello",
	// The HTTP paths. This can be a POSIX regex
	Path: []string{"/greeter"},
	// The HTTP Methods for this endpoint
	Method: []string{"GET", "POST"},
	// The API handler to use
	Handler: api.Rpc,
})

// Run it as usual
service.Run()

go-api's People

Contributors

bkono avatar jiyeyuran avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.