Server class
#include <src/microserve/server.cppm>
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
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
addressandport. - ~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
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:: |
Recognised values (case-sensitive):
"http3","h3","quic"→ Protocol::Http3 "https","http2","h2"→ Protocol::Http2 "h2c"→ Protocol::Http2ClearText
Any other string, including "http" / "http1", maps to Protocol::
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::
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.