Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/micro/go-micro into rewri…
Browse files Browse the repository at this point in the history
…te-store
  • Loading branch information
Jake Sanders committed Mar 5, 2020
2 parents b08c219 + ce2ba71 commit 171fe7c
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 9 deletions.
43 changes: 43 additions & 0 deletions api/server/cors/cors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cors

import (
"net/http"
)

// CombinedCORSHandler wraps a server and provides CORS headers
func CombinedCORSHandler(h http.Handler) http.Handler {
return corsHandler{h}
}

type corsHandler struct {
handler http.Handler
}

func (c corsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
SetHeaders(w, r)

if r.Method == "OPTIONS" {
return
}

c.handler.ServeHTTP(w, r)
}

// SetHeaders sets the CORS headers
func SetHeaders(w http.ResponseWriter, r *http.Request) {
set := func(w http.ResponseWriter, k, v string) {
if v := w.Header().Get(k); len(v) > 0 {
return
}
w.Header().Set(k, v)
}

if origin := r.Header.Get("Origin"); len(origin) > 0 {
set(w, "Access-Control-Allow-Origin", origin)
} else {
set(w, "Access-Control-Allow-Origin", "*")
}

set(w, "Access-Control-Allow-Methods", "POST, PATCH, GET, OPTIONS, PUT, DELETE")
set(w, "Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
}
9 changes: 8 additions & 1 deletion api/server/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/gorilla/handlers"
"github.com/micro/go-micro/v2/api/server"
"github.com/micro/go-micro/v2/api/server/cors"
log "github.com/micro/go-micro/v2/logger"
)

Expand Down Expand Up @@ -45,7 +46,13 @@ func (s *httpServer) Init(opts ...server.Option) error {
}

func (s *httpServer) Handle(path string, handler http.Handler) {
s.mux.Handle(path, handlers.CombinedLoggingHandler(os.Stdout, handler))
h := handlers.CombinedLoggingHandler(os.Stdout, handler)

if s.opts.EnableCORS {
h = cors.CombinedCORSHandler(h)
}

s.mux.Handle(path, h)
}

func (s *httpServer) Start() error {
Expand Down
7 changes: 7 additions & 0 deletions api/server/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ type Option func(o *Options)

type Options struct {
EnableACME bool
EnableCORS bool
ACMEProvider acme.Provider
EnableTLS bool
ACMEHosts []string
TLSConfig *tls.Config
}

func EnableCORS(b bool) Option {
return func(o *Options) {
o.EnableCORS = b
}
}

func EnableACME(b bool) Option {
return func(o *Options) {
o.EnableACME = b
Expand Down
4 changes: 4 additions & 0 deletions logger/helper.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package logger

import "os"

type Helper struct {
Logger
fields map[string]interface{}
Expand Down Expand Up @@ -51,10 +53,12 @@ func (h *Helper) Errorf(template string, args ...interface{}) {

func (h *Helper) Fatal(args ...interface{}) {
h.Logger.Fields(h.fields).Log(ErrorLevel, args...)
os.Exit(1)
}

func (h *Helper) Fatalf(template string, args ...interface{}) {
h.Logger.Fields(h.fields).Logf(ErrorLevel, template, args...)
os.Exit(1)
}

func (h *Helper) WithError(err error) *Helper {
Expand Down
19 changes: 11 additions & 8 deletions server/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,14 +630,17 @@ func (s *rpcServer) Register() error {
// set what we're advertising
s.opts.Advertise = addr

// subscribe to the topic with own name
sub, err := s.opts.Broker.Subscribe(config.Name, s.HandleEvent)
if err != nil {
return err
}
// router can exchange messages
if s.opts.Router != nil {
// subscribe to the topic with own name
sub, err := s.opts.Broker.Subscribe(config.Name, s.HandleEvent)
if err != nil {
return err
}

// save the subscriber
s.subscriber = sub
// save the subscriber
s.subscriber = sub
}

// subscribe for all of the subscribers
for sb := range s.subscribers {
Expand All @@ -654,11 +657,11 @@ func (s *rpcServer) Register() error {
opts = append(opts, broker.DisableAutoAck())
}

log.Infof("Subscribing to topic: %s", sub.Topic())
sub, err := config.Broker.Subscribe(sb.Topic(), s.HandleEvent, opts...)
if err != nil {
return err
}
log.Infof("Subscribing to topic: %s", sub.Topic())

s.subscribers[sb] = []broker.Subscriber{sub}
}
Expand Down
17 changes: 17 additions & 0 deletions util/jitter/jitter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Package jitter provides a random jitter
package jitter

import (
"math/rand"
"time"
)

var (
r = rand.New(rand.NewSource(time.Now().UnixNano()))
)

// Do returns a random time to jitter with max cap specified
func Do(d time.Duration) time.Duration {
v := r.Float64() * float64(d.Nanoseconds())
return time.Duration(v)
}

0 comments on commit 171fe7c

Please sign in to comment.