A Django-inspired C++ web framework for educational purposes
DjangoCPP is a lightweight, educational web framework written in modern C++23 that brings Django-like concepts to C++ development. It's designed to help developers understand web framework architecture while exploring the power of modern C++ features.
- 🎯 Django-inspired Architecture: Familiar patterns for web developers
- 🛣️ URL Routing: Clean and simple route definitions
- 🔧 Middleware System: Extensible request/response processing pipeline
- 🏗️ Modern C++23: Leverages latest C++ standards and features
- 🚀 High Performance: Compiled C++ performance for web applications
- 📦 Single Binary: No external dependencies at runtime
- 🧪 Educational Focus: Clean, readable code for learning purposes
- C++23 compatible compiler (GCC 12+ or Clang 15+)
- cmake 3.10+
- make
- httplib.h (included in the project)
# Clone the repository
git clone https://github.com/h3ssan/DjangoCPP.git
cd DjangoCPP
# Create a build directory
mkdir build && cd build
# Configure the project
cmake -DCMAKE_BUILD_TYPE=Release ..
# Build the project
make -j$(nproc)
# Run the server
./djangocpp
The server will start at http://127.0.0.1:8000
# Test the default route
curl http://127.0.0.1:8000/
# Test the whoami endpoint
curl http://127.0.0.1:8000/whoami
┌─────────────────┐ ┌──────────────┐ ┌─────────────┐
│ HTTP Server │ -> │ Application │ -> │ Router │
│ (httplib) │ │ │ │ │
└─────────────────┘ └──────────────┘ └─────────────┘
│ │
v │
┌─────────────┐ │
│ Middleware │ │
│ Pipeline │ │
└─────────────┘ │
v
┌─────────────┐
│ Handler │
│ (Lambda) │
└─────────────┘
- HTTP Request arrives at the httplib server
- Request Conversion transforms httplib request to internal format
- Application processes the request through:
- Middleware Pipeline for cross-cutting concerns
- Router for URL matching and handler dispatch
- Handler generates the response
- Response Conversion transforms internal response to httplib format
Routes are defined in routes.hpp
:
std::map<std::pair<std::string, std::string>, Handler> routes = {
{{"GET", "/"}, [](std::shared_ptr<Request> req) {
return std::make_shared<Response>(Response{
.status = 200,
.headers = {{"Content-Type", "text/plain"}},
.body = "Hello, World!"
});
}},
{{"POST", "/users"}, [](std::shared_ptr<Request> req) {
// Handle user creation
return std::make_shared<Response>(Response{
.status = 201,
.headers = {{"Content-Type", "application/json"}},
.body = R"({"message": "User created"})"
});
}}
};
Extend the Middleware
class in middleware.hpp
:
class AuthMiddleware : public Middleware {
public:
std::shared_ptr<Response> process_request(std::shared_ptr<Request> req) override {
// Check for authentication token
if (req->headers.find("Authorization") == req->headers.end()) {
return std::make_shared<Response>(Response{
.status = 401,
.headers = {{"Content-Type", "text/plain"}},
.body = "Unauthorized"
});
}
return nullptr; // Continue processing
}
};
// Register middleware
const std::vector<std::shared_ptr<Middleware>> middlewares = {
std::make_shared<LoggingMiddleware>(),
std::make_shared<AuthMiddleware>(),
};
Server settings are configured in settings.h
:
namespace Settings {
namespace Server {
const string HOST = "127.0.0.1";
const int PORT = 8000;
}
}
DjangoCPP/
├── 📄 main.cc # Application entry point
├── 🌐 http.h/http.cc # HTTP server and request handling
├── 🏗️ app.h/app.cc # Application core with middleware support
├── 🛣️ router.h/router.cc # URL routing system
├── 📝 routes.hpp # Route definitions
├── 🔧 middleware.hpp # Middleware system and implementations
├── ⚙️ settings.h # Configuration settings
├── 📚 httplib.h # HTTP library (header-only)
├── 🔨 Makefile # Build configuration
└── 📖 README.md # This file
This project demonstrates:
- Web Framework Architecture: Understanding how web frameworks work internally
- Modern C++ Features: C++23 syntax, smart pointers, lambdas, and more
- Design Patterns: Middleware pattern, factory pattern, and functional composition
- HTTP Protocol: Low-level HTTP request/response handling
- Memory Management: Safe memory handling with smart pointers
- Modular Design: Clean separation of concerns
- New Route: Add to
routes.hpp
- New Middleware: Extend
Middleware
class inmiddleware.hpp
- Configuration: Modify
settings.h
- Build: Run
make
to rebuild
- Modern C++23 features preferred
- Smart pointers for memory safety
- Functional programming patterns where appropriate
- Clear, educational code structure
This is an educational project! Contributions are welcome:
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
- C++23 Features
- Django Documentation (for architectural inspiration)
- HTTP/1.1 Specification
- Modern C++ Best Practices
- Basic routing (no URL parameters yet)
- Simple middleware system
- No database integration
- No template engine
- Educational focus over production readiness
This project is licensed under the MIT License - see the LICENSE file for details.
- httplib for the HTTP server implementation
- Django framework for architectural inspiration
- The C++ community for continuous language improvements
Note: This is an educational project designed for learning web framework concepts with modern C++. It's not intended for production use.