WebFrame++
v3
C++ Web Framework for setting up HTTP servers
|
Compiler version | Minimum C++ standard required |
---|---|
GCC, Clang, MinGW, MSVC | -std=c++2a -std=c++20 |
Check the static code analysis of the project here.
Check the performance check of the project here.
Check the Doxygen documentation of the library here.
Make sure to include the library
#include <core/webframe.hpp>
Initiate your Web application
webframe::core::application app;
Set directories for your static files
app.set_static(relative or absolute path/directory to the static files, web alias);
Ex. the files are in ./example/Sample/static
and the route for them is /static
:
app.set_static("./example/Sample/static", "/static");
NOTE: You can list multiple static folders
NOTE: Relative paths depend on where you execute the executable from and not on where the source file is located.
Set Inja template folder
app.set_templates(relative or absolute path/directory to the INJA templates);
Ex. the Inja template files are in ./example/Sample/static/templates
:
app.set_templates("./example/Sample/static/templates");
NOTE: You can list multiple template folders
NOTE: Relative paths depend on where you execute the executable from and not on where the source file is located.
Set up error handling
Set the code of the error
Implement the responding function using lambda that takes 1 string as parameter
app.handle("404", [](const std::string& path) {
return "Error 404: " + path + " was not found.";
});
app.handle("500", [](const std::string& reason) {
return "Error 500: Internal server error: " + reason + ".";
});
Set up your routes
Set up headers and status code yourself
app.route ("/", []() {
return webframe::core::response (webframe::core::status_line ("1.1", "200"), {{"Content-Type", "text/html; charset=utf-8"}}, "<h1>Hello, World!</h1>");
});
or let WebFrame do it for you
app.route ("/", []() {
return "<h1>Hello, World!</h1>";
});
To render your Jinja templates use app.render(filename/file path, json with variables)
You can also use path/route variables
using predefined regex
app.route("/{text}", [&app](const std::string& user) {
return app.render("template.html", {{"username", user}});
});
Predefined | Raw regex equivalent |
---|---|
string | [A-Za-z_%0-9.-]+ |
text | [A-Za-z_%0-9.-]+ |
char | [A-Za-z_%0-9.-] |
symbol | [A-Za-z_%0-9.-] |
digit | [0-9] |
number | [0-9.]+ |
path | [A-Za-z_%0-9.-\/]+ |
or using your own regex
app.route("/{[a-z]+}", [&app](const std::string& user) {
return app.render("template.html", {{"username", user}});
});
Make sure to run your app (async run)
app.run(port, cores);
Stack multiple setups
You can list different app setups consequently:
app.set_static("./example/Sample/static", "/static")
.set_templates("./example/Sample/static/templates")
.handle("404", [](const std::string& path) {
return "Error 404: " + path + " was not found.";
})
.handle("500", [](const std::string& reason) {
return "Error 500: Internal server error: " + reason + ".";
})
.route ("/", []() {
return webframe::core::response (webframe::core::status_line ("1.1", "200"), {{"Content-Type", "text/html; charset=utf-8"}}, "<h1>Hello, World!</h1>");
});
Multithreading
Wait until the server starts accepting requests: (sync function)
app.wait_start(port);
or
app.run(port, cores).wait_start(port);
Wait until the server stops accepting requests: (sync function)
app.wait_end(port);
or
app.run(port, cores).wait_end(port);
Set up custom loggers
logger is the stream for standard logs from the server
error_logger is the stream for error messages
perfomancer is the stream for performance logs
app.set_logger(ostream&)
.set_error_logger(ostream&)
.set_performancer(ostream&);
Force server stop
app.request_stop(port);
Note: port should be const char*
Organize projects
Create set of routes
init_routes(home)
.route("/home", []() {
return "This is my home page";
});
or if you need to use webframe::core::application
functions
webframe::core::application app;
...
init_routes(home)
.route("/home", [&app]() {
return app.render("template.html", {{"username", user}});
});
Import the set of routes to your app
init_routes(home)
...
app.extend_with(home);
or if you want to add prefix to the set of routes
init_routes(home)
...
app.extend_with(home, "/prefix");
Check example/ for more information.