HttpServer
Service for serving Effect HTTP responses on a concrete HTTP server.
Platform adapters provide HttpServer, and routers or applications consume it to run an HttpServerResponse effect for each incoming request. The service exposes the listening address, while this module also includes helpers for address formatting, server logging, and clients that target the current server in tests.
Accessors
addressFormattedWith
Signature
declare function addressFormattedWith<A, E, R>(
f: (address: string) => Effect<A, E, R>,
): Effect<A, E, HttpServer | R>;serveEffect
Effect that starts serving an HTTP response effect with the current HttpServer.
Details
The request service is supplied by the server for each request; the effect requires a scope and any non-request dependencies of the response effect or middleware.
Signature
declare const serveEffect: {
(): <E, R>(
effect: Effect<HttpServerResponse, E, R>,
) => Effect<void, never, Scope | HttpServer | Exclude<R, HttpServerRequest>>;
<E, R, App extends Effect<HttpServerResponse, any, any>>(
middleware: Applied<App, E, R>,
): (
effect: Effect<HttpServerResponse, E, R>,
) => Effect<void, never, Scope | HttpServer | Exclude<Services<App>, HttpServerRequest>>;
<E, R>(
effect: Effect<HttpServerResponse, E, R>,
): Effect<void, never, Scope | HttpServer | Exclude<R, HttpServerRequest>>;
<E, R, App extends Effect<HttpServerResponse, any, any>>(
effect: Effect<HttpServerResponse, E, R>,
middleware: Applied<App, E, R>,
): Effect<void, never, Scope | HttpServer | Exclude<Services<App>, HttpServerRequest>>;
};Constructors
Constructs an HttpServer service from a serving implementation and listening address.
Signature
declare function make(options: {
readonly address: Address;
readonly serve: (
httpEffect: Effect<HttpServerResponse, unknown, Scope | HttpServerRequest>,
middleware?: HttpMiddleware,
) => Effect<void, never, Scope>;
}): {
readonly address: Address;
readonly serve: {
<E, R>(
effect: Effect<HttpServerResponse, E, R>,
): Effect<void, never, Scope | Exclude<R, HttpServerRequest>>;
<E, R, App extends Effect<HttpServerResponse, any, any>>(
effect: Effect<HttpServerResponse, E, R>,
middleware: Applied<App, E, R>,
): Effect<void, never, Scope | Exclude<R, HttpServerRequest>>;
};
};Converting
formatAddress
Formats a server address as a display string.
Details
TCP addresses are formatted as http://host:port; Unix socket addresses are formatted as unix://path.
Signature
declare function formatAddress(address: Address): string;Layers
Creates a layer that starts serving an HTTP response effect with the current HttpServer.
Details
The request service is supplied by the server for each request; the returned layer still requires the server, a scope, and any non-request dependencies of the response effect or middleware.
Signature
declare const serve: {
(): <E, R>(
effect: Effect<HttpServerResponse, E, R>,
) => Layer<never, never, HttpServer | Exclude<R, Scope | HttpServerRequest>>;
<E, R, App extends Effect<HttpServerResponse, any, any>>(
middleware: Applied<App, E, R>,
): (
effect: Effect<HttpServerResponse, E, R>,
) => Layer<never, never, HttpServer | Exclude<Services<App>, Scope | HttpServerRequest>>;
<E, R>(
effect: Effect<HttpServerResponse, E, R>,
): Layer<never, never, HttpServer | Exclude<R, Scope | HttpServerRequest>>;
<E, R, App extends Effect<HttpServerResponse, any, any>>(
effect: Effect<HttpServerResponse, E, R>,
middleware: Applied<App, E, R>,
): Layer<never, never, HttpServer | Exclude<Services<App>, Scope | HttpServerRequest>>;
};withLogAddress
Adds address logging to a layer that provides an HttpServer.
Signature
declare function withLogAddress<A, E, R>(
layer: Layer<A, E, R>,
): Layer<A, E, R | Exclude<HttpServer, A>>;Logging
logAddress
Logs the formatted address of the current HTTP server.
Signature
declare const logAddress: Effect.Effect<void, never, HttpServer>;Models
Address where an HTTP server is listening.
Details
The address is either a TCP host and port or a Unix domain socket path.
Signature
type Address = UnixAddress | TcpAddress;TcpAddress interface
TCP address for an HTTP server, identified by hostname and port.
Signature
interface TcpAddress {
readonly _tag: "TcpAddress";
readonly hostname: string;
readonly port: number;
}UnixAddress interface
Unix domain socket address for an HTTP server.
Signature
interface UnixAddress {
readonly _tag: "UnixAddress";
readonly path: string;
}Services
HttpServer
Service tag for an HTTP server runtime.
Details
The service can serve an HTTP response effect and exposes the address where the server is listening.
Signature
declare class HttpServer extends Shape<
"effect/http/HttpServer",
{
readonly address: Address;
readonly serve: {
<E, R>(
effect: Effect<HttpServerResponse, E, R>,
): Effect<void, never, Scope | Exclude<R, HttpServerRequest>>;
<E, R, App extends Effect<HttpServerResponse, any, any>>(
effect: Effect<HttpServerResponse, E, R>,
middleware: Applied<App, E, R>,
): Effect<void, never, Scope | Exclude<R, HttpServerRequest>>;
};
},
this
> {
constructor(_: never);
}Testing
layerServices
Layer that provides the platform services commonly needed by HTTP server tests.
Details
It includes HttpPlatform, Path, a weak ETag generator, and a no-op FileSystem.
Signature
declare const layerServices: Layer.Layer<
Path.Path | HttpPlatform.HttpPlatform | FileSystem.FileSystem | Etag.Generator
>;layerTestClient
Layer that provides the test HttpClient created by makeTestClient.
Signature
declare const layerTestClient: Layer.Layer<
HttpClient.HttpClient,
never,
HttpServer | HttpClient.HttpClient
>;makeTestClient
Builds an HttpClient that sends requests to the current test HTTP server.
Details
For TCP servers, requests are prefixed with the server URL and 0.0.0.0 is rewritten to 127.0.0.1.
Gotchas
Unix socket addresses are not supported.
Signature
declare const makeTestClient: Effect.Effect<
HttpClient.HttpClient,
never,
HttpServer | HttpClient.HttpClient
>;
Reads the current server address, formats it with
formatAddress, and passes the formatted address to the supplied effectful function.