granitic: github.com/graniticio/granitic/facility/httpserver Index | Files

package httpserver

import "github.com/graniticio/granitic/facility/httpserver"

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

Package Files

accesslog.go builder.go httpserver.go

Constants

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)

const HttpServerComponentName = instance.FrameworkPrefix + "HttpServer"

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

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.

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.

type AccessLogWriter Uses

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 Uses

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 Uses

func (alw *AccessLogWriter) PrepareToStop()

PrepareToStop settings state to 'Stopping'

func (*AccessLogWriter) ReadyToStop Uses

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

ReadyToStop always returns true

func (*AccessLogWriter) StartComponent Uses

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 Uses

func (alw *AccessLogWriter) Stop() error

Stop closes the log file

type HttpServer Uses

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

    // 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
    // contains filtered or unexported fields
}

func (*HttpServer) AllowAccess Uses

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 Uses

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

Implements ioc.ContainerAccessor

func (*HttpServer) PrepareToStop Uses

func (h *HttpServer) PrepareToStop()

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

func (*HttpServer) ReadyToStop Uses

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

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

func (*HttpServer) Resume Uses

func (h *HttpServer) Resume() error

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

func (*HttpServer) SetProvidersManually Uses

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 Uses

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 Uses

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 Uses

func (h *HttpServer) Suspend() error

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

type HttpServerFacilityBuilder Uses

type HttpServerFacilityBuilder struct {
}

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

func (*HttpServerFacilityBuilder) BuildAndRegister Uses

func (hsfb *HttpServerFacilityBuilder) BuildAndRegister(lm *logging.ComponentLoggerManager, ca *config.ConfigAccessor, cn *ioc.ComponentContainer) error

See FacilityBuilder.BuildAndRegister

func (*HttpServerFacilityBuilder) DependsOnFacilities Uses

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

See FacilityBuilder.DependsOnFacilities

func (*HttpServerFacilityBuilder) FacilityName Uses

func (hsfb *HttpServerFacilityBuilder) FacilityName() string

See FacilityBuilder.FacilityName

Package httpserver imports 18 packages (graph) and is imported by 3 packages. Updated 2018-03-20. Refresh now. Tools for package owners.