main
Brett 2023-08-16 20:15:34 -04:00
parent 1a456c5d73
commit 793ee5ecbe
8 changed files with 57 additions and 39 deletions

View File

@ -8,6 +8,7 @@ option(ENABLE_TSAN "Enable the thread data race sanitizer" OFF)
set(CMAKE_CXX_STANDARD 20)
set(CROW_FEATURES compression)
cmake_policy(SET CMP0057 NEW)
find_package(Crow)
find_package(CURL)
find_package(OpenSSL)

View File

@ -1,21 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/static/css/home.css">
<link rel="stylesheet" href="/static/css/bar.css">
<title>{{$SITE_TITLE}}</title>
</head>
<body>
<div class="center">
<div class="body">
<div class="titlebar">
<button class="bar"><a href="/home.html">home</a></button>
<button class="bar"><a href="/projects.html">projects</a></button>
</div>
<div class="center">
HAXsdsad
</div>
</div>
</div>
</body>
</html>

View File

@ -1,16 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/static/css/home.css">
<link rel="stylesheet" href="/static/css/bar.css">
<title>{{$SITE_TITLE}}</title>
</head>
<body>
<p>Hello {{person}}!</p>
<form action="/req/posta.html" method="posts">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Submit">
</form>
<div class="center">
<div class="body">
<div class="titlebar">
<button class="bar"><a href="/home.html">home</a></button>
<button class="bar"><a href="/projects.html">projects</a></button>
</div>
<div class="center">
HAXsdsad
</div>
</div>
</div>
</body>
</html>

View File

@ -5,9 +5,11 @@
#ifndef CROWSITE_AUTH_H
#define CROWSITE_AUTH_H
#include "crowsite/utility.h"
namespace cs {
void handleLoginPost(cs::parser::Post postData);
}

View File

@ -8,6 +8,7 @@
#include <string>
#include <crowsite/config.h>
#include <filesystem>
#include <blt/std/hashmap.h>
namespace cs {
@ -18,6 +19,7 @@ namespace cs {
public:
explicit Post(const std::string& input);
bool hasKey(const std::string& key);
const std::string& operator[](const std::string& key);
std::string dump();

View File

@ -30,8 +30,8 @@ namespace cs::jellyfin
for (const auto& user : json)
{
auto username = user["Name"].s();
auto userid = user["Id"].s();
auto username = std::string(user["Name"].s());
auto userid = std::string(user["Id"].s());
//BLT_TRACE("Processing %s = %s", username.operator std::string().c_str(), userid.operator std::string().c_str());
GLOBALS.user_ids[username] = userid;
}

View File

@ -32,6 +32,11 @@ namespace cs {
}
return out;
}
bool Post::hasKey(const std::string& key)
{
return m_Values.find(key) != m_Values.end();
}
}
namespace fs {

View File

@ -10,6 +10,7 @@
#include <crowsite/requests/jellyfin.h>
#include <crowsite/requests/curl.h>
#include <blt/parse/argparse.h>
#include <crowsite/site/auth.h>
class BLT_CrowLogger : public crow::ILogHandler
{
@ -17,7 +18,8 @@ class BLT_CrowLogger : public crow::ILogHandler
void log(std::string message, crow::LogLevel crow_level) final
{
blt::logging::log_level blt_level;
switch (crow_level){
switch (crow_level)
{
case crow::LogLevel::DEBUG:
blt_level = blt::logging::log_level::DEBUG;
break;
@ -47,6 +49,7 @@ int main(int argc, const char** argv)
cs::requests::init();
blt::arg_parse parser;
parser.addArgument(blt::arg_builder("--tests").setAction(blt::arg_action_t::STORE_TRUE).build());
parser.addArgument(blt::arg_builder("token").build());
parser.addArgument(blt::arg_builder("user").build());
parser.addArgument(blt::arg_builder("pass").build());
@ -64,7 +67,7 @@ int main(int argc, const char** argv)
BLT_INFO("Init Crow with compression and logging enabled!");
crow::SimpleApp app;
app.use_compression(crow::compression::GZIP);
app.loglevel(crow::LogLevel::INFO);
app.loglevel(crow::LogLevel::WARNING);
BLT_INFO("Creating static context");
@ -103,13 +106,19 @@ int main(int argc, const char** argv)
// for (const auto& v : req.url_params.keys())
// BLT_TRACE("URL: %s = %s", v.c_str(), req.url_params.get(v));
if (name.ends_with(".html"))
return {engine.fetch(name)};
{
crow::mustache::context ctx;
// we don't want to pass all get parameters to the context to prevent leaking
auto referer = req.url_params.get("referer");
if (referer)
ctx["referer"] = referer;
auto page = crow::mustache::compile(engine.fetch(name));
return page.render(ctx);
}
crow::mustache::context ctx({{"person", name}});
auto user_page = crow::mustache::compile(engine.fetch("index.html"));
//BLT_TRACE(page);
return user_page.render(ctx);
}
);
@ -118,13 +127,15 @@ int main(int argc, const char** argv)
[](const crow::request& req) {
cs::parser::Post pp(req.body);
return "Portabella Mushrooms! " + pp.dump();
crow::response res(303);
res.set_header("Location", pp.hasKey("referer") ? pp["referer"] : "/");
return res;
}
);
CROW_ROUTE(app, "/")(
[&engine]() {
return engine.fetch("home.html");
return engine.fetch("index.html");
}
);
@ -134,6 +145,19 @@ int main(int argc, const char** argv)
}
);
app.tick(
std::chrono::seconds(1), [&app, &args]() -> void {
if (!args.contains("tests"))
return;
static uint64_t timer = 0;
const uint64_t wait = 10;
timer++;
if (timer > wait)
{
app.stop();
}
}
);
app.port(8080).multithreaded().run();
cs::requests::cleanup();