From ea0d3220db995018335c48eb06b9794235ff436b Mon Sep 17 00:00:00 2001 From: MetroWind Date: Sun, 7 Sep 2025 09:42:33 -0700 Subject: Initial commit, mostly just copied from shrt. --- src/app.hpp | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/app.hpp (limited to 'src/app.hpp') diff --git a/src/app.hpp b/src/app.hpp new file mode 100644 index 0000000..092196c --- /dev/null +++ b/src/app.hpp @@ -0,0 +1,84 @@ +#pragma once + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "data.hpp" +#include "config.hpp" + +class App : public mw::HTTPServer +{ +public: + using Request = mw::HTTPServer::Request; + using Response = mw::HTTPServer::Response; + + App() = delete; + App(const Configuration& conf, + std::unique_ptr data_source, + std::unique_ptr openid_auth); + + std::string urlFor(const std::string& name, const std::string& arg="") + const; + +private: + void setup() override; + + struct SessionValidation + { + enum { VALID, REFRESHED, INVALID } status; + mw::UserInfo user; + mw::Tokens new_tokens; + + static SessionValidation valid(mw::UserInfo&& user_info) + { + return {VALID, user_info, {}}; + } + + static SessionValidation refreshed(mw::UserInfo&& user_info, mw::Tokens&& tokens) + { + return {REFRESHED, user_info, tokens}; + } + + static SessionValidation invalid() + { + return {INVALID, {}, {}}; + } + }; + mw::E validateSession(const Request& req) const; + + // Query the auth module for the status of the session. If there + // is no session or it fails to query the auth module, set the + // status and body in “res” accordingly, and return nullopt. In + // this case if this function does return a value, it would never + // be an invalid session. + // + // If “allow_error_and_invalid” is true, failure to query and + // invalid session are considered ok, and no status and body would + // be set in “res”. In this case this function just returns an + // invalid session. + std::optional prepareSession( + const Request& req, Response& res, + bool allow_error_and_invalid=false) const; + + // This gives a path, optionally with the name of an argument, + // that is suitable to bind to a URL handler. For example, + // supposed the URL of the blog post with ID 1 is + // “http://some.domain/blog/p/1”. Calling “getPath("post", "id")” + // would give “/blog/p/:id”. This uses urlFor(), and therefore + // requires that the URL is mapped correctly in that function. + std::string getPath(const std::string& name, const std::string& arg_name="") + const; + + Configuration config; + mw::URL base_url; + inja::Environment templates; + std::unique_ptr data; + std::unique_ptr auth; +}; -- cgit v1.2.3-70-g09d2