http

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2023 License: MIT Imports: 23 Imported by: 3

Documentation

Index

Constants

View Source
const (
	ACAOrigin      = "Access-Control-Allow-Origin"
	ACAMethods     = "Access-Control-Allow-Methods"
	ACACredentials = "Access-Control-Allow-Credentials"
)
View Source
const (
	ApiUrlFormat = "%s%s/%s?%s"
)
View Source
const (
	DomainKey = "domain"
)
View Source
const (
	// StreamErrHeader is used as trailer when stream errors happen.
	StreamErrHeader = "X-Stream-Error"
)

Variables

View Source
var (
	// AllowedExposedHeadersArr defines the default Access-Control-Expose-Headers.
	AllowedExposedHeadersArr = []string{streamHeader, channelHeader, extraContentLengthHeader}
	// AllowedExposedHeaders is the list of defaults Access-Control-Expose-Headers separated by comma.
	AllowedExposedHeaders = strings.Join(AllowedExposedHeadersArr, ", ")
)
View Source
var (
	// ErrNotFound is returned when the endpoint does not exist.
	ErrNotFound = errors.New("404 page not found")
)
View Source
var (
	MIMEEncodings = map[string]cmds.EncodingType{
		"application/json": cmds.JSON,
		"application/xml":  cmds.XML,
		"text/plain":       cmds.Text,
	}
)
View Source
var OptionSkipMap = map[string]bool{
	"api": true,
}
View Source
var RemoteAccessible = func() func(c *cmds.Command, addr string) bool {
	enabled := strings.ToLower(os.Getenv("ENABLE_WALLET_REMOTE")) == "true"
	return func(c *cmds.Command, addr string) bool {
		inWhiteList := false
		if domain, b := c.Extra.GetValue(DomainKey); b {
			for _, d := range domainWhitelist {
				if d == domain {
					inWhiteList = true
					break
				}
			}
		}
		isLocal, err := network.IsLocalIp(strings.Split(addr, ":")[0])
		return (err == nil && isLocal) || !c.NoRemote || (enabled && inWhiteList)
	}
}()

Functions

func AddDomainWhiteList

func AddDomainWhiteList(domain string)

func GetRequestRemoteAddr

func GetRequestRemoteAddr(ctx context.Context) (string, bool)

GetRequestRemoteAddr extracts the remote address value from an existing context

func NewClient

func NewClient(address string, opts ...ClientOpt) cmds.Executor

NewClient constructs a new HTTP-backed command executor.

func NewHandler

func NewHandler(env cmds.Environment, root *cmds.Command, cfg *ServerConfig) http.Handler

NewHandler creates the http.Handler for the given commands.

Types

type ClientOpt

type ClientOpt func(*client)

ClientOpt is an option that can be passed to the HTTP client constructor.

func ClientWithAPIPrefix

func ClientWithAPIPrefix(apiPrefix string) ClientOpt

ClientWithAPIPrefix specifies an API URL prefix.

func ClientWithFallback

func ClientWithFallback(exe cmds.Executor) ClientOpt

ClientWithFallback adds a fallback executor to the client.

Note: This may run the PreRun function twice.

func ClientWithHTTPClient

func ClientWithHTTPClient(hc *http.Client) ClientOpt

ClientWithHTTPClient specifies a custom http.Client. Defaults to http.DefaultClient.

func ClientWithUserAgent

func ClientWithUserAgent(ua string) ClientOpt

ClientWithUserAgent specifies the HTTP user agent for the client.

type Response

type Response struct {
	// contains filtered or unexported fields
}

func (*Response) Error

func (res *Response) Error() *cmds.Error

func (*Response) Length

func (res *Response) Length() uint64

func (*Response) Next

func (res *Response) Next() (interface{}, error)

func (*Response) Request

func (res *Response) Request() *cmds.Request

type ResponseEmitter

type ResponseEmitter interface {
	cmds.ResponseEmitter
	http.Flusher
}

ResponseEmitter interface defines the components that can care of sending the response to HTTP Requests.

func NewFlushForwarder

func NewFlushForwarder(r cmds.ResponseEmitter, f http.Flusher) ResponseEmitter

func NewResponseEmitter

func NewResponseEmitter(w http.ResponseWriter, method string, req *cmds.Request, opts ...ResponseEmitterOption) (ResponseEmitter, error)

NewResponseEmitter returns a new ResponseEmitter.

type ResponseEmitterOption

type ResponseEmitterOption func(*responseEmitter)

ResponseEmitterOption is the type describing options to the NewResponseEmitter function.

type ServerConfig

type ServerConfig struct {
	// APIPath is the prefix of all request paths.
	// Example: host:port/api/v0/add. Here the APIPath is /api/v0
	APIPath string

	// RedirectPaths contain the redirect prefixes to the same handler
	RedirectPaths []string

	// Headers is an optional map of headers that is written out.
	Headers map[string][]string

	// AllowGet indicates whether or not this server accepts GET requests.
	// When unset, the server only accepts POST, HEAD, and OPTIONS.
	//
	// This is different from CORS AllowedMethods. The API may allow GET
	// requests in general, but reject them in CORS. That will allow
	// websites to include resources from the API but not _read_ them.
	AllowGet bool
	// contains filtered or unexported fields
}

func NewServerConfig

func NewServerConfig() *ServerConfig

func (*ServerConfig) AllowedMethods

func (cfg *ServerConfig) AllowedMethods() []string

func (*ServerConfig) AllowedOrigins

func (cfg *ServerConfig) AllowedOrigins() []string

func (*ServerConfig) AppendAllowedOrigins

func (cfg *ServerConfig) AppendAllowedOrigins(origins ...string)

func (*ServerConfig) SetAllowCredentials

func (cfg *ServerConfig) SetAllowCredentials(flag bool)

func (*ServerConfig) SetAllowedMethods

func (cfg *ServerConfig) SetAllowedMethods(methods ...string)

func (*ServerConfig) SetAllowedOrigins

func (cfg *ServerConfig) SetAllowedOrigins(origins ...string)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL