2
3
4
5
6#include "core/SNodeC.h"
7#include "express/legacy/in/WebApp.h"
10#include <nlohmann/json.hpp>
21static json
snapshot(
const std::shared_ptr<express::
Request>& req,
const std::string& label) {
22 json out = json::object();
24 out[
"method"] = req->
method;
25 out[
"url"] = req->
url;
28 out[
"path"] = req->
path;
30 json params = json::object();
31 for (
const auto& [k, v] : req->
params) {
34 out[
"params"] = params;
36 json query = json::object();
37 for (
const auto& [k, v] : req->
queries) {
42 json headers = json::object();
43 headers[
"x-test"] = req->
get("X-Test");
44 out[
"headers"] = headers;
54static void tracePush(
const std::shared_ptr<express::
Request>& req,
const std::string& label) {
64 json arr = json::array();
67 for (
auto const& e : t) {
75int main(
int argc,
char* argv[]) {
78 const express::legacy::
in::WebApp app
("express-compat");
82 res->
json({{
"ok",
true}, {
"server",
"snodec"}, {
"express",
true}}
);
87 res->
json({{
"ok",
true}, {
"label",
"health"}}
);
128 if (req->
params[
"id"] ==
"0") {
137 app.get(
"/nr/:id(\\d+)", []
APPLICATION(req, res) {
144 if (req->queries[
"allow"] !=
"true") {
153 app.use(
"/guarded", guarded);
156 app.get(
"/guarded/:rest(.*)", []
APPLICATION(req, res) {
157 res->
status(403
).json({{
"label",
"guarded_fallback"}, {
"status", 403}}
);
185 app.use(
"/api", api);
190 mpMerge.get(
"/users/:id", []
APPLICATION(req, res) {
193 app.use(
"/mp/merge/t/:tenant", mpMerge);
196 mpNoMerge.get(
"/users/:id", []
APPLICATION(req, res) {
199 app.use(
"/mp/nomerge/t/:tenant", mpNoMerge);
202 auto makeScope = [](
bool merge) {
205 parent.use([merge](
auto const& req,
auto const&,
auto& next) {
206 tracePush(req, merge ?
"scopeMerge.parent" :
"scopeNoMerge.parent");
212 child.use([merge](
auto const& req,
auto const&,
auto& next) {
213 tracePush(req, merge ?
"scopeMerge.child" :
"scopeNoMerge.child");
216 child.get(
"/end", [merge](
auto const& req,
auto const& res) {
217 tracePush(req, merge ?
"scopeMerge.handler" :
"scopeNoMerge.handler");
218 res->json({{
"label", merge ?
"scope_merge" :
"scope_nomerge"}, {
"trace", traceGet(req)}});
221 parent.use(
"/b/:b", child);
225 app.use(
"/scope/nomerge/:a", makeScope(
false));
226 app.use(
"/scope/merge/:a", makeScope(
true));
238 app
.listen(8080
, [](
const express::legacy::
in::WebApp::SocketAddress& socketAddress,
const core::socket::
State& state) {
241 VLOG(1) <<
"express-compat listening on '" << socketAddress
.toString() <<
"'";
244 VLOG(1) <<
"express-compat disabled";
#define APPLICATION(req, res)
#define MIDDLEWARE(req, res, next)
static void init(int argc, char *argv[])
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
static constexpr int DISABLED
static constexpr int ERROR
static constexpr int FATAL
void operator()(const std::string &how="") const
std::map< std::string, std::string > params
const std::string & get(const std::string &key, int i=0) const
std::map< std::string, std::string > queries
Response & set(const std::string &field, const std::string &value, bool overwrite=true)
void send(const std::string &chunk)
Response & status(int status)
void json(const nlohmann::json &json)
Route & get(const Router &router) const
const Router & setMergeParams(bool mergeParams=true) const
WebAppT(const std::string &name)
std::string toString(bool expanded=true) const override
const Super & listen(uint16_t port, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
constexpr bool setAttribute(const Attribute &&attribute, const std::string &subKey="", bool overwrite=false)
bool getAttribute(const std::function< void(Attribute &)> &onFound, const std::string &subKey="") const
int main(int argc, char *argv[])
std::vector< json > Trace
static void ensureTrace(const std::shared_ptr< express::Request > &req)
static void tracePush(const std::shared_ptr< express::Request > &req, const std::string &label)
static json snapshot(const std::shared_ptr< express::Request > &req, const std::string &label)
static json traceGet(const std::shared_ptr< express::Request > &req)