-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/micro/go-micro into rewri…
…te-store
- Loading branch information
Showing
6 changed files
with
90 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |