TLS Server Setup
๐ TLS Server Setup
Section titled โ๐ 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.
๐งฑ Project Structure
Section titled โ๐งฑ Project Structureโtls-server/โโโ CMakeLists.txtโโโ certs/โ โโโ cert.pemโ โโโ key.pemโโโ main.cpp
๐ CMakeLists.txt
Section titled โ๐ CMakeLists.txtโcmake_minimum_required(VERSION 3.16)project(tls_server)
set(CMAKE_CXX_STANDARD 20)
include(FetchContent)
FetchContent_Declare(zunoGIT_REPOSITORY https://github.com/ZunoFramework/zuno.gitGIT_TAG main)
FetchContent_MakeAvailable(zuno)
add_executable(tls-server main.cpp)target_link_libraries(tls-server PRIVATE zuno)
๐ main.cpp
Section titled โ๐ main.cppโ#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);}
๐ Generating a Self-Signed Certificate
Section titled โ๐ Generating a Self-Signed CertificateโFor local development, you can generate a self-signed certificate using OpenSSL:
mkdir certsopenssl 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
.
๐ Run the Server
Section titled โ๐ Run the Serverโcmake -B buildcmake --build buildsudo ./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.
๐งญ Whatโs Next?
Section titled โ๐งญ Whatโs Next?โ- Add CORS and Rate Limiting
- Serve Static Files over HTTPS
Zuno makes secure-by-default development in C++ not just possibleโbut effortless.