http

package
v0.0.0-...-e4d393c Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2020 License: MIT Imports: 26 Imported by: 0

README

HTTP

Provides HTTP client and server implementations.

endpoint "mock" "http" {
	endpoint = "/"
	method = "GET"
	codec = "json"
	read_timeout = "5s"
	write_timeout = "5s"
}

Services could be defined inside the HCL definitions.

service "mock" "http" {
	host = "https://service.prod.svc.cluster.local"

	options {
		flush_interval = "1s"
		timeout = "60s"
		keep_alive = "60s"
		max_idle_conns = "100"
	}
}

Or in schema definitions such as proto.

rpc Mock(Empty) returns (Empty) {
	option (maestro.http) = {
		endpoint: "/endpoint"
		method: "GET"
	};
};

Object properties available inside the request object could be referenced inside a endpoint.

rpc Mock(Empty) returns (Empty) {
	option (maestro.http) = {
		endpoint: "/endpoint/:id"
		method: "GET"
	};
};

Override services options through a select.

services {
    select "proto.users.*" {
			host = "api.jexia.com"
			insecure = "false"
			ca_file = "/etc/ca.crt"
    }

    select "proto.projects.*" {
      host = "api.jexia.com"
			insecure = "true"
    }
}

Documentation

Index

Constants

View Source
const (
	// InsecureOption connection flag
	InsecureOption = "insecure"
	// CAFileOption certificate authority file path
	CAFileOption = "ca_file"
	// CertFileOption certificate file path
	CertFileOption = "cert_file"
	// KeyFileOption certificate key file path
	KeyFileOption = "key_file"
	// ReadTimeoutOption represents the HTTP read timeout option key
	ReadTimeoutOption = "read_timeout"
	// WriteTimeoutOption represents the HTTP write timeout option key
	WriteTimeoutOption = "write_timeout"
	// EndpointOption represents the HTTP endpoints option key
	EndpointOption = "endpoint"
	// MethodOption represents the HTTP method option key
	MethodOption = "method"
	// CodecOption represents the HTTP listener codec option key
	CodecOption = "codec"
	// FlushIntervalOption represents the flush interval option key
	FlushIntervalOption = "flush_interval"
	// TimeoutOption represents the timeout option key
	TimeoutOption = "timeout"
	// KeepAliveOption represents the keep alive option key
	KeepAliveOption = "keep_alive"
	// MaxIdleConnsOption represents the max idle connections option key
	MaxIdleConnsOption = "max_idle_conns"
)
View Source
const ContentTypeHeaderKey = "Content-Type"

ContentTypeHeaderKey represents the HTTP header content type key

Variables

View Source
var ContentTypes = map[string]string{
	"json": "application/json",
}

ContentTypes represents a lists of available codec content types and their Content-Type value

View Source
var ReferenceLookup = regexp.MustCompile(`(?m):[a-zA-Z\d\^\&\%\$@\_\-\.]+`)

ReferenceLookup is executed to lookup references within a given endpoint. A reference starts with a colon and could contain characters, numbers, underscores and hyphens and dots to define nested properties

Functions

func CopyHTTPHeader

func CopyHTTPHeader(source http.Header) metadata.MD

CopyHTTPHeader copies the given HTTP header into a transport header

func CopyMetadataHeader

func CopyMetadataHeader(header metadata.MD) http.Header

CopyMetadataHeader copies the given transport header into a HTTP header

func GetMethodEndpoint

func GetMethodEndpoint(method *specs.Method) (string, string, error)

GetMethodEndpoint attempts to find the endpoint for the given method. Empty values are returned when a empty method name is given.

func LookupEndpointReferences

func LookupEndpointReferences(method *Method, store refs.Store) string

LookupEndpointReferences looks up the references within the given endpoint and returns the newly constructed endpoint

func NewCaller

func NewCaller() transport.NewCaller

NewCaller constructs a new HTTP caller

func NewListener

func NewListener(addr string, opts specs.Options) transport.NewListener

NewListener constructs a new listener for the given addr

func NewProxy

func NewProxy(options *CallerOptions) *httputil.ReverseProxy

NewProxy constructs a new reverse proxy with the given options

func NewRequest

func NewRequest(req *http.Request) *transport.Request

NewRequest constructs a new transport request of the given http request

func SetHTTPHeader

func SetHTTPHeader(writer http.Header, metadata metadata.MD)

SetHTTPHeader copies the given transport header into a HTTP header

func TemplateReferences

func TemplateReferences(value string, functions functions.Custom) ([]*specs.Property, error)

TemplateReferences returns the property references within the given value

Types

type Call

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

Call represents the HTTP caller implementation

func (*Call) Close

func (call *Call) Close() error

Close closes the given caller

func (*Call) GetMethod

func (call *Call) GetMethod(name string) transport.Method

GetMethod attempts to return a method matching the given name

func (*Call) GetMethods

func (call *Call) GetMethods() []transport.Method

GetMethods returns the available methods within the HTTP caller

func (*Call) SendMsg

func (call *Call) SendMsg(ctx context.Context, rw transport.ResponseWriter, pr *transport.Request, refs refs.Store) error

SendMsg calls the configured host and attempts to call the given endpoint with the given headers and stream

