Skip to content

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.

14 exports Added in v4.0.0 Source

Accessors

Reads the current server address, formats it with formatAddress, and passes the formatted address to the supplied effectful function.

Signature

declare function addressFormattedWith<A, E, R>(
  f: (address: string) => Effect<A, E, R>,
): Effect<A, E, HttpServer | R>;

serveEffect

Added in v4.0.0 Source

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

make

Added in v4.0.0 Source

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

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

serve

Added in v4.0.0 Source

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>>;
};

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

Added in v4.0.0 Source

Logs the formatted address of the current HTTP server.

Signature

declare const logAddress: Effect.Effect<void, never, HttpServer>;

Models

Address type

Added in v4.0.0 Source

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

Added in v4.0.0 Source

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

Added in v4.0.0 Source

Unix domain socket address for an HTTP server.

Signature

interface UnixAddress {
  readonly _tag: "UnixAddress";
  readonly path: string;
}

Services

HttpServer

Added in v4.0.0 Source

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

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
>;

Layer that provides the test HttpClient created by makeTestClient.

Signature

declare const layerTestClient: Layer.Layer<
  HttpClient.HttpClient,
  never,
  HttpServer | HttpClient.HttpClient
>;

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
>;