microserve::Server class

Public Server facade over protocol-specific backends.

Owns a ServerImpl chosen from the requested Protocol. The destructor stops the backend if it is still running.

Public types

enum class Protocol { Http1, Http2, Http3, Http2ClearText, Http1Tls }
Transport and application protocol selected at construction.

Public static functions

static auto protocol_from_string(const std::string_view s) -> Protocol
Map a protocol name to a Protocol enumerator.

Constructors, destructors, conversion operators

Server(net::io_context& ioc, const std::string& address, unsigned short port, Protocol protocol = Protocol::Http1, std::string tls_cert = "certs/server.crt", std::string tls_key = "certs/server.key", int verbosity = 0, bool allow_h11 = true)
Construct a server bound to address and port.
~Server()
Stop the backend if it is still running and release it.

Public functions

void set_handler(Handler handler) const
Install the request handler used by all accepted sessions.
void start() const
Start accepting connections on the configured endpoint.
void stop() const
Stop accepting connections and shut down active sessions.

Enum documentation

enum class microserve::Server::Protocol

Transport and application protocol selected at construction.

Enumerators
Http1

Clear-text HTTP/1.1.

Http2

HTTP/2 over TLS (ALPN h2).

Http3

HTTP/3 over QUIC.

Http2ClearText

Clear-text HTTP/2 (h2c), optionally with HTTP/1.1.

Http1Tls

HTTP/1.1 over TLS.

Function documentation

static Protocol microserve::Server::protocol_from_string(const std::string_view s)

Map a protocol name to a Protocol enumerator.

Parameters
s Protocol name, without a trailing port or scheme separator.
Returns Matching enumerator, or Protocol::Http1 when unknown.

Recognised values (case-sensitive):

Any other string, including "http" / "http1", maps to Protocol::Http1.

microserve::Server::Server(net::io_context& ioc, const std::string& address, unsigned short port, Protocol protocol = Protocol::Http1, std::string tls_cert = "certs/server.crt", std::string tls_key = "certs/server.key", int verbosity = 0, bool allow_h11 = true)

Construct a server bound to address and port.

Parameters
ioc Asio I/O context that drives the acceptor and sessions.
address Bind address (for example "0.0.0.0" or "::").
port TCP/UDP port to listen on.
protocol Selected backend; defaults to clear-text HTTP/1.1.
tls_cert Path to the PEM server certificate.
tls_key Path to the PEM private key.
verbosity Backend log verbosity (0 is quiet).
allow_h11 When true, h2c also accepts HTTP/1.1 requests.

TLS certificate paths are used by the HTTP/2 and HTTP/3 backends. allow_h11 applies when protocol is Protocol::Http2ClearText.

void microserve::Server::set_handler(Handler handler) const

Install the request handler used by all accepted sessions.

Parameters
handler Callback invoked for each incoming request.

Must be called before start. Replacing the handler while the server is running is backend-defined.