Quick Start¶
This guide shows how to build and run a simple Webserver instance.
Minimal Example¶
Create a config.toml
:
Create a main.cpp
:
#include "server/server.h"
#include "Protocols/HTTP/Message.h"
#include <iostream>
using namespace usub::server;
void handlerFunction(protocols::http::Request &request,
protocols::http::Response &response) {
std::cout << "Matched: " << request.getURL() << std::endl;
response.setStatus(200)
.setMessage("OK")
.addHeader("Content-Type", "text/plain")
.setBody("Hello World from Webserver!\n");
}
int main() {
Server server("config.toml");
// Register a simple route
server.handle({"GET"}, "/hello", handlerFunction);
// Run server loop
server.run();
}
Run¶
Compile and run:
Test¶
Send a request using curl:
Expected output:
Next Steps¶
- Learn how to work with requests/responses
- Learn how to add middleware.