Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/araujo88/teapot
A simple and lightweight asynchronous web framework in C++
https://github.com/araujo88/teapot
cpp-library cpp-network cpp-web-services web-framework webframewok webframework
Last synced: 15 days ago
JSON representation
A simple and lightweight asynchronous web framework in C++
- Host: GitHub
- URL: https://github.com/araujo88/teapot
- Owner: araujo88
- License: gpl-3.0
- Created: 2022-12-06T02:52:56.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-29T03:50:32.000Z (9 months ago)
- Last Synced: 2024-03-30T01:50:29.747Z (9 months ago)
- Topics: cpp-library, cpp-network, cpp-web-services, web-framework, webframewok, webframework
- Language: C++
- Homepage:
- Size: 1.6 MB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# teapot 🫖
[![license](https://img.shields.io/badge/license-GPL-green)](https://raw.githubusercontent.com/araujo88/teapot/main/LICENSE)
[![build](https://github.com/araujo88/teapot/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/araujo88/teapot/actions/workflows/build.yml)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:
```cpp
#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:
```cpp
#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 `.
Optionally, the following arguments can be provided for the server instance:
```cpp
#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)`:
```cpp
#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
```cpp
#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
```cpp
#include "../include/teapot.hpp"int main(int argc, char *argv[])
{
tpt::Teapot server;
server.returnHTML("/example", "Example
");
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.
```cpp
#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.