OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
RouteController.cpp
Go to the documentation of this file.
1#include "RouteController.h"
2#include <ETag.h>
3
4
5namespace Odb::Lib::App
6{
8 : m_serverApp(serverApp)
9 {
10 }
11
13 {
14 m_serverApp.crow_app().route_dynamic(std::move(route))
15 ([/*&,*/ handler](const crow::request& req)
16 {
18 //auto authResp = m_serverApp.request_auth().AuthenticateRequest(req);
19 //if (authResp.code != crow::status::OK)
20 //{
21 // return authResp;
22 //}
23
24 return handler(req);
25 });
26
27 //app.route<crow::black_magick::get_parameter_tag(url)>(url)
28 // or
29 //app.route_dynamic(url)
30
31 //m_serverApp.crow_app().route_dynamic(const_cast<std::string&&>(route)).methods("GET"_method, "POST"_method)([&](const crow::request& req)
32 // {
33 // return handler(req);
34 // })
35
37 //CROW_ROUTE(m_pServerApp->crow_app(), "/steps/edadata")
38 // ([&](const crow::request& req)
39 // {
40 // return handler(req);
41 // });
42 }
43
45 {
46 auto unloadedDesignNames = m_serverApp.designs().getUnloadedDesignNames();
47 auto loadedFileArchiveNames = m_serverApp.designs().getLoadedFileArchiveNames();
48 auto loadedDesignNames = m_serverApp.designs().getLoadedDesignNames();
49
50 crow::json::wvalue::list designs;
51 for (const auto& designName : unloadedDesignNames)
52 {
53 auto loaded = false;
54 auto isDesign = false;
55 if (std::find(loadedFileArchiveNames.begin(), loadedFileArchiveNames.end(), designName) != loadedFileArchiveNames.end())
56 {
57 loaded = true;
58 isDesign = false;
59 }
60 else if (std::find(loadedDesignNames.begin(), loadedDesignNames.end(), designName) != loadedDesignNames.end())
61 {
62 loaded = true;
63 isDesign = true;
64 }
65 crow::json::wvalue design;
66 design["name"] = designName;
67 design["loaded"] = loaded;
68 design["type"] = isDesign? "Design" : "FileArchive";
69 designs.push_back(design);
70 }
71 crow::json::wvalue jsonResponse;
72 jsonResponse["filearchives"] = std::move(designs);
73
74#if defined(_DEBUG)
75 auto j = jsonResponse.dump();
76#endif
77
78 // determine type of success code
79 auto httpCode = crow::status::OK;
80 if (created)
81 {
82 httpCode = crow::status::CREATED;
83 }
84 else if (unloadedDesignNames.empty())
85 {
86 httpCode = crow::status::NO_CONTENT;
87 }
88
89 return crow::response(httpCode , jsonResponse);
90 }
91
92 std::optional<crow::response> RouteController::checkConditionalGet(const crow::request& req,
93 const std::string& designName,
94 const std::string& endpointPath,
95 std::string& etag) const
96 {
97 // Computed from the archive file on disk plus the design's cache
98 // invalidation generation, not the (possibly not yet loaded) cached
99 // object, so the 304 decision never pays for a load or a serialization.
100 // The generation covers in-memory-only changes (POST /filemodels with
101 // save=false replaces the cache without touching the file): those bump
102 // the generation, rotating the tag, so revalidation serves a fresh 200
103 // instead of 304-forever. A stat-to-load race (archive replaced in
104 // between) is benign: the 200 body then carries the pre-load tag, the
105 // next request recomputes the post-replacement tag, and the client's
106 // stale copy is revalidated and refreshed.
107 etag = Utils::MakeDesignEtag(m_serverApp.args().designsDir(), designName, endpointPath,
109 if (etag.empty())
110 {
111 return std::nullopt;
112 }
113
114 const auto& ifNoneMatch = req.get_header_value("If-None-Match");
115 if (!ifNoneMatch.empty() && Utils::IfNoneMatchMatches(ifNoneMatch, etag))
116 {
117 // 304 short-circuits the handler: no archive load, no to_json().
118 // ETag + Cache-Control are echoed per RFC 7232 so the client can
119 // refresh its cached header set.
120 crow::response notModified(crow::status::NOT_MODIFIED);
121 notModified.set_header("ETag", etag);
122 notModified.set_header("Cache-Control", CACHE_CONTROL_DATA_VALUE);
123 return notModified;
124 }
125
126 return std::nullopt;
127 }
128
129 crow::response RouteController::withCacheHeaders(crow::response response, const std::string& etag) const
130 {
131 // private: design payloads are user-scoped data and must not be served
132 // from shared/proxy caches; max-age allows short-term client reuse
133 // between revalidations.
134 if (!etag.empty())
135 {
136 response.set_header("ETag", etag);
137 response.set_header("Cache-Control", CACHE_CONTROL_DATA_VALUE);
138 }
139 return response;
140 }
141}
std::uint64_t GetResponseGeneration(const std::string &designName) const
virtual const OdbDesignArgs & args() const =0
virtual DesignCache & designs()=0
virtual CrowApp & crow_app()=0
std::string designsDir() const
std::optional< crow::response > checkConditionalGet(const crow::request &req, const std::string &designName, const std::string &endpointPath, std::string &etag) const
RouteController(IOdbServerApp &serverApp)
crow::response withCacheHeaders(crow::response response, const std::string &etag) const
void register_route_handler(std::string route, TRouteHandlerFunction handler)
std::function< crow::response(const crow::request &req)> TRouteHandlerFunction
static constexpr const char * CACHE_CONTROL_DATA_VALUE
crow::response makeLoadedFileModelsResponse(bool) const
bool IfNoneMatchMatches(const std::string &header, const std::string &etag)
Definition ETag.h:152
std::string MakeDesignEtag(const std::string &designsDir, const std::string &designName, const std::string &endpointPath, std::uint64_t generation=0)
Definition ETag.h:120