commit 42b3ed44150c1f81a82553aab237efa774d2eb43 Author: apio Date: Fri Oct 7 16:38:14 2022 +0200 Ready. Set. Go! diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..065e739 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/** +.vscode/** diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..265747b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.8..3.22) + +project(example-webcxx-app LANGUAGES CXX) + +add_executable(main main.cpp) + +include(webcxx/CMakeLists.txt) + +target_include_directories(main PUBLIC ${webcxx_include}) +target_link_libraries(main PUBLIC ${webcxx_libs}) \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..14de64a --- /dev/null +++ b/main.cpp @@ -0,0 +1,79 @@ +#include "webcxx/App.h" +#include "webcxx/Request.h" +#include "webcxx/Response.h" +#include "webcxx/Map.h" +#include +#include + +using namespace webcxx; + +int main() { + auto app = App::create(); + + app->set_static_resource_path("/public"); + + app->set_favicon("static/favicon.ico"); + + app->on(GET, "/", + [](Request &req) { return html("

Hello, webcxx world!

"); }); + + app->on(GET, "/index.html", + [](Request& req) { return file("static/index.html"); }); + + app->on(GET, "/teapot", [](Request &req) { return error(418); }); + + app->on(GET, "/google", + [](Request &req) { return redirect("https://www.google.com"); }); + + app->on(GET, "/args", [](Request &req) { + auto args = req.args(); + if (args.contains("kirby") && args["kirby"] == "poyo") { + return html("

POYO!!

"); + } + return error(418); + }); + + app->on(GET, "/user-agent", [](Request &req) { + auto headers = req.headers(); + if (!headers.contains("User-Agent")) + return error(400); + return html(headers["User-Agent"], "Your user agent is..."); + }); + + app->on(POST, "/post", [](Request &req) { return html(req.content()); }); + + app->on(GET | POST, "/multiple", [](Request &req) { + return html(req.method() == GET ? "

GET

" : "

POST

"); + }); + + app->on(GET, "/all_args", [](Request& req){ + std::ostringstream ss; + for(const auto& arg : req.args()) + { + ss << "

"; + ss << arg.first; + ss << ":"; + ss << arg.second; + ss << "

"; + } + return html("
" + ss.str() + "
"); + }); + + app->on(GET, "/fail", [](Request& req) { + throw std::runtime_error("runtime error"); + return error(404); + }); + + app->on(GET, "/user", [](Request& req) { + auto args = req.args(); + if(!args.contains("username")) return error(400); + if(args["username"] != "john_doe") return error(404); + return html("

John Doe

"); + }); + + app->on_error(404, [](){ + return html("

Sorry, page not found

", "Page not found"); + }); + + return app->run(8080); +} \ No newline at end of file diff --git a/static/favicon.ico b/static/favicon.ico new file mode 100644 index 0000000..351efa2 Binary files /dev/null and b/static/favicon.ico differ diff --git a/static/index.html b/static/index.html new file mode 100644 index 0000000..523def7 --- /dev/null +++ b/static/index.html @@ -0,0 +1,12 @@ + + + + + + + Index + + + download evo2 + + \ No newline at end of file diff --git a/static/script.js b/static/script.js new file mode 100644 index 0000000..acbc445 --- /dev/null +++ b/static/script.js @@ -0,0 +1 @@ +alert("hello world!"); \ No newline at end of file