diff options
author | MetroWind <chris.corsair@gmail.com> | 2025-09-07 09:42:33 -0700 |
---|---|---|
committer | MetroWind <chris.corsair@gmail.com> | 2025-09-07 09:42:33 -0700 |
commit | ea0d3220db995018335c48eb06b9794235ff436b (patch) | |
tree | 892564cdd4946c6ee9c1051bc31ff5c7bba6ddf1 /src/app.hpp |
Initial commit, mostly just copied from shrt.
Diffstat (limited to 'src/app.hpp')
-rw-r--r-- | src/app.hpp | 84 |
1 files changed, 84 insertions, 0 deletions
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 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include <memory> | ||
4 | #include <optional> | ||
5 | #include <string> | ||
6 | |||
7 | #include <inja.hpp> | ||
8 | #include <mw/url.hpp> | ||
9 | #include <mw/http_server.hpp> | ||
10 | #include <mw/error.hpp> | ||
11 | #include <mw/auth.hpp> | ||
12 | |||
13 | #include "data.hpp" | ||
14 | #include "config.hpp" | ||
15 | |||
16 | class App : public mw::HTTPServer | ||
17 | { | ||
18 | public: | ||
19 | using Request = mw::HTTPServer::Request; | ||
20 | using Response = mw::HTTPServer::Response; | ||
21 | |||
22 | App() = delete; | ||
23 | App(const Configuration& conf, | ||
24 | std::unique_ptr<DataSourceInterface> data_source, | ||
25 | std::unique_ptr<mw::AuthInterface> openid_auth); | ||
26 | |||
27 | std::string urlFor(const std::string& name, const std::string& arg="") | ||
28 | const; | ||
29 | |||
30 | private: | ||
31 | void setup() override; | ||
32 | |||
33 | struct SessionValidation | ||
34 | { | ||
35 | enum { VALID, REFRESHED, INVALID } status; | ||
36 | mw::UserInfo user; | ||
37 | mw::Tokens new_tokens; | ||
38 | |||
39 | static SessionValidation valid(mw::UserInfo&& user_info) | ||
40 | { | ||
41 | return {VALID, user_info, {}}; | ||
42 | } | ||
43 | |||
44 | static SessionValidation refreshed(mw::UserInfo&& user_info, mw::Tokens&& tokens) | ||
45 | { | ||
46 | return {REFRESHED, user_info, tokens}; | ||
47 | } | ||
48 | |||
49 | static SessionValidation invalid() | ||
50 | { | ||
51 | return {INVALID, {}, {}}; | ||
52 | } | ||
53 | }; | ||
54 | mw::E<SessionValidation> validateSession(const Request& req) const; | ||
55 | |||
56 | // Query the auth module for the status of the session. If there | ||
57 | // is no session or it fails to query the auth module, set the | ||
58 | // status and body in “res” accordingly, and return nullopt. In | ||
59 | // this case if this function does return a value, it would never | ||
60 | // be an invalid session. | ||
61 | // | ||
62 | // If “allow_error_and_invalid” is true, failure to query and | ||
63 | // invalid session are considered ok, and no status and body would | ||
64 | // be set in “res”. In this case this function just returns an | ||
65 | // invalid session. | ||
66 | std::optional<SessionValidation> prepareSession( | ||
67 | const Request& req, Response& res, | ||
68 | bool allow_error_and_invalid=false) const; | ||
69 | |||
70 | // This gives a path, optionally with the name of an argument, | ||
71 | // that is suitable to bind to a URL handler. For example, | ||
72 | // supposed the URL of the blog post with ID 1 is | ||
73 | // “http://some.domain/blog/p/1”. Calling “getPath("post", "id")” | ||
74 | // would give “/blog/p/:id”. This uses urlFor(), and therefore | ||
75 | // requires that the URL is mapped correctly in that function. | ||
76 | std::string getPath(const std::string& name, const std::string& arg_name="") | ||
77 | const; | ||
78 | |||
79 | Configuration config; | ||
80 | mw::URL base_url; | ||
81 | inja::Environment templates; | ||
82 | std::unique_ptr<DataSourceInterface> data; | ||
83 | std::unique_ptr<mw::AuthInterface> auth; | ||
84 | }; | ||