Coder Social home page Coder Social logo

araujo88 / teapot Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 0.0 1.64 MB

A simple and lightweight asynchronous web framework in C++

License: GNU General Public License v3.0

Makefile 1.23% C++ 91.84% HTML 4.04% CSS 0.13% JavaScript 0.06% Shell 2.69%
cpp-network cpp-web-services web-framework webframework cpp-library webframewok

teapot's Introduction

teapot ๐Ÿซ–

license build

A simple and lightweight asynchronous web framework in C++ that serves static websites.

Getting started

Pre-requisites

This library uses features from C++20.

Linux

  • gcc
  • make

Windows

  • Visual Studio 2022

Compiling

make clean && make

Setting up

In main.cpp file:

#include "../include/teapot.hpp"

int main(int argc, char *argv[])
{
    tpt::Teapot server;
    server.run();
    return 0;
}

To start the server, run ./teapot. This will start the server at the default url localhost:8000.

To run in a different port:

#include "../include/teapot.hpp"

int main(int argc, char *argv[])
{
    tpt::Teapot server = tpt::Teapot(atoi(argv[1]));
    server.run();
    return 0;
}

Start the server by running ./teapot <port_number>.

Optionally, the following arguments can be provided for the server instance:

#include "../include/teapot.hpp"

int main(int argc, char *argv[])
{
    tpt::Teapot server = tpt::Teapot(ip_address, port, max_connections, logging_type, static_files_dir);
    server.run();
    return 0;
}

ip_address: the server IP address. The default is 127.0.0.1.
port: the server port. The default is 8000.
max_connections: the maximum number of simultaneous requests. The default is 10.
logging_type: the Teapot server provides three levels of logging: DEFAULT, DISABLED and VERBOSE.
static_files_dir: the relative folder path where static files are located. The default is set to static.

Serving files

You can link a HTML file to a URL by using the method serveFile(url, file_path):

#include "../include/teapot.hpp"

int main(int argc, char *argv[])
{
    tpt::Teapot server;
    server.serveFile("/example", "/example.html");
    server.run();
    return 0;
}

Returning JSON responses

#include "../include/teapot.hpp"

int main(int argc, char *argv[])
{
    tpt::Teapot server;
    server.returnJSON("/test", "{\"name\": \"john\", \"surname\": \"doe\"}");
    server.run();
    return 0;
}

Returning hard-coded HTML

#include "../include/teapot.hpp"

int main(int argc, char *argv[])
{
    tpt::Teapot server;
    server.returnHTML("/example", "<html><h1>Example</h1></html>");
    server.run();
    return 0;
}

Adding middleware

Teapot web framework provides some builtin middleware such as CORS middleware, sanitizer and security headers by default. However, they can be customized to your needs by instantiating an object and then adding it to the server instance.

#include "../include/teapot.hpp"
#include "../include/cors_middleware.hpp"

int main(int argc, char *argv[])
{
    tpt::Teapot server;
    tpt::CORSMiddleware cors_middleware = tpt::CORSMiddleware("*", "*", "*", 86400, true);
    server.addMiddleware(cors_middleware);
    server.run();
    return 0;
}

For CORS middleware, the parameters are: CORSMiddleware(allow_origins, allow_methods, allow_headers, max_age, allow_credentials).

You can create your own custom middleware by inherting from IMiddleware interface and implementing the handle method.

teapot's People

Contributors

araujo88 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.