Ready. Set. Go!

This commit is contained in:
apio 2022-10-07 16:38:14 +02:00
commit 42b3ed4415
6 changed files with 104 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
build/**
.vscode/**

10
CMakeLists.txt Normal file
View File

@ -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})

79
main.cpp Normal file
View File

@ -0,0 +1,79 @@
#include "webcxx/App.h"
#include "webcxx/Request.h"
#include "webcxx/Response.h"
#include "webcxx/Map.h"
#include <sstream>
#include <stdexcept>
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("<p>Hello, webcxx world!</p>"); });
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("<p>POYO!!</p>");
}
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 ? "<p>GET</p>" : "<p>POST</p>");
});
app->on(GET, "/all_args", [](Request& req){
std::ostringstream ss;
for(const auto& arg : req.args())
{
ss << "<p>";
ss << arg.first;
ss << ":";
ss << arg.second;
ss << "</p>";
}
return html("<div>" + ss.str() + "</div>");
});
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("<p>John Doe</p>");
});
app->on_error(404, [](){
return html("<p>Sorry, page not found</p>", "Page not found");
});
return app->run(8080);
}

BIN
static/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 109 KiB

12
static/index.html Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Index</title>
</head>
<body>
<a href="https://cloud-apio.web.app/dl/linux/evo2">download evo2</a>
</body>
</html>

1
static/script.js Normal file
View File

@ -0,0 +1 @@
alert("hello world!");