diff --git a/examples/restful_api/index.html b/examples/restful_api/index.html new file mode 100644 index 00000000..9051ff08 --- /dev/null +++ b/examples/restful_api/index.html @@ -0,0 +1,66 @@ + + + + + RESTful API demo + + + + + + + +
+

RESTful API demo.

+ +

+ This page demonstrates how Mongoose web server could be used to implement + RESTful APIs. Enter numbers below, and press Submit. Browser will send + two numbers to /api/sum URI, Mongoose calclulates the sum of + two and returns the result. +

+ +
+ +
+ +
+   +
+ +
+ + diff --git a/examples/restful_api/restful_api.c b/examples/restful_api/restful_api.c index 8884c5c2..ff0ac83a 100644 --- a/examples/restful_api/restful_api.c +++ b/examples/restful_api/restful_api.c @@ -3,62 +3,32 @@ #include #include "mongoose.h" -static int static_value = 123; // Exposed and changeable via the form +static const char *s_no_cache_header = + "Cache-Control: max-age=0, post-check=0, " + "pre-check=0, no-store, no-cache, must-revalidate\r\n"; -static void print_html_form(struct mg_connection *conn) { - mg_send_header(conn, "Content-Type", "text/html"); - mg_send_header(conn, "Cache-Control", "max-age=0, post-check=0, " - "pre-check=0, no-store, no-cache, must-revalidate"); +static void handle_restful_call(struct mg_connection *conn) { + char n1[100], n2[100]; - // Note that all the following normally should reside in static HTML page - mg_printf_data(conn, "%s", ""); - // It is better to use local copy though - mg_printf_data(conn, "", - "http://code.jquery.com/jquery-1.11.0.min.js"); - mg_printf_data(conn, "%s", ""); - mg_printf_data(conn, "%s", ""); - mg_printf_data(conn, "%s", "

Ajax form submission example

"); - mg_printf_data(conn, "%s", "
"); - mg_printf_data(conn, "%s", "Device value: "); - mg_printf_data(conn, "%s", ""); - mg_printf_data(conn, "%s", "
"); - mg_printf_data(conn, "%s", ""); + // Get form variables + mg_get_var(conn, "n1", n1, sizeof(n1)); + mg_get_var(conn, "n2", n2, sizeof(n2)); + + mg_printf_data(conn, "{ \"result\": %lf }", strtod(n1, NULL) + strtod(n2, NULL)); } static int ev_handler(struct mg_connection *conn, enum mg_event ev) { - if (ev == MG_REQUEST) { - if (strcmp(conn->uri, "/get_value") == 0) { - mg_printf_data(conn, "{\"value\": %d}", static_value); - } else if (strcmp(conn->uri, "/set_value") == 0) { - // This Ajax endpoint sets the new value for the device variable - char buf[100] = ""; - mg_get_var(conn, "new_value", buf, sizeof(buf)); // Get form variable - static_value = atoi(buf); // Set new value - mg_printf_data(conn, "%s", "{\"success\": true}"); - printf("Setting value to [%d]\n", static_value); // Debug trace - } else { - // Better way is to set "document_root" option, put "index.html" file - // into document_root and return MG_FALSE here. We're printing HTML - // page by hands just to keep everything in one C file. - print_html_form(conn); - } - return MG_TRUE; - } else if (ev == MG_AUTH) { - return MG_TRUE; + switch (ev) { + case MG_AUTH: return MG_TRUE; + case MG_REQUEST: + if (!strcmp(conn->uri, "/api/sum")) { + handle_restful_call(conn); + return MG_TRUE; + } + mg_send_file(conn, "index.html", s_no_cache_header); + return MG_MORE; + default: return MG_FALSE; } - - return MG_FALSE; } int main(void) {