restful_api.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <stdlib.h>
00004 #include "mongoose.h"
00005 
00006 static const char *s_no_cache_header =
00007   "Cache-Control: max-age=0, post-check=0, "
00008   "pre-check=0, no-store, no-cache, must-revalidate\r\n";
00009 
00010 static void handle_restful_call(struct mg_connection *conn) {
00011   char n1[100], n2[100];
00012 
00013   // Get form variables
00014   mg_get_var(conn, "n1", n1, sizeof(n1));
00015   mg_get_var(conn, "n2", n2, sizeof(n2));
00016 
00017   mg_printf_data(conn, "{ \"result\": %lf }", strtod(n1, NULL) + strtod(n2, NULL));
00018 }
00019 
00020 static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
00021   switch (ev) {
00022     case MG_AUTH: return MG_TRUE;
00023     case MG_REQUEST:
00024       if (!strcmp(conn->uri, "/api/sum")) {
00025         handle_restful_call(conn);
00026         return MG_TRUE;
00027       }
00028       mg_send_file(conn, "index.html", s_no_cache_header);
00029       return MG_MORE;
00030     default: return MG_FALSE;
00031   }
00032 }
00033 
00034 int main(void) {
00035   struct mg_server *server;
00036 
00037   // Create and configure the server
00038   server = mg_create_server(NULL, ev_handler);
00039   mg_set_option(server, "listening_port", "8000");
00040 
00041   // Serve request. Hit Ctrl-C to terminate the program
00042   printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
00043   for (;;) {
00044     mg_poll_server(server, 1000);
00045   }
00046 
00047   // Cleanup, and free server instance
00048   mg_destroy_server(&server);
00049 
00050   return 0;
00051 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06