Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

WebFrame/Core

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WebFrame++ C++ Mocha
Compile & Build Benchmark & Deploy Code Quality
Issues Forks Stars License

Make your web application faster now!


Requirements

Compiler version Minimum C++ standard required
GCC, Clang, MSVC -std=c++2a or -std=c++20

Testing

Cppcheck - static code analysis

Check the static code analysis of the project here.

Performance

Check the performance check of the project here.

Code documentation

Check the Doxygen documentation of the library here.

How to use

  1. Make sure to include the library

    #include <core/core.hpp>
  2. Initiate your Web application

    webframe::core::application app;
  3. 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.

  4. 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.

  5. 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 + ".";
      });
  6. 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}});
        });
  7. Make sure to run your app (async run)

    app.run(port, cores);

Advanced usage

  1. 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>");
        });
  2. 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);
  3. 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&);		
  4. Force server stop

    app.request_stop(port);

    Note: port should be const char*

  5. 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.

Additional info

  • Currently working on setting up CMake

Socials

LinkedIn