Documentation ¶
Index ¶
- Constants
- func PrepareForTestHTTPD(t testingstub.T)
- func TestAPIHandlers(httpd *Daemon, t testingstub.T)
- func TestHTTPD(httpd *Daemon, t testingstub.T)
- type Daemon
- func (daemon *Daemon) GetHandlerByFactoryType(match handler.Handler) string
- func (daemon *Daemon) Initialise(stripURLPrefixFromRequest string, stripURLPrefixFromResponse string) error
- func (daemon *Daemon) StartAndBlockNoTLS(fallbackPort int) error
- func (daemon *Daemon) StartAndBlockWithTLS() error
- func (daemon *Daemon) StopNoTLS()
- func (daemon *Daemon) StopTLS()
- type HandlerCollection
Constants ¶
const ( DirectoryHandlerRateLimitFactor = 8 // DirectoryHandlerRateLimitFactor is 7 times less expensive than the most expensive handler RateLimitIntervalSec = 1 // Rate limit is calculated at 1 second interval IOTimeoutSec = 60 // IO timeout for both read and write operations // MaxRequestBodyBytes is the maximum size (in bytes) of a request body that HTTP server will process for a request. MaxRequestBodyBytes = 1024 * 1024 // EnvironmentPortNumber is the name of an environment variable, the value // of which determines the port number HTTP (no TLS) server will listen on. // As a special consideration, the variable name "PORT" is conventionally // used by a few public cloud services (e.g. AWS Elastic Beanstalk) to // inform an application which port it should listen on. EnvironmentPortNumber = "PORT" // EnvironmentIndexPage is the name of an environment variable that provides // an way of installing an HTML doc handler for an index page instead of // writing the same configuration in JSON configuration. // When the environment variable is present, the HTTP daemons will // initialise themselves with an HTML doc handler serving the environment // variable content at "/", "/index.htm", and "/index.html". // This environment variable value takes precedence over JSON configuration. EnvironmentIndexPage = "LAITOS_INDEX_PAGE" )
const (
TestLaitosIndexHTMLContent = "this is index #LAITOS_CLIENTADDR #LAITOS_3339TIME"
)
Variables ¶
This section is empty.
Functions ¶
func PrepareForTestHTTPD ¶
func PrepareForTestHTTPD(t testingstub.T)
PrepareForTestHTTPD sets up a directory and HTML file to be hosted by HTTPD during tests.
func TestAPIHandlers ¶
func TestAPIHandlers(httpd *Daemon, t testingstub.T)
Run unit tests on API handlers of an already started HTTP daemon all API handlers. Essentially, it tests "handler" package.
func TestHTTPD ¶
func TestHTTPD(httpd *Daemon, t testingstub.T)
Run unit test on HTTP daemon. See TestHTTPD_StartAndBlock for daemon setup.
Types ¶
type Daemon ¶
type Daemon struct { Address string `json:"Address"` // Network address to listen to, e.g. 0.0.0.0 for all network interfaces. Port int `json:"Port"` // Port number to listen on PlainPort int `json:"-"` // PlainPort is assigned to the port used by NoTLS listener once it starts. TLSCertPath string `json:"TLSCertPath"` // (Optional) serve HTTPS via this certificate TLSKeyPath string `json:"TLSKeyPath"` // (Optional) serve HTTPS via this certificate (key) PerIPLimit int `json:"PerIPLimit"` // PerIPLimit is approximately how many concurrent users are expected to be using the server from same IP address ServeDirectories map[string]string `json:"ServeDirectories"` // Serve directories (value) on prefix paths (key) HandlerCollection HandlerCollection `json:"-"` // Specialised handlers that implement handler.HandlerFactory interface Processor *toolbox.CommandProcessor `json:"-"` // Feature command processor // ResourcePaths is the whole collection of URLs handled by the server. ResourcePaths map[string]struct{} `json:"-"` // contains filtered or unexported fields }
Generic HTTP daemon.
func (*Daemon) GetHandlerByFactoryType ¶
Return path to Handler among special handlers that matches the specified type. Primarily used by test case code.
func (*Daemon) Initialise ¶
func (daemon *Daemon) Initialise(stripURLPrefixFromRequest string, stripURLPrefixFromResponse string) error
Initialise validates daemon configuration and initialises internal states.
stripURLPrefixFromRequest is an optional prefix string that is expected to be present in request URLs. If used, HTTP server will install all of its handlers at URL location according to the server configuration, but with the prefix URL string added to each of them. This often helps when some kind of API gateway (e.g. AWS API gateway) proxies visitors' requests and places a prefix string in each request. For example: a homepage's domain is served by a CDN, the CDN forwards visitors' requests to a backend ("origin") and in doing so automatically adds a URL prefix "/stageLive" because the backend expects such prefix. In this case, the stripURLPrefixFromRequest shall be "/stageLive".
stripURLPrefixFromResponse is an optional prefix string that will be stirpped from rendered HTML pages, such as links on pages and form action URLs, this is usually used in conjunction with stripURLPrefixFromRequest.
func (*Daemon) StartAndBlockNoTLS ¶
StartAndBlockNoTLS starts HTTP daemon and serve unencrypted connections. Blocks caller until StopNoTLS function is called. You may call this function only after having called Initialise()!
func (*Daemon) StartAndBlockWithTLS ¶
StartAndBlockWithTLS starts HTTP daemon and serve encrypted connections. Blocks caller until StopTLS function is called. You may call this function only after having called Initialise()!
type HandlerCollection ¶
HandlerCollection is a mapping between URL and implementation of handlers. It does not contain directory handlers.
func (HandlerCollection) SelfTest ¶
func (col HandlerCollection) SelfTest() error
SelfTest invokes self test function on all special handlers and report back if any error is encountered.