Skip to content

API Reference

The core library (io.github.cgardev:connect-kotlin-server) is framework-agnostic and contains zero Spring or Servlet API dependencies.

Package: io.github.cgardev.library.connect

The primary entry point. Hosts BindableServices on an in-process gRPC channel and serves them over Connect, Connect-streaming, and gRPC-Web via an embedded Netty HTTP server.

ConnectServer(
services: List<BindableService>,
interceptors: List<ServerInterceptor> = emptyList(),
config: ConnectServerConfig = ConnectServerConfig(),
)
  • services — A list of gRPC services (any BindableService). Required.
  • interceptors — Optional list of gRPC ServerInterceptors to apply to the in-process channel, exactly as a traditional gRPC server would. Defaults to empty.
  • config — A ConnectServerConfig with server parameters. Defaults to a config with sensible defaults (port 8080, CORS enabled).
  • config: ConnectServerConfig — The configuration object passed at construction; readable for inspection.
  • registry: ConnectMethodRegistry — The internal method registry built from the services at construction; contains introspection about the available methods.
  • boundPort: Int — The actual port the Netty server is listening on. Valid only after start() completes; resolves ephemeral port 0 to its real port.
  • start(): Unit — Starts the in-process gRPC channel and binds the Netty HTTP server. Must be called before serving requests.
  • close(): Unit — Shuts down the Netty server gracefully (respecting shutdownGraceMillis) and closes the gRPC channel. Implements AutoCloseable; safe to call multiple times.

Package: io.github.cgardev.library.connect.config

Plain Kotlin data class holding server configuration. No framework annotations — designed to be mapped onto from any configuration source.

data class ConnectServerConfig(
val host: String = "0.0.0.0",
val port: Int = 8080,
val basePath: String = "/",
val requireProtocolVersion: Boolean = false,
val getEnabled: Boolean = true,
val compressMinBytes: Int = 1024,
val readMaxBytes: Long = 4L * 1024 * 1024,
val shutdownGraceMillis: Long = 5_000,
val cors: Cors = Cors(),
)
  • host — Address the Netty server binds to. Default: "0.0.0.0".
  • port — Port to bind to; 0 selects an ephemeral port. Default: 8080.
  • basePath — URL path prefix under which RPCs are served. Full RPC paths are <basePath><package>.<Service>/<Method>; the default root / keeps paths equal to the gRPC full method name. Default: "/".
  • requireProtocolVersion — If true, Connect unary requests must carry the Connect-Protocol-Version: 1 header. Default: false.
  • getEnabled — Allow idempotent (NO_SIDE_EFFECTS) unary methods to be invoked via HTTP GET. Default: true.
  • compressMinBytes — Only compress responses whose serialized size reaches this threshold (in bytes). Default: 1024.
  • readMaxBytes — Maximum accepted (decompressed) request size, in bytes. Default: 4 MiB.
  • shutdownGraceMillis — Grace period awaited when shutting down the in-process gRPC server/channel. Default: 5000.
  • cors — A nested Cors object controlling CORS behavior.
data class Cors(
val enabled: Boolean = true,
val allowedOrigins: List<String> = listOf("*"),
val allowCredentials: Boolean = true,
val allowPrivateNetwork: Boolean = true,
val maxAgeSeconds: Long = 4 * 60 * 60,
)
  • enabled — Master switch for CORS. Default: true.
  • allowedOrigins — List of allowed origins. "*" echoes the request origin (required when credentials are allowed). Default: listOf("*").
  • allowCredentials — Allow credentials in CORS requests. Default: true.
  • allowPrivateNetwork — Allow requests from private IP ranges. Default: true.
  • maxAgeSeconds — Max age for preflight response caching. Default: 14400 (4 hours).

Package: io.github.cgardev.library.connect.error

Enum of the canonical Connect error codes. Each code carries three on-the-wire representations: a numeric value (gRPC-Web trailer), a lowercase snake_case name (Connect unary JSON and Connect end-of-stream), and the HTTP status used for Connect unary error responses.

enum class ConnectCode(
val number: Int,
val wireName: String,
val httpStatus: Int,
)
CodeNumberWire nameHTTP status
CANCELED1canceled499
UNKNOWN2unknown500
INVALID_ARGUMENT3invalid_argument400
DEADLINE_EXCEEDED4deadline_exceeded504
NOT_FOUND5not_found404
ALREADY_EXISTS6already_exists409
PERMISSION_DENIED7permission_denied403
RESOURCE_EXHAUSTED8resource_exhausted429
FAILED_PRECONDITION9failed_precondition400
ABORTED10aborted409
OUT_OF_RANGE11out_of_range400
UNIMPLEMENTED12unimplemented501
INTERNAL13internal500
UNAVAILABLE14unavailable503
DATA_LOSS15data_loss500
UNAUTHENTICATED16unauthenticated401
  • number: Int — The numeric code (used in gRPC-Web trailers).
  • wireName: String — The lowercase snake_case name (used in Connect JSON and end-of-stream).
  • httpStatus: Int — The HTTP status code for Connect unary error responses.
  • grpcName: String — Alias for the enum name; the uppercase form used in gRPC status names and Connect end-of-stream messages.
  • fromNumber(number: Int): ConnectCode — Resolve a code from its numeric value; defaults to UNKNOWN if not found.
  • fromWireName(wireName: String): ConnectCode — Resolve a code from its lowercase wire name; defaults to UNKNOWN if not found.
  • fromGrpc(code: Status.Code): ConnectCode — Map a gRPC status code to the equivalent Connect code. OK resolves to UNKNOWN (it has no error representation in Connect).

Package: io.github.cgardev.library.connect.error

The server-side error type. Handlers and the dispatcher throw this to produce a Connect error response (unary JSON body, Connect end-of-stream message, or gRPC-Web trailer frame depending on the protocol).

class ConnectException(
val code: ConnectCode,
override val message: String = "",
val details: List<ConnectErrorDetail> = emptyList(),
cause: Throwable? = null,
) : RuntimeException(message, cause)
  • code — The Connect error code. Required.
  • message — Human-readable error message. Default: empty string.
  • details — List of structured error details (protobuf Any messages). Default: empty list.
  • cause — Optional wrapped exception (for debugging). Default: null.

Package: io.github.cgardev.library.connect.error

A single structured error detail, carrying an arbitrary protobuf message serialized as a google.protobuf.Any.

data class ConnectErrorDetail(val any: ProtoAny)

Serialized to the details array of the Connect error JSON as { "type": ..., "value": <base64> }.

The Spring integration modules are optional. Both re-export the core types and add Spring-specific wiring.

Package: io.github.cgardev.library.connect.spring

Binds the spring-free ConnectServer to Spring’s SmartLifecycle, so the server starts and stops with the application context.

class ConnectServerLifecycle(private val connectServer: ConnectServer) : SmartLifecycle
  • Starts on context startupisAutoStartup() returns true.
  • Runs in a late phasegetPhase() returns Int.MAX_VALUE, ensuring the server starts last (after all other beans are ready) and stops first (before other beans shut down).
  • Graceful shutdown — Delegates to connectServer.close(), which respects the configured grace period.
  • Thread-safe — Uses AtomicBoolean to guard against concurrent start/stop calls.

Package: io.github.cgardev.library.connect.spring

Spring Boot @ConfigurationProperties bound to the connect.server.* namespace. Kept separate from the core’s plain ConnectServerConfig so the library stays free of Spring annotations.

@ConfigurationProperties(prefix = "connect.server")
data class ConnectServerProperties(
val enabled: Boolean = true,
val host: String = "0.0.0.0",
val port: Int = 8080,
val basePath: String = "/",
val requireProtocolVersion: Boolean = false,
val getEnabled: Boolean = true,
val compressMinBytes: Int = 1024,
val readMaxBytes: Long = 4L * 1024 * 1024,
val shutdownGraceMillis: Long = 5_000,
val cors: Cors = Cors(),
)
  • enabled — Master switch for the auto-configuration. Default: true. Set to false to disable the auto-configuration entirely.
  • host, port, basePath, requireProtocolVersion, getEnabled, compressMinBytes, readMaxBytes, shutdownGraceMillis — Mirror the core ConnectServerConfig properties; see above.
  • cors — Nested Cors object with the same structure as the core type.
  • toConfig(): ConnectServerConfig — Maps these Spring properties onto the core’s plain configuration type. Used internally by the auto-configuration.

Package: io.github.cgardev.library.connect.spring.autoconfigure

The Spring Boot auto-configuration starter. Declares the ConnectServer and ConnectServerLifecycle beans, discovering all BindableService and ServerInterceptor beans in the context.

@AutoConfiguration
@ConditionalOnClass(BindableService::class)
@ConditionalOnProperty(prefix = "connect.server", name = ["enabled"], havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(ConnectServerProperties::class)
class ConnectServerAutoConfiguration
  • connectServer(services, interceptors, properties): ConnectServer — Declared @ConditionalOnMissingBean. Collects all BindableService beans (in order), all ServerInterceptor beans (in order), and builds a ConnectServer from the bound ConnectServerProperties. Uses Spring’s ObjectProvider for lazy, optional discovery.

  • connectServerLifecycle(connectServer): ConnectServerLifecycle — Declared @ConditionalOnMissingBean. Wraps the ConnectServer in a lifecycle bean for automatic startup/shutdown.

  • Active only if BindableService is on the classpath.
  • Active only if the property connect.server.enabled is true (or absent). Set to false to disable.
  • ConnectServerProperties are bound and validated.

[!NOTE] Zero wiring required. Simply declare your BindableService beans as @Component or @Bean, and the auto-configuration discovers and serves them automatically.