-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from krakendio/header_id
Replace the header.Id modifier with our custom implementation since t…
- Loading branch information
Showing
4 changed files
with
62 additions
and
33 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
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,56 @@ | ||
package header | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/google/martian" | ||
"github.com/google/martian/parse" | ||
"github.com/google/uuid" | ||
) | ||
|
||
const defaultHeaderName string = "X-Krakend-Id" | ||
|
||
type idModifier struct { | ||
header string | ||
} | ||
|
||
type idModifierJSON struct { | ||
Scope []parse.ModifierType `json:"scope"` | ||
Header string `json:"header"` | ||
} | ||
|
||
// NewIDModifier returns a request modifier that will set a header with the name | ||
// X-Krakend-Id with a value that is a unique identifier for the request. In the case | ||
// that the X-Krakend-Id header is already set, the header is unmodified. | ||
func NewIDModifier(header string) martian.RequestModifier { | ||
if header == "" { | ||
header = defaultHeaderName | ||
} | ||
return &idModifier{header: header} | ||
} | ||
|
||
// ModifyRequest sets the X-Krakend-Id header with a unique identifier. In the case | ||
// that the X-Krakend-Id header is already set, the header is unmodified. | ||
func (im *idModifier) ModifyRequest(req *http.Request) error { | ||
// Do not rewrite an ID if req already has one | ||
if req.Header.Get(im.header) != "" { | ||
return nil | ||
} | ||
|
||
id := uuid.New() | ||
req.Header.Set(im.header, id.String()) | ||
|
||
return nil | ||
} | ||
|
||
func IdModifierFromJSON(b []byte) (*parse.Result, error) { | ||
msg := &idModifierJSON{} | ||
if err := json.Unmarshal(b, msg); err != nil { | ||
return nil, err | ||
} | ||
|
||
modifier := NewIDModifier(msg.Header) | ||
|
||
return parse.NewResult(modifier, msg.Scope) | ||
} |
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