Compare commits

..

11 Commits

Author SHA1 Message Date
Asim Aslam
1ce0df4e63 Merge pull request #331 from shuLhan/master
all: replace "pborman/uuid" with "google/uuid"
2018-11-21 10:48:36 +00:00
Shulhan
415fb3a730 all: replace "pborman/uuid" with "google/uuid"
Internally, "pborman/uuid.NewUUID()" is calling "google/uuid.New()"
that return nil when there is an error [1].

Both package use the same license.

[1] https://github.com/pborman/uuid/blob/master/version1.go#L17
2018-11-21 17:29:21 +07:00
Asim Aslam
172ffee8c3 add rpc package comments 2018-11-20 10:30:53 +00:00
Asim Aslam
48c068d88d add codec package comments 2018-11-20 10:06:13 +00:00
Asim Aslam
3f7152a4f5 update doc link 2018-11-19 12:30:23 +00:00
Asim Aslam
54f58a15d3 update readme 2018-11-19 12:29:26 +00:00
Asim Aslam
bcb6c12aa1 Merge pull request #329 from micro/http
add http handler option for broker
2018-11-18 20:44:16 +00:00
Asim Aslam
1cb40831a4 add http handler option for broker 2018-11-18 20:40:43 +00:00
Asim Aslam
d9fc2c922d update package comment 2018-11-18 18:38:46 +00:00
Asim Aslam
212c6c5ae9 Merge pull request #328 from micro/http
add option to set http handlers
2018-11-18 18:32:01 +00:00
Asim Aslam
1d8047a272 add option to set http handlers 2018-11-18 16:32:53 +00:00
16 changed files with 99 additions and 20 deletions

View File

