Skip to content

Entity069/cpp-webframework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CPP-Webframework

A very very minimal micro web framework written in C++ (not usable in production, obv).

Introduction and Motivation

It is a very minimalistic micro-web framework similar to flask but in entirely in C++. It uses fork() to make child process to handle concurrent connections.

Motivation: Because it can be made.

File Structure

cpp-webframework/
├─ LICENSE
├─ README.md
├─ TODO
├─ include/
│  ├─ constants.h
│  ├─ encode.h
│  ├─ inja/
│  │  └─ inja.hpp
│  ├─ logger.h
│  ├─ middleware.h
│  ├─ nlohmann/
│  │  ├─ json.hpp
│  │  └─ json_fwd.hpp
│  ├─ request.h
│  ├─ response.h
│  ├─ router.h
│  └─ server.h
├─ main.cpp
└─ src/
   ├─ logger.cpp
   ├─ middleware.cpp
   ├─ request.cpp
   ├─ response.cpp
   ├─ router.cpp
   └─ server.cpp

Dependencies

It currently has mainly two dependencies:

Thank you arthurafarias for his wonderful gist which helped in encoding/decoding URI components.

Usage

Make a server object and define your routes in main.cpp like this:

Server server;

// Basic route
server.route("GET", "/hello", [](Request& req, Response& res) {
    res.setContentType(MIME_JSON);
    json response = {
        {"message", "Hello, World!"},
        {"status", "success"}
    };
    res.json(response);
});

Accessing https://localhost:8080/hello will give :

{"message":"Hello, World!","status":"success"}

Then compile:

g++ main.cpp src/*.cpp -o web

Then run the output binary:

./web

Currently this only supports static file serving and returning data via JSON (which makes it somewhat usable for making very basic APIs). It also supports serving html pages with contextual data with inja.

TODO

  • Add redirect functionality
  • Add support for file upload
  • Add support for authentication methods (currently the authentication middleware doesn't work as expected)
  • Choose a nice name for the repo

License

The code is released under MIT License. See LICENSE.

About

A very minimal microweb framework made in C++.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages