Documentation ¶
Index ¶
- Constants
- func AddRequestId(t any) http.Header
- func ExchangeHeaders(h http.Header) (req, resp, status string)
- func FmtRFC3339Millis(t time.Time) string
- func HttpCode(code int) int
- func HttpStatus(code int) string
- func JsonMarkup(name, value string, stringValue bool) string
- func NewContext(ctx context.Context) context.Context
- func NewInvalidBodyTypeError(t any) error
- func NewValues(o Origin) url.Values
- func OriginMatch(target Origin, filter Origin) bool
- func ParseRFC3339Millis(ts string) (time.Time, error)
- func ParseTimestamp2(s string) (time.Time, error)
- func RequestId(t any) string
- func SetLogFunc(fn LogFunc)
- func StringMatch(target, filter string) bool
- type Bypass
- type ErrorHandler
- type ExchangeProxy
- type HttpExchange
- type HttpHandler
- type Log
- type LogFunc
- type Origin
- type Output
- type Status
- func (s *Status) AddLocation() *Status
- func (s *Status) AddParentLocation() *Status
- func (s *Status) HttpCode() int
- func (s *Status) NoContent() bool
- func (s *Status) NotFound() bool
- func (s *Status) OK() bool
- func (s *Status) StatusCode() int
- func (s *Status) String() string
- func (s *Status) Timeout() bool
- func (s *Status) Trace() []string
- func (s *Status) WithRequestId(t any) *Status
Examples ¶
Constants ¶
const ( TimestampName = "timestamp" StatusName = "status" CodeName = "code" TraceName = "trace" RequestIdName = "request-id" ErrorsName = "errors" )
const ( XRequestId = "X-Request-Id" XRelatesTo = "X-Relates-To" XDomain = "X-Domain" XVersion = "X-Version" XURLPath = "x-url-path" XTest = "X-Test" XFrom = "X-From" XTo = "X-To" XRoute = "X-Route" XExchangeRequest = "X-Exchange-Request" XExchangeResponse = "X-Exchange-Response" XExchangeStatus = "X-Exchange-Status" )
const ( RegionKey = "region" ZoneKey = "zone" SubZoneKey = "sub-zone" HostKey = "host" InstanceIdKey = "id" RouteKey = "route" RegionZoneHostFmt = "%v:%v.%v.%v" RegionZoneSubZoneHostFmt = "%v:%v.%v.%v.%v" )
const ( StatusInvalidContent = int(90) // Content is not available, is nil, or is of the wrong type, usually found via unmarshalling StatusIOError = int(91) // I/O operation failed StatusJsonDecodeError = int(92) // Json decoding failed StatusJsonEncodeError = int(93) // Json encoding failed StatusContentEncodingError = int(94) // Content encoding error StatusContentEncodingInvalidType = int(95) // Content encoding error StatusNotProvided = int(96) // No status is available StatusRateLimited = int(97) // Rate limited StatusNotStarted = int(98) // Not started StatusHaveContent = int(99) // Content is available StatusGzipEncodingError = int(100) // Gzip encoding error StatusGzipDecodingError = int(101) // Gzip decoding error StatusTxnBeginError = int(102) // Transaction processing begin error StatusTxnRollbackError = int(103) // Transaction processing rollback error StatusTxnCommitError = int(104) // Transaction processing commit error StatusExecError = int(105) // Execution error, as in a database call StatusNoChange = int(106) // State has not changed StatusInvalidArgument = 3 //codes.InvalidArgument // The client specified an invalid argument. Note that this differs from FAILED_PRECONDITION. INVALID_ARGUMENT indicates arguments that are problematic regardless of the state of the system (e.g., a malformed file name). StatusDeadlineExceeded = 4 //codes.DeadlineExceeded // The deadline expired before the operation could complete. For operations that change the state of the system, this error may be returned even if the operation has completed successfully. For example, a successful response from a server could have been delayed long )
Variables ¶
This section is empty.
Functions ¶
func AddRequestId ¶
AddRequestId - add a request to an http.Request or an http.Header
Example ¶
h := AddRequestId(nil) fmt.Printf("test: AddRequestId(nil) -> [empty:%v]\n", len(h.Get(XRequestId)) == 0) head := make(http.Header) h = AddRequestId(head) fmt.Printf("test: AddRequestId(head) -> [empty:%v]\n", len(h.Get(XRequestId)) == 0) head = make(http.Header) head.Add(XRequestId, "123-45-head") h = AddRequestId(head) fmt.Printf("test: AddRequestId(head) -> %v\n", h.Get(XRequestId)) req, _ := http.NewRequest("", "https.www.google.com", nil) h = AddRequestId(req) fmt.Printf("test: RequestId(request) -> [empty:%v]\n", len(h.Get(XRequestId)) == 0) req, _ = http.NewRequest("", "https.www.google.com", nil) req.Header.Set(XRequestId, "123-456-request") h = AddRequestId(req) fmt.Printf("test: RequestId(request) -> %v\n", h.Get(XRequestId))
Output: test: AddRequestId(nil) -> [empty:false] test: AddRequestId(head) -> [empty:false] test: AddRequestId(head) -> 123-45-head test: RequestId(request) -> [empty:false] test: RequestId(request) -> 123-456-request
func ExchangeHeaders ¶
func FmtRFC3339Millis ¶
FmtRFC3339Millis - format time https://datatracker.ietf.org/doc/html/rfc3339
func HttpStatus ¶
HttpStatus - string representation of status code
func JsonMarkup ¶
JsonMarkup - markup a name/value pair
func NewInvalidBodyTypeError ¶
NewInvalidBodyTypeError - invalid type error
func NewValues ¶
Example ¶
o := Origin{ Region: "region", Zone: "zone", SubZone: "sub-zone", Host: "host", InstanceId: "", } values := NewValues(o) fmt.Printf("test: NewValues() -> [%v]\n", values)
Output: test: NewValues() -> [map[host:[host] region:[region] sub-zone:[sub-zone] zone:[zone]]]
func OriginMatch ¶
Example ¶
target := Origin{ Region: "Region", Zone: "zonE", SubZone: "sub-zone", Host: "hOst", InstanceId: "", } filter := Origin{ Region: "", Zone: "", SubZone: "", Host: "", InstanceId: "", } fmt.Printf("test: OriginMatch(%v,%v) -> [match:%v]\n", target, filter, OriginMatch(target, filter)) filter.Region = "region" fmt.Printf("test: OriginMatch(%v,%v) -> [match:%v]\n", target, filter, OriginMatch(target, filter)) filter.Zone = "zone" fmt.Printf("test: OriginMatch(%v,%v) -> [match:%v]\n", target, filter, OriginMatch(target, filter)) filter.SubZone = "sub-zone" fmt.Printf("test: OriginMatch(%v,%v) -> [match:%v]\n", target, filter, OriginMatch(target, filter)) filter.Host = "host" fmt.Printf("test: OriginMatch(%v,%v) -> [match:%v]\n", target, filter, OriginMatch(target, filter)) filter.SubZone = "" fmt.Printf("test: OriginMatch(%v,%v) -> [match:%v]\n", target, filter, OriginMatch(target, filter)) filter.SubZone = "invalid" fmt.Printf("test: OriginMatch(%v,%v) -> [match:%v]\n", target, filter, OriginMatch(target, filter))
Output: test: OriginMatch({Region zonE sub-zone hOst },{ }) -> [match:false] test: OriginMatch({Region zonE sub-zone hOst },{region }) -> [match:true] test: OriginMatch({Region zonE sub-zone hOst },{region zone }) -> [match:true] test: OriginMatch({Region zonE sub-zone hOst },{region zone sub-zone }) -> [match:true] test: OriginMatch({Region zonE sub-zone hOst },{region zone sub-zone host }) -> [match:true] test: OriginMatch({Region zonE sub-zone hOst },{region zone host }) -> [match:true] test: OriginMatch({Region zonE sub-zone hOst },{region zone invalid host }) -> [match:false]
func ParseRFC3339Millis ¶
ParseRFC3339Millis - parse a string into a time.Time, using the following string : 2023-04-14T14:14:45.522Z
func ParseTimestamp2 ¶
ParseTimestamp2 - parse a string into a time.Time, using the following string : 2023-04-14 14:14:45.522460
func RequestId ¶
RequestId - return a request id from any type and will create a new one if not found
Example ¶
id := RequestId("123-456-string") fmt.Printf("test: RequestId(string) -> %v\n", id) req, _ := http.NewRequest("", "https.www.google.com", nil) req.Header.Set(XRequestId, "123-456-request") id = RequestId(req) fmt.Printf("test: RequestId(request) -> %v\n", id) h := make(http.Header) h.Set(XRequestId, "123-456-header") id = RequestId(h) fmt.Printf("test: RequestId(header) -> %v\n", id)
Output: test: RequestId(string) -> 123-456-string test: RequestId(request) -> 123-456-request test: RequestId(header) -> 123-456-header
func StringMatch ¶
Types ¶
type ErrorHandler ¶
ErrorHandler - error handler interface
type ExchangeProxy ¶
type ExchangeProxy struct {
// contains filtered or unexported fields
}
ExchangeProxy - key value pairs of a domain -> HttpExchange
func NewExchangeProxy ¶
func NewExchangeProxy() *ExchangeProxy
NewExchangeProxy - create a new Exchange Proxy
func (*ExchangeProxy) Lookup ¶
func (p *ExchangeProxy) Lookup(domain string) HttpExchange
Lookup - get an HttpExchange from the proxy, using an domain as a key
func (*ExchangeProxy) LookupByRequest ¶
func (p *ExchangeProxy) LookupByRequest(req *http.Request) HttpExchange
LookupByRequest - find an HttpExchange from a request
func (*ExchangeProxy) Register ¶
func (p *ExchangeProxy) Register(domain string, handler HttpExchange) error
type HttpHandler ¶
type HttpHandler func(w http.ResponseWriter, r *http.Request)
type Origin ¶
type Origin struct { Region string `json:"region"` Zone string `json:"zone"` SubZone string `json:"sub-zone"` Host string `json:"host"` Route string `json:"route"` InstanceId string `json:"instance-id"` }
Origin - location
func NewOrigin ¶
Example ¶
o := Origin{ Region: "region", Zone: "zone", SubZone: "sub-zone", Host: "host", Route: "route", InstanceId: "", } values := NewValues(o) o = NewOrigin(values) fmt.Printf("test: NewOrigin() -> [%v]\n", o)
Output: test: NewOrigin() -> [{region zone sub-zone host route }]
func (Origin) Tag ¶
Example ¶
o := Origin{ Region: "region", Zone: "zone", SubZone: "sub-zone", Host: "host", InstanceId: "", } fmt.Printf("test: Tag() -> [%v]\n", o.Tag()) o.Zone = "" fmt.Printf("test: Tag() -> [%v]\n", o.Tag()) o.Host = "" fmt.Printf("test: Tag() -> [%v]\n", o.Tag()) o.SubZone = "" fmt.Printf("test: Tag() -> [%v]\n", o.Tag())
Output: test: Tag() -> [region:zone:sub-zone:host] test: Tag() -> [region:sub-zone:host] test: Tag() -> [region:sub-zone] test: Tag() -> [region]
func (Origin) Uri ¶
Example ¶
target := Origin{ Region: "region", Zone: "zone", SubZone: "sub-zone", Host: "host", InstanceId: "", } fmt.Printf("test: Origin_Uri_SubZone() -> [%v]\n", target.Uri("class")) target.Route = "route" fmt.Printf("test: Origin_Uri_SubZone_Route() -> [%v]\n", target.Uri("class")) target.SubZone = "" target.Route = "" fmt.Printf("test: Origin_Uri_No_SubZone() -> [%v]\n", target.Uri("class"))
Output: test: Origin_Uri_SubZone() -> [class:region.zone.sub-zone.host] test: Origin_Uri_SubZone_Route() -> [class:region.zone.sub-zone.host.route] test: Origin_Uri_No_SubZone() -> [class:region.zone.host]
type Status ¶
type Status struct { Code int `json:"code"` Err error `json:"err"` RequestId string `json:"request-id"` Handled bool `json:"handled"` Duration time.Duration Content any // contains filtered or unexported fields }
func NewStatusError ¶
func StatusBadRequest ¶
func StatusBadRequest() *Status
func StatusNoContent ¶
func StatusNoContent() *Status
func StatusNotFound ¶
func StatusNotFound() *Status
func (*Status) AddLocation ¶
func (*Status) AddParentLocation ¶
func (*Status) StatusCode ¶
func (*Status) WithRequestId ¶
Example ¶
h := make(http.Header) h.Add(XRequestId, "123456789") status := StatusNotFound() fmt.Printf("test: Status_WithRequestId() -> [request-id:%v]\n", status.WithRequestId(h).RequestId) fmt.Printf("test: Status_WithRequestId() -> [request-id:%v]\n", status.WithRequestId("333-444-555").RequestId)
Output: test: Status_WithRequestId() -> [request-id:123456789] test: Status_WithRequestId() -> [request-id:333-444-555]