Fix for adding boilerplate
This commit is contained in:
10
CMakeLists.txt
Normal file
10
CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
project(Webserver VERSION 1.0)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_BUILD_TYPE Debug)
|
||||
set(CMAKE_CXX_FLAGS "-Wall -g -fsanitize=address")
|
||||
|
||||
add_executable(Webserver "src/Entry.cpp" "src/Webserver.cpp" "src/HTTP.cpp")
|
||||
target_link_libraries(Webserver PRIVATE)
|
16
src/Entry.cpp
Normal file
16
src/Entry.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#include "main.hpp"
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
|
||||
// Entry point a.k.a "main.cpp"
|
||||
// You do not want to program here 99% of the time
|
||||
int main(int argc, char *argv[]) {
|
||||
try {
|
||||
asio::io_context context;
|
||||
Webserver server(context);
|
||||
context.run();
|
||||
} catch (std::exception &e) {
|
||||
std::cerr << e.what() << "\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
34
src/HTTP.cpp
Normal file
34
src/HTTP.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "main.hpp"
|
||||
|
||||
void HTTPrequest::start() {
|
||||
// Possible Logging here
|
||||
readData();
|
||||
}
|
||||
|
||||
void HTTPrequest::readData() {
|
||||
std::shared_ptr<HTTPrequest> self(shared_from_this());
|
||||
|
||||
//
|
||||
// Reading happens here
|
||||
//
|
||||
}
|
||||
|
||||
void HTTPrequest::processRequest(std::error_code ec, std::size_t size) {
|
||||
//
|
||||
// This is where we will process requests
|
||||
//
|
||||
}
|
||||
|
||||
void HTTPrequest::writeData(std::string data) {
|
||||
//
|
||||
// Response here
|
||||
//
|
||||
}
|
||||
|
||||
// ================= CLASS HANDLING SPECIFIC =================
|
||||
|
||||
HTTPrequest::HTTPrequest(asio::io_context &context) : sock(context) {}
|
||||
asio::ip::tcp::socket &HTTPrequest::socket() { return sock; }
|
||||
HTTPrequest::HTTPrequest_ptr HTTPrequest::create(asio::io_context &context) {
|
||||
return HTTPrequest_ptr(new HTTPrequest(context));
|
||||
}
|
22
src/Webserver.cpp
Normal file
22
src/Webserver.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "main.hpp"
|
||||
|
||||
// Webserver is defined at main.hpp, you probably do not want to code here
|
||||
Webserver::Webserver(asio::io_context &context)
|
||||
: io(context),
|
||||
accept(context,
|
||||
asio::ip::tcp::endpoint(asio::ip::make_address(IP), PORT)) {
|
||||
begin();
|
||||
}
|
||||
|
||||
void Webserver::begin() {
|
||||
HTTPrequest::HTTPrequest_ptr newconn = HTTPrequest::create(io);
|
||||
|
||||
accept.async_accept(newconn->socket(),
|
||||
[this, newconn](const asio::error_code &err) {
|
||||
if (!err) {
|
||||
newconn->start();
|
||||
}
|
||||
|
||||
begin();
|
||||
});
|
||||
}
|
47
src/main.hpp
Normal file
47
src/main.hpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <asio.hpp>
|
||||
#include <asio/buffer.hpp>
|
||||
#include <asio/error_code.hpp>
|
||||
#include <asio/impl/read.hpp>
|
||||
#include <asio/impl/read_until.hpp>
|
||||
#include <asio/impl/write.hpp>
|
||||
#include <asio/io_context.hpp>
|
||||
#include <asio/ip/address.hpp>
|
||||
#include <asio/ip/tcp.hpp>
|
||||
#include <asio/placeholders.hpp>
|
||||
#include <asio/streambuf.hpp>
|
||||
|
||||
#define IP "127.0.0.1"
|
||||
#define PORT 8000
|
||||
using asocket = asio::ip::tcp::socket;
|
||||
using asocket_ptr = std::shared_ptr<asocket>;
|
||||
|
||||
// We handle HTTP requests with readData, writeData, and processRequest
|
||||
class HTTPrequest : public std::enable_shared_from_this<HTTPrequest> {
|
||||
public:
|
||||
typedef std::shared_ptr<HTTPrequest> HTTPrequest_ptr;
|
||||
|
||||
asio::ip::tcp::socket &socket();
|
||||
static HTTPrequest_ptr create(asio::io_context &context);
|
||||
|
||||
void start();
|
||||
|
||||
private:
|
||||
HTTPrequest(asio::io_context &context);
|
||||
void readData();
|
||||
void processRequest(std::error_code ec, std::size_t size);
|
||||
void writeData(std::string data);
|
||||
|
||||
asio::ip::tcp::socket sock;
|
||||
asio::streambuf buffer;
|
||||
};
|
||||
|
||||
// Server manages connections/requests
|
||||
class Webserver : public std::enable_shared_from_this<Webserver> {
|
||||
public:
|
||||
Webserver(asio::io_context &context);
|
||||
|
||||
private:
|
||||
void begin();
|
||||
asio::io_context &io;
|
||||
asio::ip::tcp::acceptor accept;
|
||||
};
|
Reference in New Issue
Block a user