switchboard

package module
v0.0.0-...-733cdae Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2014 License: Apache-2.0 Imports: 10 Imported by: 0

README

Switchboard

Compose an HTTP API out of a collection of HTTP service backends.

Backend services register themselves with frontend exchanges by pushing their configuration into etcd. This configuration describes HTTP methods and URL patterns that the service is capable of responding to. Exchanges respond to API consumers and route their requests to service backends. They watch for configuration changes in etcd and update their routing rules to add and remove routes as services come and go.

Switchboard operators

Install

go get github.com/jkakar/switchboard

API documentation is at godoc.org/github.com/jkakar/switchboard.

Example

The examples/exchange.go and examples/service.go programs demonstrate Switchboard. You need to run etcd on http://127.0.0.1:4001 before running the example programs. If you're using a git checkout you can build and run etcd with ./build && bin/etcd -v. Next, start the exchange and a handful of service processes in their own terminals.

PORT=5000 go run examples/exchange.go
PORT=5001 go run examples/service.go
PORT=5002 go run examples/service.go
PORT=5003 go run examples/service.go

Now you can make requests and watch the exchange pass them to service backends:

curl http://localhost:5000/hello/jane

License

Copyright 2014, Jamshed Kakar <jkakar@kakar.ca>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at:

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Overview

Package switchboard composes an HTTP API out of a collection of HTTP service backends.

Backend services register themselves with frontend exchanges by pushing their configuration into etcd. This configuration describes HTTP methods and URL patterns that the service is capable of responding to. Exchanges respond to API consumers and route their requests to service backends. They watch for configuration changes in etcd and update their routing rules to add and remove routes as services come and go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Exchange

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

Exchange watches for service changes in etcd and update an ExchangeServeMux.

func NewExchange

func NewExchange(namespace string, client *etcd.Client, mux *ExchangeServeMux) *Exchange

NewExchange creates a new exchange configured to watch for changes in a given etcd directory.

func (*Exchange) Init

func (exchange *Exchange) Init() error

Init fetches service information from etcd and initializes the exchange.

func (*Exchange) Register

func (exchange *Exchange) Register(service *ServiceRecord)

Register adds routes exposed by a service to the ExchangeServeMux.

func (*Exchange) Unregister

func (exchange *Exchange) Unregister(service *ServiceRecord)

Unregister removes routes exposed by a service from the ExchangeServeMux.

func (*Exchange) Watch

func (exchange *Exchange) Watch(stop chan bool)

Watch observes changes in etcd and registers and unregisters services, as necessary, with the ExchangeServeMux. This blocking call will terminate when a value is received on the stop channel.

type ExchangeServeMux

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

ExchangeServeMux is an HTTP request multiplexer. It matches the URL of each incoming request against a list of registered patterns to find the service that can respond to it and proxies the request to the appropriate backend. Pattern matching logic is based on pat.go.

func NewExchangeServeMux

func NewExchangeServeMux() *ExchangeServeMux

NewExchangeServeMux allocates and returns a new ExchangeServeMux.

func (*ExchangeServeMux) Add

func (mux *ExchangeServeMux) Add(method, pattern, address string)

Add registers the address of a backend service as a handler for an HTTP method and URL pattern.

func (*ExchangeServeMux) Match

func (mux *ExchangeServeMux) Match(method, pattern string) (*[]string, error)

Match finds backend service addresses capable of handling a request for the given HTTP method and URL pattern. An error is returned if no addresses are registered for the given HTTP method and URL pattern.

func (*ExchangeServeMux) Remove

func (mux *ExchangeServeMux) Remove(method, pattern, address string)

Remove unregisters the address of a backend service as a handler for an HTTP method and URL pattern.

func (*ExchangeServeMux) ServeHTTP

func (mux *ExchangeServeMux) ServeHTTP(writer http.ResponseWriter, request *http.Request)

ServeHTTP dispatches the request to the backend service whose pattern most closely matches the request URL.

type Routes

type Routes map[string][]string

Routes maps HTTP methods to URLs.

type Service

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

Service responds to HTTP requests for a set of endpoints described by a JSON schema.

func NewService

func NewService(namespace string, client *etcd.Client, address string, routes Routes) *Service

NewService creates a service that can be registered with etcd to handle requests from an exchange.

func (*Service) Address

func (service *Service) Address() string

Address returns the root URL configured for this service.

func (*Service) Broadcast

func (service *Service) Broadcast(interval uint64, ttl uint64, stop chan bool)

Broadcast registers this service with etcd every interval seconds. The ttl is the time to live for the service record, in seconds. This blocking call will terminate when a value is received on the stop channel.

func (*Service) ID

func (service *Service) ID() string

ID returns the UUID that identifies this service.

func (*Service) Register

func (service *Service) Register(ttl uint64) (*ServiceRecord, error)

Register adds a service record to etcd. The ttl is the time to live for the service record, in seconds. A ttl of 0 registers a service record that never expires.

func (*Service) Routes

func (service *Service) Routes() Routes

Routes returns the map of HTTP methods to URL patterns configured for this service.

func (*Service) Unregister

func (service *Service) Unregister() error

Unregister destroys the service record in etcd. An error is returned if the service isn't registered.

type ServiceRecord

type ServiceRecord struct {
	ID      string `json:"id"`
	Address string `json:"address"`
	Routes  Routes `json:"routes"`
}

ServiceRecord is a representation of a service stored in etcd and used by exchanges.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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