type Caller

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

Caller represents the caller constructor

func (*Caller) Dial

func (caller *Caller) Dial(service *specs.Service, functions functions.Custom, opts specs.Options) (transport.Call, error)

Dial constructs a new caller for the given host

func (*Caller) Name

func (caller *Caller) Name() string

Name returns the name of the given caller

type CallerOptions

type CallerOptions struct {
	Timeout       time.Duration
	KeepAlive     time.Duration
	FlushInterval time.Duration
	MaxIdleConns  int
	Insecure      bool
	CAFile        string
}

CallerOptions represents the available HTTP options

func ParseCallerOptions

func ParseCallerOptions(options specs.Options) (*CallerOptions, error)

ParseCallerOptions parses the given specs options into HTTP options

type EndpointOptions

type EndpointOptions struct {
	Method       string
	Endpoint     string
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	Codec        string
}

EndpointOptions represents the available HTTP options

func ParseEndpointOptions

func ParseEndpointOptions(options specs.Options) (*EndpointOptions, error)

ParseEndpointOptions parses the given specs options into HTTP options

type Handle

type Handle struct {
	*transport.Endpoint

	Options *EndpointOptions
	Proxy   *Proxy
	// contains filtered or unexported fields
}

Handle holds a endpoint its options and a optional request and response

func NewHandle

func NewHandle(ctx instance.Context, endpoint *transport.Endpoint, options *EndpointOptions, constructors map[string]codec.Constructor) (*Handle, error)

NewHandle constructs a new handle function for the given endpoint to the given flow

func (*Handle) HTTPFunc

func (handle *Handle) HTTPFunc(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

HTTPFunc represents a HTTP function which could be used inside a HTTP router

type Listener

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

Listener represents a HTTP listener

func (*Listener) Close

func (listener *Listener) Close() error

Close closes the given listener

func (*Listener) Handle

func (listener *Listener) Handle(ctx instance.Context, endpoints []*transport.Endpoint, codecs map[string]codec.Constructor) error

Handle parses the given endpoints and constructs route handlers

func (*Listener) HandleCors

func (listener *Listener) HandleCors(h http.Handler) http.Handler

HandleCors handles the defining of cors headers for the incoming HTTP request

func (*Listener) Name

func (listener *Listener) Name() string

Name returns the name of the given listener

func (*Listener) Serve

func (listener *Listener) Serve() (err error)

Serve opens the HTTP listener and calls the given handler function on reach request

type ListenerOptions

type ListenerOptions struct {
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	CertFile     string
	KeyFile      string
}

ListenerOptions represents the available HTTP options

func ParseListenerOptions

func ParseListenerOptions(options specs.Options) (*ListenerOptions, error)

ParseListenerOptions parses the given specs options into HTTP options

type Method

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

Method represents a service method

func (*Method) GetName

func (method *Method) GetName() string

GetName returns the method name

func (*Method) References

func (method *Method) References() []*specs.Property

References returns the available method references

type Proxy

type Proxy struct {
	Handle *httputil.ReverseProxy
	Header *metadata.Manager
}

Proxy represents a HTTP reverse proxy

type Request

type Request struct {
	Codec  codec.Manager
	Header *metadata.Manager
}

Request represents a codec manager and header manager

type ResponseWriter

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

A ResponseWriter interface is used by an HTTP handler to construct an HTTP response.

func NewResponseWriter

func NewResponseWriter(rw http.ResponseWriter) *ResponseWriter

NewResponseWriter constructs a new HTTP response writer of the given HTTP response writer

func (*ResponseWriter) Header

func (rw *ResponseWriter) Header() metadata.MD

Header returns the header map that will be sent by WriteHeader. The Header map also is the mechanism with which Handlers can set HTTP trailers.

func (*ResponseWriter) Write

func (rw *ResponseWriter) Write(bb []byte) (int, error)

Write writes the data to the connection as part of an HTTP reply.

func (*ResponseWriter) WriteHeader

func (rw *ResponseWriter) WriteHeader(status int)

WriteHeader sends an HTTP response header with the provided status code.

type TransportResponseWriter

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

A TransportResponseWriter interface is used by an HTTP handler to construct an HTTP response.

func NewTransportResponseWriter

func NewTransportResponseWriter(ctx context.Context, rw transport.ResponseWriter) *TransportResponseWriter

NewTransportResponseWriter constructs a new HTTP response writer of the given transport response writer

func (*TransportResponseWriter) AwaitStatus

func (rw *TransportResponseWriter) AwaitStatus()

AwaitStatus awaits till the response header status code has been written

func (*TransportResponseWriter) Header

func (rw *TransportResponseWriter) Header() http.Header

Header returns the header map that will be sent by WriteHeader. The Header map also is the mechanism with which Handlers can set HTTP trailers.

func (*TransportResponseWriter) Write

func (rw *TransportResponseWriter) Write(bb []byte) (int, error)

Write writes the data to the connection as part of an HTTP reply.

func (*TransportResponseWriter) WriteHeader

func (rw *TransportResponseWriter) WriteHeader(status int)

WriteHeader sends an HTTP response header with the provided status code.

Jump to

Keyboard shortcuts

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