2
3
4
5
6#include "core/SNodeC.h"
7#include "express/legacy/in/WebApp.h"
10#include <nlohmann/json.hpp>
20static json
snapshot(
const std::shared_ptr<express::
Request>& req,
const std::string& label) {
21 json out = json::object();
23 out[
"method"] = req->
method;
24 out[
"url"] = req->
url;
27 out[
"path"] = req->
path;
29 json params = json::object();
30 for (
const auto& [k, v] : req->
params) {
33 out[
"params"] = params;
35 json query = json::object();
36 for (
const auto& [k, v] : req->
queries) {
41 json headers = json::object();
42 headers[
"x-test"] = req->
get("X-Test");
43 out[
"headers"] = headers;
53static void tracePush(
const std::shared_ptr<express::
Request>& req,
const std::string& label) {
63 json arr = json::array();
66 for (
auto const& e : t) {
74int main(
int argc,
char* argv[]) {
77 const express::legacy::
in::WebApp app
("express-compat");
81 res->
json({{
"ok",
true}, {
"server",
"snodec"}, {
"express",
true}}
);
86 res->
json({{
"ok",
true}, {
"label",
"health"}}
);
127 if (req->
params[
"id"] ==
"0") {
136 app.get(
"/nr/:id(\\d+)", []
APPLICATION(req, res) {
143 if (req->queries[
"allow"] !=
"true") {
152 app.use(
"/guarded", guarded);
155 app.get(
"/guarded/:rest(.*)", []
APPLICATION(req, res) {
156 res->
status(403
).json({{
"label",
"guarded_fallback"}, {
"status", 403}}
);
184 app.use(
"/api", api);
189 mpMerge.get(
"/users/:id", []
APPLICATION(req, res) {
192 app.use(
"/mp/merge/t/:tenant", mpMerge);
195 mpNoMerge.get(
"/users/:id", []
APPLICATION(req, res) {
198 app.use(
"/mp/nomerge/t/:tenant", mpNoMerge);
201 auto makeScope = [](
bool merge) {
204 parent.use([merge](
auto const& req,
auto const&,
auto& next) {
205 tracePush(req, merge ?
"scopeMerge.parent" :
"scopeNoMerge.parent");
211 child.use([merge](
auto const& req,
auto const&,
auto& next) {
212 tracePush(req, merge ?
"scopeMerge.child" :
"scopeNoMerge.child");
215 child.get(
"/end", [merge](
auto const& req,
auto const& res) {
216 tracePush(req, merge ?
"scopeMerge.handler" :
"scopeNoMerge.handler");
217 res->json({{
"label", merge ?
"scope_merge" :
"scope_nomerge"}, {
"trace", traceGet(req)}});
220 parent.use(
"/b/:b", child);
224 app.use(
"/scope/nomerge/:a", makeScope(
false));
225 app.use(
"/scope/merge/:a", makeScope(
true));
237 app
.listen(8080
, [](
const express::legacy::
in::WebApp::SocketAddress& socketAddress,
const core::socket::
State& state) {
240 VLOG(1) <<
"express-compat listening on '" << socketAddress
.toString() <<
"'";
243 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)
LogMessage(Level level, int verboseLevel=-1, bool withErrno=false)
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)