httpserver

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2019 License: Apache-2.0 Imports: 19 Imported by: 3

Documentation

Overview

Package httpserver provides the HttpServer facility which defines a configurable HTTP server for processing web-service requests.

The HttpServer facility provides a server that will listen for HTTP web-service requests and map them to the web-service endpoints defined by your application. A full description of how to configure this facility can be found at http://granitic.io/1.0/ref/http-server

This package defines two main types HttpServer and AccessLogWriter. HttpServer is a layer over Go's built-in http.Server adding runtime control (suspension, resumption) and mapping of requests to instances of ws.Handler. AccessLogWriter supports Apache/Tomcat style access log formatting and writing.

Most applications will only need to enable this facility (probably changing the listen Port) and define mappings between incoming paths and application logic in their component definition files. See handler.WsHandler for more details.

Index

Constants

View Source
const HttpServerAbnormalStatusFieldName = "AbnormalStatusWriter"

The field on the HttpServer component into which a ws.AbnormalStatusWriter can be injected. Most applications will use either the JsonWs or XmlWs facility, in which case a AbnormalStatusWriter that will respond to requests with an abnormal result (404, 503 etc) by sending a JSON or XML response respectively.

If this behaviour is undesirable, an alternative AbnormalStatusWriter can set by using the frameworkModifiers mechanism (see http://granitic.io/1.0/ref/components)

View Source
const HttpServerComponentName = instance.FrameworkPrefix + "HttpServer"

The name of the HttpServer component as stored in the IoC framework.

View Source
const PresetCombinedFormat = "%h %l %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-agent}i\""

The log format used when AccessLogWriter.LogLinePreset is set to combined. Similar to the Apache HTTP preset of the same name.

View Source
const PresetFrameworkFormat = "%h XFF[%{X-Forwarded-For}i] %l %u [%{02/Jan/2006:15:04:05 Z0700}t] \"%m %U%q\" %s %bB %{us}Tμs"

The log format used when AccessLogWriter.LogLinePreset is set to framework. Uses the X-Forwarded-For header to show all IP addresses that the request has been proxied for (useful for services that sit behind multiple load-balancers and proxies) and logs processing time in microseconds.

Variables

This section is empty.

Functions

This section is empty.

Types

type AccessLogWriter

type AccessLogWriter struct {

	// The path of the log file to be written to (and created if required)
	LogPath string

	// The format of each log line. See the top of this GoDoc page for supported formats. Mutually exclusive with LogLinePreset.
	LogLineFormat string

	// A pre-defined format. Supported values are framework or combined. Mutually exclusive with LogLineFormat.
	LogLinePreset string

	//The number of lines that can be buffered for asynchronous writing to the log file before calls to LogRequest block.
	LineBufferSize int

	//Whether or not timestamps should be converted to UTC before they are written to the access log.
	UtcTimes bool
	// contains filtered or unexported fields
}

A component able to asynchronously write an Apache HTTPD style access log. See the top of this GoDoc page for more information.

func (*AccessLogWriter) LogRequest

func (alw *AccessLogWriter) LogRequest(req *http.Request, res *httpendpoint.HttpResponseWriter, rec *time.Time, fin *time.Time, ctx context.Context)

LogRequest generates an access log line according the configured format. As long as the number of log lines waiting to be written to the file does not exceed the value of AccessLogWriter.LineBufferSize, this method will return immediately.

func (*AccessLogWriter) PrepareToStop

func (alw *AccessLogWriter) PrepareToStop()

PrepareToStop settings state to 'Stopping'

func (*AccessLogWriter) ReadyToStop

func (alw *AccessLogWriter) ReadyToStop() (bool, error)

ReadyToStop always returns true

func (*AccessLogWriter) StartComponent

func (alw *AccessLogWriter) StartComponent() error

StartComponent parses the specified log format, sets up a channel to buffer lines for asynchrnous writing and opens the log file. An error is returned if any of these steps fails.

func (*AccessLogWriter) Stop

func (alw *AccessLogWriter) Stop() error

Stop closes the log file

type HttpServer added in v1.1.0

type HttpServer struct {

	// Logger used by Granitic framework components. Automatically injected.
	FrameworkLogger logging.Logger

	// A component able to write an access log. Automatically added by this facility's builder, is access log support is enabled.
	AccessLogWriter *AccessLogWriter

	// Whether or not access logging should be enabled.
	AccessLogging bool

	// Whether or not instances of httpendpoint.HttpEndpointProvider found in the IoC container should be automatically
	// registered with this server
	AutoFindHandlers bool

	// The TCP port on which the HTTP server should listen for requests.
	Port int

	// The IP/hostname this server should listen on, follows standard Go net package syntax. Empty string means listen on all.
	Address string

	// A component able to write valid HTTP responses in the event a user request results in an abnormal result
	// (not found, server too busy, panic in application logic). If you use the JsonWs or XmlWs facility, this is automatically injected.
	AbnormalStatusWriter ws.AbnormalStatusWriter

	// The name of a component in the IoC container that should be used as an AbnormalStatusWriter if one is not being auto-injected.
	AbnormalStatusWriterName string

	// The number of HTTP requests currently being handled by the server.
	ActiveRequests int64

	// Allow request instrumentation to begin BEFORE too-busy/suspended checks. Allows instrumentation of requests that would be trivially
	// rejected, but potentially increases risk of denial-of-service if instrumentation setup causes load or consumes memory.
	AllowEarlyInstrumentation bool

	// How many concurrent requests the server should allow before returning 'too busy' responses to subsequent requests.
	MaxConcurrent int64

	// The HTTP status code returned with 'too busy responses'. Normally 503
	TooBusyStatus int

	// A component able to examine an incoming request and determine which version of functionality is being requested.
	VersionExtractor httpendpoint.RequestedVersionExtractor

	// A component able to use data in an HTTP request's headers to populate a context
	IdContextBuilder IdentifiedRequestContextBuilder

	// ID of a component that implements instrument.RequestInstrumentationManager
	RequestInstrumentationManagerName string
	// contains filtered or unexported fields
}

func (*HttpServer) AllowAccess added in v1.1.0

func (h *HttpServer) AllowAccess() error

AllowAccess starts the server listening on the configured address and port. Returns an error if the port is already in use.

func (*HttpServer) Container added in v1.1.0

func (h *HttpServer) Container(container *ioc.ComponentContainer)

Implements ioc.ContainerAccessor

func (*HttpServer) PrepareToStop added in v1.1.0

func (h *HttpServer) PrepareToStop()

PrepareToStop sets state to Stopping. Any subsequent requests will receive a 'too busy response'

func (*HttpServer) ReadyToStop added in v1.1.0

func (h *HttpServer) ReadyToStop() (bool, error)

ReadyToStop returns false is the server is currently handling any requests.

func (*HttpServer) Resume added in v1.1.0

func (h *HttpServer) Resume() error

Resume allows subsequent requests to be processed normally (reserves the effect of calling Suspend).

func (*HttpServer) SetProvidersManually added in v1.1.0

func (h *HttpServer) SetProvidersManually(p map[string]httpendpoint.HttpEndpointProvider)

SetProvidersManually manually injects a set of httpendpoint.HttpEndpointProviders when auto finding is disabled.

func (*HttpServer) StartComponent added in v1.1.0

func (h *HttpServer) StartComponent() error

StartComponent Finds and registers any available components that implement httpendpoint.HttpEndpointProvider (normally instances of handler.WsHandler) unless auto finding of handlers is disabled. The server does not actually start listening for requests until the IoC container calls AllowAccess.

func (*HttpServer) Stop added in v1.1.0

func (h *HttpServer) Stop() error

Stop sets state to Stopped. Any subsequent requests will receive a 'too busy response'. Note that the HTTP server is still listening on its configured port and address.

func (*HttpServer) Suspend added in v1.1.0

func (h *HttpServer) Suspend() error

Suspend causes all subsequent new HTTP requests to receive a 'too busy' response until Resume is called.

type HttpServerFacilityBuilder

type HttpServerFacilityBuilder struct {
}

Creates the components that make up the HttpServer facility (the server and an access log writer).

func (*HttpServerFacilityBuilder) BuildAndRegister

See FacilityBuilder.BuildAndRegister

func (*HttpServerFacilityBuilder) DependsOnFacilities

func (hsfb *HttpServerFacilityBuilder) DependsOnFacilities() []string

See FacilityBuilder.DependsOnFacilities

func (*HttpServerFacilityBuilder) FacilityName

func (hsfb *HttpServerFacilityBuilder) FacilityName() string

See FacilityBuilder.FacilityName

type IdentifiedRequestContextBuilder added in v1.3.0

type IdentifiedRequestContextBuilder interface {
	// WithIdentity uses information in the supplied request to assign an ID to this context
	WithIdentity(ctx context.Context, req *http.Request) (context.Context, error)
	Id(ctx context.Context) string
}

Components implementing this interface are called by the HttpServer facility before the request is matched to a handler. It is an opportunity to extract information from the HTTP request to add an ID for this request to the context that will be passed to the handler.

It is the responsibility of the implementor to control the uniqueness of the allocated ID

It is recommended that only the request meta data (headers, path, parameters) are accessed by implementations, as loading the request body will interfere with later phases of the request processing.

Jump to

Keyboard shortcuts

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