Silgy

Hello Worlds

1. Empty application = static web server

This is just an extremely fast static web server. If no resource is requested it'll look for index.html in res directory. If requested file is not present in res nor resmin, it'll return 404.
void silgy_app_main(int ci) { RES_STATUS(404); }

2. Simplest Hello World

Return static file if present, otherwise "Hello World!".
void silgy_app_main(int ci) { OUT("Hello World!"); }

3. Simple HTML with 2 pages

Application, yet without moving parts.
void silgy_app_main(int ci) { if ( REQ("") ) // landing page { OUT_HTML_HEADER; OUT("<h1>%s</h1>", APP_WEBSITE); OUT("<h2>Welcome to my web app!</h2>"); OUT_HTML_FOOTER; } else if ( REQ("about") ) { OUT_HTML_HEADER; OUT("<h1>%s</h1>", APP_WEBSITE); OUT("<h2>About</h2>"); OUT("<p>Hello World Sample Silgy Web Application</p>"); OUT("<p><a href=\"/\">Back to landing page</a></p>"); OUT_HTML_FOOTER; } else // page not found { RES_STATUS(404); } }

4. Using query string value

Finally some logic. QS() will automatically choose between query string or payload, depending on the HTTP request method.
void silgy_app_main(int ci) { if ( REQ("") ) // landing page { OUT_HTML_HEADER; OUT("<h1>%s</h1>", APP_WEBSITE); OUT("<h2>Welcome to my web app!</h2>"); if ( REQ_DSK ) OUT("<p>You're on desktop.</p>"); else /* REQ_MOB */ OUT("<p>You're on the phone.</p>"); OUT("<p>Click <a href=\"/welcome\">here</a> to try my welcoming bot.</p>"); OUT_HTML_FOOTER; } else if ( REQ("welcome") ) // welcoming bot { OUT_HTML_HEADER; OUT("<h1>%s</h1>", APP_WEBSITE); OUT("<p>Please enter your name:</p>"); OUT("<form action=\"welcome\"><input name=\"firstname\" autofocus> <input type=\"submit\" value=\"Run\"></form>"); QSVAL qs_firstname; // query string value if ( QS("firstname", qs_firstname) ) // if present, bid welcome OUT("<p>Welcome %s, my dear friend!</p>", qs_firstname); OUT("<p><a href=\"/\">Back to landing page</a></p>"); OUT_HTML_FOOTER; } else // page not found { RES_STATUS(404); } }


Copyright © 2018–2020 Jurek Muszyński