67 {
69
71 std::cout << "Hier 1a ************" << std::endl;
72 std::cout << "Params: " << req->params["id"] << std::endl;
73 res->send("Done\n");
74 });
75
77 std::cout << "Hier 1b ************" << std::endl;
78 std::cout << "Params: " << req->params["id"] << std::endl;
79 std::cout << "Params: " << req->params["subcollection"] << std::endl;
80 res->send("Done\n");
81 });
82
84 std::cout << "Hier 2a ************" << std::endl;
85 std::cout << "Params: " << req->params["id"] << std::endl;
86 res->send("Done\n");
87 });
88
90 std::cout << "Hier 2b ************" << std::endl;
91 std::cout << "Params: " << req->params["id"] << std::endl;
92 std::cout << "Params: " << req->params["subcollection"] << std::endl;
93 res->send("Done\n");
94 });
95
97 std::cout << "Hier 3 ************" << std::endl;
98 std::cout << "Params: " << req->params["variable"] << std::endl;
99 std::cout << "Params: " << req->params["uri"] << std::endl;
100 res->send("Done\n");
101 });
102
104 .get(
105 "/query/:userId",
107 VLOG(1) << "Move on to the next route to query database";
108 next();
109 },
111 VLOG(1) << "UserId: " << req->params["userId"];
112 std::string userId = req->params["userId"];
113
114 req->setAttribute<std::string, "html-table">(std::string());
115
116 req->getAttribute<std::string, "html-table">([&userId](std::string& table) {
117 table = "<html>\n"
118 " <head>\n"
119 " <title>"
120 "Response from snode.c for " +
121 userId +
122 "\n"
123 " </title>\n"
124 " </head>\n"
125 " <body>\n"
126 " <h1>Return for " +
127 userId +
128 "\n"
129 " </h1>\n"
130 " <body>\n"
131 " <table border = \"1\">\n";
132 });
133
134 int i = 0;
136 "SELECT * FROM snodec where username = '" + userId + "'",
137 [next, req, i](const MYSQL_ROW row) mutable {
138 if (row != nullptr) {
139 i++;
140 req->getAttribute<std::string, "html-table">([row, &i](std::string& table) {
141 table.append(" <tr>\n"
142 " <td>\n" +
143 std::to_string(i) +
144 "\n"
145 " </td>\n"
146 " <td>\n" +
147 std::string(row[0]) +
148 "\n"
149 " </td>\n"
150 " <td>\n" +
151 row[1] +
152 "\n"
153 " </td>\n"
154 " </tr>\n");
155 });
156 } else {
157 req->getAttribute<std::string, "html-table">([](std::string& table) {
158 table.append(std::string(" </table>\n"
159 " </body>\n"
160 "</html>\n"));
161 });
162 VLOG(1) << "Move on to the next route to send result";
163 next();
164 }
165 },
166 [res, userId](const std::string& errorString, unsigned int errorNumber) {
167 VLOG(1) << "Error: " << errorString << " : " << errorNumber;
168 res->status(404).send(userId + ": " + errorString + " - " + std::to_string(errorNumber));
169 });
170 },
172 VLOG(1) << "And again 1: Move on to the next route to send result";
173 next();
174 },
176 VLOG(1) << "And again 2: Move on to the next route to send result";
177 next();
178 })
180 VLOG(1) << "And again 3: Move on to the next route to send result";
181 next();
182 })
184 VLOG(1) << "SendResult";
185
186 req->getAttribute<std::string, "html-table">(
187 [res](std::string& table) {
188 res->send(table);
189 },
190 [res](const std::string&) {
191 res->end();
192 });
193 });
195 VLOG(1) << "Show account of";
196 VLOG(1) << "UserId: " << req->params["userId"];
197 VLOG(1) << "UserName: " << req->params["userName"];
198
199 const std::string response = "<html>"
200 " <head>"
201 " <title>Response from snode.c</title>"
202 " </head>"
203 " <body>"
204 " <h1>Regex return</h1>"
205 " <ul>"
206 " <li>UserId: " +
207 req->params["userId"] +
208 " </li>"
209 " <li>UserName: " +
210 req->params["userName"] +
211 " </li>"
212 " </ul>"
213 " </body>"
214 "</html>";
215
216 const std::string userId = req->params["userId"];
217 const std::string userName = req->params["userName"];
218
220 "INSERT INTO `snodec`(`username`, `password`) VALUES ('" + userId + "','" + userName + "')",
221 [userId, userName]() {
222 VLOG(1) << "Inserted: -> " << userId << " - " << userName;
223 },
224 [](const std::string& errorString, unsigned int errorNumber) {
225 VLOG(1) << "Error: " << errorString << " : " << errorNumber;
226 });
227
228 res->send(response);
229 });
230 router.get(
"/asdf/:testRegex1(d\\d{3}e)/jklö/:testRegex2", []
APPLICATION(req, res) {
231 VLOG(1) << "Testing Regex";
232 VLOG(1) << "Regex1: " << req->params["testRegex1"];
233 VLOG(1) << "Regex2: " << req->params["testRegex2"];
234
235 const std::string response = "<html>"
236 " <head>"
237 " <title>Response from snode.c</title>"
238 " </head>"
239 " <body>"
240 " <h1>Regex return</h1>"
241 " <ul>"
242 " <li>Regex 1: " +
243 req->params["testRegex1"] +
244 " </li>"
245 " <li>Regex 2: " +
246 req->params["testRegex2"] +
247 " </li>"
248 " </ul>"
249 " </body>"
250 "</html>";
251
252 res->send(response);
253 });
255 VLOG(1) << "Show Search of";
256 VLOG(1) << "Search: " << req->params["search"];
257 VLOG(1) << "Queries: " << req->query("test");
258
259 res->send(req->params["search"]);
260 });
262 res->status(404).send("Not found: " + req->url);
263 });
264
266}
#define APPLICATION(req, res)
#define MIDDLEWARE(req, res, next)
MariaDBCommandSequence & exec(const std::string &sql, const std::function< void(void)> &onExec, const std::function< void(const std::string &, unsigned int)> &onError)
MariaDBCommandSequence & query(const std::string &sql, const std::function< void(const MYSQL_ROW)> &onQuery, const std::function< void(const std::string &, unsigned int)> &onError)