Skip to content

TLS Server Setup

In this example, youโ€™ll learn how to configure a secure HTTPS server using Zunoโ€™s built-in TLS support. This is essential for protecting sensitive data and enabling secure communication between clients and your API.


tls-server/
โ”œโ”€โ”€ CMakeLists.txt
โ”œโ”€โ”€ certs/
โ”‚ โ”œโ”€โ”€ cert.pem
โ”‚ โ””โ”€โ”€ key.pem
โ””โ”€โ”€ main.cpp

cmake_minimum_required(VERSION 3.16)
project(tls_server)
set(CMAKE_CXX_STANDARD 20)
include(FetchContent)
FetchContent_Declare(
zuno
GIT_REPOSITORY https://github.com/ZunoFramework/zuno.git
GIT_TAG main
)
FetchContent_MakeAvailable(zuno)
add_executable(tls-server main.cpp)
target_link_libraries(tls-server PRIVATE zuno)

#include <zuno/zuno.hpp>
int main() {
zuno::App app;
app.useTLS({
"certs/cert.pem",
"certs/key.pem"
});
app.get("/", [](auto& req, auto& res) {
res.send("Secure connection established via TLS.");
});
app.listen(443);
}

For local development, you can generate a self-signed certificate using OpenSSL:

Terminal window
mkdir certs
openssl req -x509 -newkey rsa:4096 -keyout certs/key.pem -out certs/cert.pem -days 365 -nodes

Use dummy values for the prompts, or automate with -subj.


Terminal window
cmake -B build
cmake --build build
sudo ./build/tls-server

โš ๏ธ Port 443 requires elevated privileges on most systems.

Then visit https://localhost in your browser. You may need to bypass a security warning if using a self-signed cert.



Zuno makes secure-by-default development in C++ not just possibleโ€”but effortless.