Connector Map

Pedestal provides functions to create a connector.

The service map contains general information that any Pedestal connector will need to create a network connector. This is supplemented by a connector-specific configuration map.

Service Map Keys

An initial service map can be created by default-connector-map.

Key Type Description

:port

Number

Port for the network connector to receive connections on

:host

String

Hostname to listen on, defaults to "localhost"

:interceptors

Vector

Interceptor to used when processing requests.

:initial-context

Map

Initial context that will be used when executing the interceptor chain

:join?

Boolean

If true (the default is false) then the current thread will block when the connector is started.

The created Pedestal connector will often prefix the supplied interceptors with ones specific to the connector.

Supplying an :initial-context is a way to provide useful data, functions, or components to all interceptors and handlers.

Default interceptors

The with-default-interceptors function is used to add interceptors to the service map for much common functionality. It is passed the service map and an additional configuration map and will add interceptors for the following functionality:

  • Request tracing

  • Request logging

  • Allowed origins

  • Session support

  • Cross site request forgery detection

  • Default content type for responses

  • Parsing of query parameters

  • Secure headers (adds a few headers to improve security)

with-default-interceptors is useful, but limited. It doesn’t let you fully configure many of the interceptors it adds, and it may add interceptors you do not need. It is intended as scaffolding: useful only at the outset of a new project, but should be removed and replaced with application specific setup once development is fully underway.

If the value for ::http/secure-headers is present, it may contain keys and string values for the security headers. Any other keys will be ignored.

Files and Resources

The with-file-access and with-resource-access functions add interceptors that will expose file system or class path resources to the client; this is an alternative to exposing files and resources via routes.

Routing

The with-routing macro is used to add a router and routes to your service. It is a macro because it sets up a REPL-friendly workflow (when in development mode).

Creating the Pedestal connector

The service map is passed to a Pedestal connector factory function, along with a map of configuration specific to the Pedestal connector, and returns a PedestalConnector object, which can be started with start!.

Factory functions:

Development Mode

Additional functions in the io.pedestal.connector.dev namespace provide additional support that may be used when developing and testing locally, but should not be used in production.

Spec Validation

The service map can grow quite complex, with options controlling everything from routing, to security, to container-specific options.

The service map may be validated via Clojure spec; this involves loading the appropriate spec namespaces and using Clojure’s built-in instrumention and testing support.