From 492328474d179087c79426183ec9383c487c6a82 Mon Sep 17 00:00:00 2001
From: Sergey Lyubka <valenok@gmail.com>
Date: Mon, 16 Dec 2013 13:33:50 +0000
Subject: [PATCH] Only hello example left, adopted to the new API

---
 examples/Makefile | 27 +++++++-----------------
 examples/hello.c  | 53 ++++++++++++++---------------------------------
 2 files changed, 24 insertions(+), 56 deletions(-)

diff --git a/examples/Makefile b/examples/Makefile
index b39c20aca..7c7c7cb00 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -16,25 +16,14 @@ else
   endif
 endif
 
-all: hello upload post websocket chat
-
-hello:
-	$(CC) hello.c ../mongoose.c  -o $@ $(CFLAGS)
-
-upload:
-	$(CC) upload.c ../mongoose.c -o $@ $(CFLAGS)
-
-post:
-	$(CC) post.c ../mongoose.c -o $@ $(CFLAGS)
-
-websocket:
-	$(CC) -DUSE_WEBSOCKET websocket.c ../mongoose.c -o $@ $(CFLAGS)
-
-chat:
-	$(CC) chat.c ../mongoose.c -o $@ $(CFLAGS)
-
-lua_dll:
-	$(CC) lua_dll.c ../build/lua_5.2.1.c -o $@.so $(CFLAGS) $(DLL_FLAGS)
+all:
+	$(CC) hello.c ../mongoose.c  -o hello $(CFLAGS)
+
+#	$(CC) upload.c ../mongoose.c -o upload $(CFLAGS)
+#	$(CC) post.c ../mongoose.c -o post $(CFLAGS)
+#	$(CC) -DUSE_WEBSOCKET websocket.c ../mongoose.c -o $@ $(CFLAGS)
+#	$(CC) chat.c ../mongoose.c -o chat $(CFLAGS)
+#	$(CC) lua_dll.c ../build/lua_5.2.1.c -o $@.so $(CFLAGS) $(DLL_FLAGS)
 
 
 MSVC    = ../../vc6
diff --git a/examples/hello.c b/examples/hello.c
index b516d7eb5..c0369665f 100644
--- a/examples/hello.c
+++ b/examples/hello.c
@@ -2,50 +2,29 @@
 #include <string.h>
 #include "mongoose.h"
 
-// This function will be called by mongoose on every new request.
-static int event_handler(struct mg_event *event) {
-
-  if (event->type == MG_REQUEST_BEGIN) {
-    char content[100];
-
-    // Prepare the message we're going to send
-    int content_length = snprintf(content, sizeof(content),
-        "Hello from mongoose! Requested: [%s] [%s]",
-        event->request_info->request_method, event->request_info->uri);
-
-    // Send HTTP reply to the client
-    mg_printf(event->conn,
-        "HTTP/1.1 200 OK\r\n"
-        "Content-Type: text/plain\r\n"
-        "Content-Length: %d\r\n"        // Always set Content-Length
-        "\r\n"
-        "%s",
-        content_length, content);
-
-    // Returning non-zero tells mongoose that our function has replied to
-    // the client, and mongoose should not send client any more data.
-    return 1;
-  }
-
-  // We do not handle any other event
+// This function will be called by mongoose on every new request
+static int index_html(struct mg_connection *conn) {
+  static const char *reply = "HTTP/1.0 200 OK\r\n\r\nHello!";
+  mg_write(conn, reply, strlen(reply));
   return 0;
 }
 
 int main(void) {
-  struct mg_context *ctx;
+  struct mg_server *server;
 
-  // List of options. Last element must be NULL.
-  const char *options[] = {"listening_ports", "8080", NULL};
+  // Create and configure the server
+  server = mg_create_server(NULL);
+  mg_set_option(server, "listening_port", "8080");
+  mg_add_uri_handler(server, "/", index_html);
 
-  // Start the web server.
-  ctx = mg_start(options, &event_handler, NULL);
-
-  // Wait until user hits "enter". Server is running in separate thread.
-  // Navigating to http://localhost:8080 will invoke begin_request_handler().
-  getchar();
+  // Serve request. Hit Ctrl-C to terminate the program
+  printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
+  for (;;) {
+    mg_poll_server(server, 1000);
+  }
 
-  // Stop the server.
-  mg_stop(ctx);
+  // Cleanup, and free server instance
+  mg_destroy_server(&server);
 
   return 0;
 }
-- 
GitLab