-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnpp_app.cpp
More file actions
353 lines (265 loc) · 11.2 KB
/
npp_app.cpp
File metadata and controls
353 lines (265 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* --------------------------------------------------------------------------
Node++ Web App
Jurek Muszynski
nodepp.org
-----------------------------------------------------------------------------
Hello World Sample Node++ Web Application
-------------------------------------------------------------------------- */
#include "npp.h"
/* --------------------------------------------------------------------------
Output HTML & page header
-------------------------------------------------------------------------- */
static void header()
{
OUT("<!DOCTYPE html>");
OUT("<html>");
OUT("<head>");
OUT("<meta charset=\"UTF-8\">");
OUT("<title>%s</title>", NPP_APP_NAME);
#ifdef NPP_APP_DESCRIPTION
OUT("<meta name=\"description\" content=\"%s\">", NPP_APP_DESCRIPTION);
#endif
if ( REQ_MOB )
OUT("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
OUT("</head>");
OUT("<body>");
/* -------------------------------------------------------------------------- */
/* style */
OUT("<style>");
if ( REQ_DSK )
OUT("body{margin:0 25px 40px 25px;}"); /* for desktop only */
OUT("code{font-size:1.1em;}");
OUT(".m{font-family:monospace;font-size:1.1em;}");
OUT(".mt{margin-top:40px;}");
OUT("</style>");
/* -------------------------------------------------------------------------- */
/* first row -- logo */
OUT("<div>");
static char logo[]="<img src=\"/nodepp.jpg\" alt=\"Logo\" width=\"250\" height=\"62\">";
if ( REQ("") )
OUT("<h1>%s</h1>", logo);
else
OUT("<h1><a href=\"/\">%s</a></h1>", logo);
OUT("</div>"); /* end of first row */
/* -------------------------------------------------------------------------- */
/* second row -- main menu */
OUT("<div style=\"margin-bottom:2.5em;\">");
char lnk_home[256]="<a href=\"/\">Home</a>";
char lnk_welcome[256]="<a href=\"/welcome\">Welcome</a>";
char lnk_snippets[256]="<a href=\"/snippets\">Snippets</a>";
char lnk_performance[256]="<a href=\"/performance\">Performance</a>";
if ( REQ("") )
strcpy(lnk_home, "<b>Home</b>");
else if ( REQ("welcome") )
strcpy(lnk_welcome, "<b>Welcome</b>");
else if ( REQ("snippets") )
strcpy(lnk_snippets, "<b>Snippets</b>");
else if ( REQ("performance") )
strcpy(lnk_performance, "<b>Performance</b>");
/* display menu */
OUT(lnk_home);
OUT(" | ");
OUT(lnk_welcome);
OUT(" | ");
OUT(lnk_snippets);
OUT(" | ");
OUT(lnk_performance);
OUT("</div>"); /* end of second row */
OUT("<hr style=\"margin-bottom:28px;\">");
}
/* --------------------------------------------------------------------------
Output footer; body & html tags close here
-------------------------------------------------------------------------- */
static void footer()
{
OUT("</body></html>");
}
/* --------------------------------------------------------------------------
Render landing page
-------------------------------------------------------------------------- */
void render_landing()
{
header();
OUT("<h2>Congratulations!</h2>");
OUT("<p>You've managed to compile and run Node++ web application! I hope you will like the performance of the compiled code.</p>");
/* show client type */
if ( REQ_DSK )
OUT("<p>You're on desktop, right?</p>");
else if ( REQ_TAB )
OUT("<p>You're on tablet, right?</p>");
else /* REQ_MOB */
OUT("<p>You're on the phone, right?</p>");
/* show some info */
OUT("<p>You can see how a <a href=\"/welcome\">simple form</a> works.</p>");
OUT("<p>You can use <a href=\"/snippets\">HTML and Markdown snippets</a> for static content.</p>");
OUT("<p>You can also estimate <a href=\"/performance\">your server's performance</a>.</p>");
OUT("<p>You can modify this app in <b class=m>src/npp_app.cpp</b>. The entry point is <code>npp_app_main()</code>. Have fun!</p>");
/* --------------------------------------------------- */
OUT("<p class=mt><i>To see the source code look at <code>render_landing()</code>.</i></p>");
footer();
}
/* --------------------------------------------------------------------------
Render page
-------------------------------------------------------------------------- */
void render_welcome()
{
header();
/* display form */
OUT("<p>Say something about yourself:</p>");
OUT("<form action=\"/welcome\" style=\"width:250px;text-align:right;margin-bottom:2em;\">");
OUT("<p>Name: <input name=\"name\" autofocus></p>");
OUT("<p>Age: <input name=\"age\" type=\"number\"></p>");
OUT("<p><input type=\"submit\" value=\"Submit\"></p>");
OUT("</form>");
/* try to retrieve query string values */
QSVAL name; /* string value */
int age; /* integer */
if ( QS("name", name) ) /* if present, bid welcome */
OUT("<p>Welcome <b>%s</b>, my dear friend!</p>", name); /* QS sanitizes strings by default */
if ( QSI("age", &age) ) /* if present, say something nice */
OUT("<p>It's good to see you in a good health, despite being already %d years old!</p>", age);
/* --------------------------------------------------- */
if ( QS("name", NULL) ) /* show this only if there's a name in query string */
OUT("<p class=mt><i>To see the source code look at <code>render_welcome()</code>.</i></p>");
footer();
}
/* --------------------------------------------------------------------------
Render page
-------------------------------------------------------------------------- */
void render_snippets()
{
header();
OUT("<p>You don't have to wrap all the output in <b class=m>OUT</b> macros.</p>");
OUT_SNIPPET("sample_snippet.html");
OUT("<hr style=\"margin-top:30px;\">");
OUT_SNIPPET_MD("sample_snippet.md");
/* --------------------------------------------------- */
OUT("<p class=mt><i>To see the source code look at <code>render_snippets()</code>.</i></p>");
footer();
}
/* --------------------------------------------------------------------------
Render page
-------------------------------------------------------------------------- */
void render_performance()
{
header();
if ( G_cnts_today.req < 2 )
{
OUT("<p>Refresh this page to let the server measure the average processing time.</p>");
}
else /* server average is available only after the first request has finished */
{
long long requests_daily = 86400000 / G_cnts_today.average;
OUT("<p>Based on %s requests, the average rendering time is %0.3lf ms.</p>", INT(G_cnts_today.req), G_cnts_today.average);
OUT("<p>It seems that this Node++ application could handle up to <b>%s</b> requests per day on your server.</p>", INT(requests_daily));
OUT("<p>Refresh this page a couple of times to obtain more accurate estimation.</p>");
}
/* --------------------------------------------------- */
OUT("<p class=mt><i>To see the source code look at <code>render_performance()</code>.</i></p>");
RES_DONT_CACHE;
footer();
}
/* --------------------------------------------------------------------------------
This is the main entry point for a request
------------------------------
Called after parsing HTTP request headers
------------------------------
If required (NPP_REQUIRED_AUTH_LEVEL >= AUTH_LEVEL_ANONYMOUS),
the session is already created
If valid ls cookie is present in the request or
it's over existing connection that has already been authenticated,
the session is already authenticated
------------------------------
Response status is 200 by default
Use RES_STATUS() if you want to change it
Response content type is text/html by default
Use RES_CONTENT_TYPE() if you want to change it
-------------------------------------------------------------------------------- */
void npp_app_main()
{
if ( REQ("") ) // landing page
{
render_landing();
}
else if ( REQ("welcome") )
{
render_welcome();
}
else if ( REQ("snippets") )
{
render_snippets();
}
else if ( REQ("performance") )
{
render_performance();
}
else // page not found
{
RES_STATUS(404);
}
}
/* --------------------------------------------------------------------------------
Called when application starts
------------------------------
Return true if everything OK
------------------------------
Returning false will stop booting process,
npp_app_done() will be called and application will be terminated
-------------------------------------------------------------------------------- */
bool npp_app_init(int argc, char *argv[])
{
return true;
}
/* --------------------------------------------------------------------------------
Called when new anonymous session is starting
------------------------------
Return true if everything OK
------------------------------
Returning false will cause the session to be closed
and npp_app_session_done() will be called
Response status will be set to 500
-------------------------------------------------------------------------------- */
bool npp_app_session_init()
{
return true;
}
#ifdef NPP_USERS
/* --------------------------------------------------------------------------------
******* Only for USERS *******
------------------------------
Called after successful authentication (using password or cookie)
when user session is upgraded from anonymous to logged in
------------------------------
Return true if everything OK
------------------------------
Returning false will cause the session to be downgraded back to anonymous
and npp_app_user_logout() will be called
-------------------------------------------------------------------------------- */
bool npp_app_user_login()
{
return true;
}
/* --------------------------------------------------------------------------------
******* Only for USERS *******
------------------------------
Called when downgrading logged in user session to anonymous
Application session data (SESSION_DATA) will be zero-ed as well,
unless NPP_KEEP_SESSION_DATA_ON_LOGOUT is defined
-------------------------------------------------------------------------------- */
void npp_app_user_logout()
{
}
#endif /* NPP_USERS */
/* --------------------------------------------------------------------------------
Called when closing anonymous session
After calling this the session memory will be zero-ed
-------------------------------------------------------------------------------- */
void npp_app_session_done()
{
}
/* --------------------------------------------------------------------------------
Called when application shuts down
-------------------------------------------------------------------------------- */
void npp_app_done()
{
}