io.pedestal.connector
added in 0.8.0
Replacement for io.pedestal.http that is not (directly) linked to the Jakarta Servlet API and is generally simpler to use.
default-connector-map
(default-connector-map port)
(default-connector-map host port)
Creates a default connector map for the given port and optional host. host defaults to “localhost” which is appropriate for local testing (accessible only from the local host), but “0.0.0.0” (accessible from any TCP/IP connection) is a better option when deployed.
start!
(start! connector)
A convienience function for starting the connector.
This may block the current thread until the connector is stopped.
Returns the connector.
with-default-interceptors
(with-default-interceptors connector-map & {:as options})
Sets up a default set of interceptors for early development of an application.
It is expected that an application under active development will not use this function, but will provide its own interceptor stack explicitly.
These interceptors provide basic secure functionality with a limited amount of configurability. Many of the underlying interceptors can be configured for greater security.
A routing interceptor should be added after all other interceptors.
Interceptors (in order):
Role | Description | Provided By |
---|---|---|
Request tracing | Make request observable via Open Telemetry | request-tracing-interceptor |
Request logging | Log incoming request method and URI | log-request |
Allowed origins (optional) | Only allow requests from specified origins | allow-origin |
Session support (optional) | Persist data between requests | session |
Body params | Parse JSON, EDN, etc. body into appropriate parameters (:edn-body, :json-body, etc.) | body-params |
Cross site request forgery | Detect forged requests | anti-forgery |
Default content type | Set response content type from request file extension, if not otherwise set | content-type |
Query parameters | Decode request :query-string to :query-params | query-params |
Secure headers | Ensures a number of security-related headers | secure-headers |
Note that application code should not depend on the exact names of the interceptors, as those may be subject to change.
Option | Notes |
---|---|
:allowed-origins | Passed to allow-origin |
:session-options | If non-nil, passed to session |
:extra-mime-types | Passed to content-type |
with-file-access
(with-file-access connector-map file-path)
Adds an interceptor exposing access to files on the file system, routed at file-path; this uses file. The URI /
maps to the contents of the directory at file-path
.
This should be called just before adding a routing interceptor.
This is an alternative to file-routes, and should only be used when file routing would conflict with other routes.
with-interceptor
(with-interceptor connector-map interceptor)
Appends to the :interceptors in the service map, or does nothing if interceptor is nil.
interceptor must be an interceptor record, or convertable to an interceptor record.
with-interceptors
(with-interceptors service-map interceptors)
Appends a sequence of interceptors using with-interceptor.
with-resource-access
(with-resource-access connector-map root-path)
Adds an interceptor exposing access to resources on the classpath system, routed at root-path; this uses file. The URI /
maps to the contents of the classpath with a prefix of root-path
.
This should be called just before adding a routing interceptor.
This is an alternative to resource-routes, and should only be used when resource routing would conflict with other routes.
with-routing
macro
(with-routing service-map router-constructor & route-fragments)
A macro for adding a routing interceptor (and an interceptor to decode path parameters) to the service map. This is generally the last step in building the interceptor chain.
This is a wrapper around the routes-from macro, which helps with developing at the REPL.
The router-constructor is a function that is passed the expanded routes and returns a routing interceptor. It may also be one of :sawtooth, :map-tree, :prefix-tree, or :linear-search (the four built-in router constructors). :sawtooth is a good default for new applications especially.
The provided route-fragments must extend the ExpandableRoutes protocol; these will either be RoutingFragments (from directly invoking a function such as table-routes) or a data structure (vector, map, or set) that can be implicitly converted to a RoutingFragment.
At least one route fragment is required.
Evalulates to the service map with two added interceptors:
- A routing interceptor
- A path-params-decoder