web

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2019 License: Apache-2.0 Imports: 20 Imported by: 146

README

Go Web GoDoc Travis CI Go Report Card

Go Web is a framework for micro service web development.

Overview

Go Web provides a tiny HTTP web server library which leverages go-micro to create micro web services as first class citizens in a microservice world. It wraps go-micro to give you service discovery, heartbeating and the ability to create web apps as microservices.

Features

  • Service Discovery - Services are automatically registered in service discovery on startup. Go Web includes a http.Client with pre-initialised roundtripper which makes use of service discovery so you can use service names.

  • Heartbeating - Go Web apps will periodically heartbeat with service discovery to provide liveness updates. In the event a service fails it will be removed from the registry after a pre-defined expiry time.

  • Custom Handlers - Specify your own http router for handling requests. This allows you to maintain full control over how you want to route to internal handlers.

Getting Started

Dependencies

Go Web makes use of Go Micro which means it needs service discovery

See the go-micro for install instructions

For a quick start use consul

# install
brew install consul

# run
consul agent -dev

Usage

service := web.NewService(
	web.Name("example.com"),
)

service.HandleFunc("/foo", fooHandler)

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

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

Set Handler

You might have a preference for a HTTP handler, so use something else. This loses the ability to register endpoints in discovery but we'll fix that soon.

import "github.com/gorilla/mux"

r := mux.NewRouter()
r.HandleFunc("/", indexHandler)
r.HandleFunc("/objects/{object}", objectHandler)

service := web.NewService(
	web.Handler(r)
)

Call Service

Go-web includes a http.Client with a custom http.RoundTripper that uses service discovery

c := service.Client()

rsp, err := c.Get("http://example.com/foo")

This will lookup service discovery for the service example.com and route to one of the available nodes.

Documentation

Overview

Package web provides web based micro services

Index

Constants

This section is empty.

Variables

View Source
var (
	// For serving
	DefaultName    = "go-web"
	DefaultVersion = "latest"
	DefaultId      = uuid.New().String()
	DefaultAddress = ":0"

	// for registration
	DefaultRegisterTTL      = time.Minute
	DefaultRegisterInterval = time.Second * 30
)

Functions

This section is empty.

Types

type Option

type Option func(o *Options)

func Action

func Action(a func(*cli.Context)) Option

Action sets the command action.

func Address

func Address(a string) Option

Address to bind to - host:port

func Advertise(a string) Option

The address to advertise for discovery - host:port

func AfterStart

func AfterStart(fn func() error) Option

AfterStart is executed after server start.

func AfterStop

func AfterStop(fn func() error) Option

AfterStop is executed after server stop.

func BeforeStart

func BeforeStart(fn func() error) Option

BeforeStart is executed before the server starts.

func BeforeStop

func BeforeStop(fn func() error) Option

BeforeStop is executed before the server stops.

func Context added in v0.2.0

func Context(ctx context.Context) Option

Context specifies a context for the service. Can be used to signal shutdown of the service. Can be used for extra option values.

func Flags

func Flags(flags ...cli.Flag) Option

Flags sets the command flags.

func Handler

func Handler(h http.Handler) Option

func Id

func Id(id string) Option

Unique server id

func Metadata

func Metadata(md map[string]string) Option

Metadata associated with the service

func MicroService added in v0.5.0

func MicroService(s micro.Service) Option

MicroService sets the micro.Service used internally

func Name

func Name(n string) Option

Server name

func RegisterInterval

func RegisterInterval(t time.Duration) Option

func RegisterTTL

func RegisterTTL(t time.Duration) Option

func Registry

func Registry(r registry.Registry) Option

func Secure added in v0.5.0

func Secure(b bool) Option

Secure Use secure communication. If TLSConfig is not specified we use InsecureSkipVerify and generate a self signed cert

func Server added in v0.3.0

func Server(srv *http.Server) Option

func TLSConfig added in v0.5.0

func TLSConfig(t *tls.Config) Option

TLSConfig to be used for the transport.

func Version

func Version(v string) Option

Version of the service

type Options

type Options struct {
	Name      string
	Version   string
	Id        string
	Metadata  map[string]string
	Address   string
	Advertise string

	Action func(*cli.Context)
	Flags  []cli.Flag

	RegisterTTL      time.Duration
	RegisterInterval time.Duration

	Server  *http.Server
	Handler http.Handler

	// Alternative Options
	Context context.Context

	Registry registry.Registry
	Service  micro.Service

	Secure      bool
	TLSConfig   *tls.Config
	BeforeStart []func() error
	BeforeStop  []func() error
	AfterStart  []func() error
	AfterStop   []func() error
}

type Service

type Service interface {
	Client() *http.Client
	Init(opts ...Option) error
	Options() Options
	Handle(pattern string, handler http.Handler)
	HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
	Run() error
}

Service is a web service with service discovery built in

func NewService

func NewService(opts ...Option) Service

NewService returns a new web.Service

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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