@@ -1,9 +1,8 @@
# Go Micro [![License](https://img.shields.io/:license-apache-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![GoDoc](https://godoc.org/github.com/micro/go-micro?status.svg)](https://godoc.org/github.com/micro/go-micro) [![Travis CI](https://api.travis-ci.org/micro/go-micro.svg?branch=master)](https://travis-ci.org/micro/go-micro) [![Go Report Card](https://goreportcard.com/badge/micro/go-micro)](https://goreportcard.com/report/github.com/micro/go-micro)
Go Micro is a pluggable RPC framework for distributed systems development.
The **micro** philosophy is sane defaults with a pluggable architecture. We provide defaults to get you started quickly but everything can be easily swapped out. It comes with built in support for {json,proto}-rpc encoding, consul or multicast dns for service discovery, http for communication and random hashed client side load balancing.
Go Micro is a pluggable framework for microservice development.
The **micro** philosophy is sane defaults with a pluggable architecture. We provide defaults to get you started quickly but everything can be swapped out.
Plugins are available at [github.com/micro/go-plugins](https://github.com/micro/go-plugins).
Follow us on [Twitter](https://twitter.com/microhq) or join the [Slack](http://slack.micro.mu/) community.
@@ -15,12 +14,12 @@ Go Micro abstracts away the details of distributed systems. Here are the main fe
- **Service Discovery** - Automatic service registration and name resolution
- **Load Balancing** - Client side load balancing built on discovery
- **Message Encoding** - Dynamic encoding based on content-type with protobuf and json support
- **Sync Streaming** - RPC based communication with support for bidirectional streaming
- **Async Messaging** - Native PubSub messaging built in for event driven architectures
- **Sync Streaming** - RPC based request/response with support for bidirectional streaming
- **Async Messaging** - Native pubsub messaging built in for event driven architectures
## Getting Started
For detailed information on the architecture, installation and use of go-micro checkout the [docs](https://micro.mu/docs).
For detailed information on the architecture, installation and use of go-micro checkout the [docs](https://micro.mu/docs/go-micro.html).
## Sponsors

View File

@@ -1,9 +1,11 @@
// Package http provides a http based message broker
package http
import (
"github.com/micro/go-micro/broker"
)
// NewBroker returns a new http broker
func NewBroker(opts ...broker.Option) broker.Broker {
return broker.NewBroker(opts...)
}

23
broker/http/options.go Normal file
View File

@@ -0,0 +1,23 @@
package http
import (
"context"
"net/http"
"github.com/micro/go-micro/broker"
)
// Handle registers the handler for the given pattern.
func Handle(pattern string, handler http.Handler) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
handlers, ok := o.Context.Value("http_handlers").(map[string]http.Handler)
if !ok {
handlers = make(map[string]http.Handler)
}
handlers[pattern] = handler
o.Context = context.WithValue(o.Context, "http_handlers", handlers)
}
}

View File

@@ -18,6 +18,7 @@ import (
"sync"
"time"
"github.com/google/uuid"
"github.com/micro/go-log"
"github.com/micro/go-micro/broker/codec/json"
merr "github.com/micro/go-micro/errors"
@@ -26,7 +27,6 @@ import (
maddr "github.com/micro/util/go/lib/addr"
mnet "github.com/micro/util/go/lib/net"
mls "github.com/micro/util/go/lib/tls"
"github.com/pborman/uuid"
)
// HTTP Broker is a point to point async broker
@@ -116,7 +116,7 @@ func newHttpBroker(opts ...Option) Broker {
}
h := &httpBroker{
id: "broker-" + uuid.NewUUID().String(),
id: "broker-" + uuid.New().String(),
address: addr,
opts: options,
r: reg,
@@ -126,7 +126,19 @@ func newHttpBroker(opts ...Option) Broker {
mux: http.NewServeMux(),
}
// specify the message handler
h.mux.Handle(DefaultSubPath, h)
// get optional handlers
if h.opts.Context != nil {
handlers, ok := h.opts.Context.Value("http_handlers").(map[string]http.Handler)
if ok {
for pattern, handler := range handlers {
h.mux.Handle(pattern, handler)
}
}
}
return h
}
@@ -401,7 +413,7 @@ func (h *httpBroker) Init(opts ...Option) error {
}
if len(h.id) == 0 {
h.id = "broker-" + uuid.NewUUID().String()
h.id = "broker-" + uuid.New().String()
}
// get registry
@@ -508,7 +520,7 @@ func (h *httpBroker) Subscribe(topic string, handler Handler, opts ...SubscribeO
}
// create unique id
id := h.id + "." + uuid.NewUUID().String()
id := h.id + "." + uuid.New().String()
var secure bool

View File

@@ -5,15 +5,15 @@ import (
"testing"
"time"
"github.com/google/uuid"
"github.com/micro/go-micro/registry/mock"
"github.com/pborman/uuid"
)
func sub(be *testing.B, c int) {
be.StopTimer()
m := mock.NewRegistry()
b := NewBroker(Registry(m))
topic := uuid.NewUUID().String()
topic := uuid.New().String()
if err := b.Init(); err != nil {
be.Fatalf("Unexpected init error: %v", err)
@@ -72,7 +72,7 @@ func pub(be *testing.B, c int) {
be.StopTimer()
m := mock.NewRegistry()
b := NewBroker(Registry(m))
topic := uuid.NewUUID().String()
topic := uuid.New().String()
if err := b.Init(); err != nil {
be.Fatalf("Unexpected init error: %v", err)

View File

@@ -4,8 +4,8 @@ import (
"errors"
"sync"
"github.com/google/uuid"
"github.com/micro/go-micro/broker"
"github.com/pborman/uuid"
)
type mockBroker struct {
@@ -112,7 +112,7 @@ func (m *mockBroker) Subscribe(topic string, handler broker.Handler, opts ...bro
sub := &mockSubscriber{
exit: make(chan bool, 1),
id: uuid.NewUUID().String(),
id: uuid.New().String(),
topic: topic,
handler: handler,
opts: options,

View File

@@ -1,9 +1,11 @@
// Package rpc provides an rpc client
package rpc
import (
"github.com/micro/go-micro/client"
)
// NewClient returns a new micro client interface
func NewClient(opts ...client.Option) client.Client {
return client.NewClient(opts...)
}

View File

@@ -1,3 +1,4 @@
// Package jsonrpc provides a json-rpc 1.0 codec
package jsonrpc
import (

View File

@@ -1,3 +1,4 @@
// Protorpc provides a net/rpc proto-rpc codec. See envelope.proto for the format.
package protorpc
import (

View File

@@ -4,8 +4,8 @@ import (
"errors"
"sync"
"github.com/google/uuid"
"github.com/micro/go-micro/server"
"github.com/pborman/uuid"
)
type MockServer struct {
@@ -69,7 +69,7 @@ func (m *MockServer) NewHandler(h interface{}, opts ...server.HandlerOption) ser
}
return &MockHandler{
Id: uuid.NewUUID().String(),
Id: uuid.New().String(),
Hdlr: h,
Opts: options,
}

View File

@@ -1,9 +1,11 @@
// Package rpc provides an rpc server
package rpc
import (
"github.com/micro/go-micro/server"
)
// NewServer returns a micro server interface
func NewServer(opts ...server.Option) server.Server {
return server.NewServer(opts...)
}

View File

@@ -7,8 +7,8 @@ import (
"os/signal"
"syscall"
"github.com/google/uuid"
"github.com/micro/go-log"
"github.com/pborman/uuid"
)
type Server interface {
@@ -63,7 +63,7 @@ var (
DefaultAddress = ":0"
DefaultName = "go-server"
DefaultVersion = "1.0.0"
DefaultId = uuid.NewUUID().String()
DefaultId = uuid.New().String()
DefaultServer Server = newRpcServer()
)

View File

@@ -1,9 +1,11 @@
// Package http returns a http2 transport using net/http
package http
import (
"github.com/micro/go-micro/transport"
)
// NewTransport returns a new http transport using net/http and supporting http2
func NewTransport(opts ...transport.Option) transport.Transport {
return transport.NewTransport(opts...)
}

23
transport/http/options.go Normal file
View File

@@ -0,0 +1,23 @@
package http
import (
"context"
"net/http"
"github.com/micro/go-micro/transport"
)
// Handle registers the handler for the given pattern.
func Handle(pattern string, handler http.Handler) transport.Option {
return func(o *transport.Options) {
if o.Context == nil {
o.Context = context.Background()
}
handlers, ok := o.Context.Value("http_handlers").(map[string]http.Handler)
if !ok {
handlers = make(map[string]http.Handler)
}
handlers[pattern] = handler
o.Context = context.WithValue(o.Context, "http_handlers", handlers)
}
}

View File

@@ -357,6 +357,8 @@ func (h *httpTransportListener) Close() error {
func (h *httpTransportListener) Accept(fn func(Socket)) error {
// create handler mux
mux := http.NewServeMux()
// register our transport handler
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var buf *bufio.ReadWriter
var con net.Conn
@@ -403,6 +405,16 @@ func (h *httpTransportListener) Accept(fn func(Socket)) error {
})
})
// get optional handlers
if h.ht.opts.Context != nil {
handlers, ok := h.ht.opts.Context.Value("http_handlers").(map[string]http.Handler)
if ok {
for pattern, handler := range handlers {
mux.Handle(pattern, handler)
}
}
}
// default http2 server
srv := &http.Server{
Handler: mux,

View File

@@ -1,4 +1,4 @@
// Package is an interface for synchronous communication
// Package transport is an interface for synchronous communication
package transport
import (