Documentation
¶
Index ¶
- Constants
- func CollectEscapeStringOption(option string, input map[string]any, collectors ...func(v string)) error
- func CollectStringOption(option string, input map[string]any, collectors ...func(v string)) error
- func DeferClose(c io.Closer, err *error)
- func DoDelete(client *http.Client, url string) (*http.Response, error)
- func ErrorResp(rd *render.Render, w http.ResponseWriter, err error)
- func GetCallerIDOnHTTP(r *http.Request) string
- func GetIPPortFromHTTPRequest(r *http.Request) (ip, port string)
- func GetJSON(client *http.Client, url string, data []byte) (*http.Response, error)
- func GetRouteName(req *http.Request) string
- func NewCustomReverseProxies(dialClient *http.Client, urls []url.URL) http.Handler
- func ParseHexKeys(format string, keys [][]byte) (decodedBytes [][]byte, err error)
- func ParseKey(name string, input map[string]any) ([]byte, string, error)
- func ParseTime(t string) (time.Time, error)
- func PatchJSON(client *http.Client, url string, data []byte) (*http.Response, error)
- func PostJSON(client *http.Client, url string, data []byte) (*http.Response, error)
- func PostJSONIgnoreResp(client *http.Client, url string, data []byte) error
- func ReadJSON(r io.ReadCloser, data any) error
- func ReadJSONRespondError(rd *render.Render, w http.ResponseWriter, body io.ReadCloser, data any) error
- func RegisterUserDefinedHandlers(registerMap map[string]http.Handler, group *APIServiceGroup, ...) error
- func TagJSONError(err error) error
- type APIServiceGroup
- type AccessPath
- type CallerIDRoundTripper
- type FieldError
- type JSONError
Constants ¶
const ( // PDRedirectorHeader is used to mark which PD redirected this request. PDRedirectorHeader = "PD-Redirector" // PDAllowFollowerHandleHeader is used to mark whether this request is allowed to be handled by the follower PD. PDAllowFollowerHandleHeader = "PD-Allow-follower-handle" // #nosec G101 // XForwardedForHeader is used to mark the client IP. XForwardedForHeader = "X-Forwarded-For" // XForwardedPortHeader is used to mark the client port. XForwardedPortHeader = "X-Forwarded-Port" // XRealIPHeader is used to mark the real client IP. XRealIPHeader = "X-Real-Ip" // XCallerIDHeader is used to mark the caller ID. XCallerIDHeader = "X-Caller-ID" // XForbiddenForwardToMicroServiceHeader is used to indicate that forwarding the request to a microservice is explicitly disallowed. XForbiddenForwardToMicroServiceHeader = "X-Forbidden-Forward-To-MicroService" // XForwardedToMicroServiceHeader is used to signal that the request has already been forwarded to a microservice. XForwardedToMicroServiceHeader = "X-Forwarded-To-MicroService" )
const ( // CorePath the core group, is at REST path `/pd/api/v1`. CorePath = "/pd/api/v1" // ExtensionsPath the named groups are REST at `/pd/apis/{GROUP_NAME}/{Version}`. ExtensionsPath = "/pd/apis" )
Variables ¶
This section is empty.
Functions ¶
func CollectEscapeStringOption ¶
func CollectEscapeStringOption(option string, input map[string]any, collectors ...func(v string)) error
CollectEscapeStringOption is used to collect string using escaping from input map for given option
func CollectStringOption ¶
CollectStringOption is used to collect string using from input map for given option
func DeferClose ¶
DeferClose captures the error returned from closing (if an error occurs). This is designed to be used in a defer statement.
func ErrorResp ¶
func ErrorResp(rd *render.Render, w http.ResponseWriter, err error)
ErrorResp Respond to the client about the given error, integrating with errcode.ErrorCode.
Important: if the `err` is just an error and not an errcode.ErrorCode (given by errors.Cause), then by default an error is assumed to be a 500 Internal Error.
If the error is nil, this also responds with a 500 and logs at the error level.
func GetCallerIDOnHTTP ¶
GetCallerIDOnHTTP returns caller ID from the request header.
func GetIPPortFromHTTPRequest ¶
GetIPPortFromHTTPRequest returns http client host IP and port from context. Because `X-Forwarded-For ` header has been written into RFC 7239(Forwarded HTTP Extension), so `X-Forwarded-For` has the higher priority than `X-Real-Ip`. And both of them have the higher priority than `RemoteAddr`
func GetRouteName ¶
GetRouteName return mux route name registered
func NewCustomReverseProxies ¶
NewCustomReverseProxies returns the custom reverse proxies.
func ParseHexKeys ¶
ParseHexKeys decodes hexadecimal src into DecodedLen(len(src)) bytes if the format is "hex".
ParseHexKeys expects that each key contains only hexadecimal characters and each key has even length. If existing one key is malformed, ParseHexKeys returns the original bytes.
func ParseTime ¶
ParseTime parses a time string with the format "1694580288" If the string is empty, it returns a zero time.
func PostJSONIgnoreResp ¶
PostJSONIgnoreResp is used to do post request with JSON body and ignore response.
func ReadJSON ¶
func ReadJSON(r io.ReadCloser, data any) error
ReadJSON reads a JSON data from r and then closes it. An error due to invalid json will be returned as a JSONError
func ReadJSONRespondError ¶
func ReadJSONRespondError(rd *render.Render, w http.ResponseWriter, body io.ReadCloser, data any) error
ReadJSONRespondError writes json into data. On error respond with a 400 Bad Request
func RegisterUserDefinedHandlers ¶
func RegisterUserDefinedHandlers(registerMap map[string]http.Handler, group *APIServiceGroup, handler http.Handler) error
RegisterUserDefinedHandlers register the user defined handlers.
func TagJSONError ¶
TagJSONError wraps the JSON error to one type.
Types ¶
type APIServiceGroup ¶
APIServiceGroup used to register the HTTP REST API.
func (*APIServiceGroup) Path ¶
func (sg *APIServiceGroup) Path() string
Path returns the path of the service.
type AccessPath ¶
AccessPath is used to identify HTTP api access path including path and method
func NewAccessPath ¶
func NewAccessPath(path, method string) AccessPath
NewAccessPath returns an AccessPath
type CallerIDRoundTripper ¶
type CallerIDRoundTripper struct {
// contains filtered or unexported fields
}
CallerIDRoundTripper is used to add caller ID in the HTTP header.
func NewCallerIDRoundTripper ¶
func NewCallerIDRoundTripper(roundTripper http.RoundTripper, callerID string) *CallerIDRoundTripper
NewCallerIDRoundTripper returns a new `CallerIDRoundTripper`.
type FieldError ¶
type FieldError struct {
// contains filtered or unexported fields
}
FieldError connects an error to a particular field
func ParseUint64VarsField ¶
func ParseUint64VarsField(vars map[string]string, varName string) (uint64, *FieldError)
ParseUint64VarsField connects strconv.ParseUint with request variables It hardcodes the base to 10 and bit size to 64 Any error returned will connect the requested field to the error via FieldError