Service

public class Service

A Service is a basic HTTP server. Each incoming request will trigger the provided handler to fire. The handler implementation is responsible for routing, parsing, or any other custom behaviour then populating the Response as necessary.

  • Initializes a new Service with the provided request handler.

    Warning

    The provided handler must call send() on the provided Response in order to return a response to the client. Failure to do so will cause the connection to hang until the client times out.

    Note

    send() is called automatically when using any of the write() convenience methods.

    Declaration

    Swift

    public init(logger: Logger? = nil, handler: @escaping RequestHandler)

    Parameters

    logger

    A Logger instance. If one is not provided, a default will be created.

    handler

    The request handler.

  • Starts the Service listening on the provided port.

    Declaration

    Swift

    public func listen(port: Int = 5000)
  • A Logger instance used throughout the Service.

    Note

    A default Logger will be created if one is not provided in init()

    See also

    swift-log

    Declaration

    Swift

    public let logger: Logger