API.md is split to API.md and Embed.md

This commit is contained in:
Sergey Lyubka
2013-12-10 11:16:25 +00:00
parent d4eae0e251
commit 9bd5903f1d
2 changed files with 88 additions and 60 deletions
+1 -60
View File
@@ -1,63 +1,4 @@
# Mongoose Embedding Guide
Embedding Mongoose is done in two steps:
1. Copy
[mongoose.c](https://raw.github.com/cesanta/mongoose/master/mongoose.c) and
[mongoose.h](https://raw.github.com/cesanta/mongoose/master/mongoose.h)
to your application's source tree and include these two files in the build.
2. Somewhere in the application code, call `mg_create_server()` to create
a server, configure it with `mg_set_option()` and loop with
`mg_poll_server()` until done. Call `mg_destroy_server()` to cleanup.
Here's minimal application, suppose it is in the `minimal.c` file:
#include "mongoose.h"
int main(void) {
struct mg_server *server = mg_create_server(NULL);
mg_set_option(server, "document_root", ".");
mg_set_option(server, "listening_port", "8080");
for (;;) mg_poll_server(server, 1000); // Infinite loop, Ctrl-C to stop
mg_destroy_server(&server);
return 0;
}
To compile it, put `mongoose.c`, `mongoose.h` and `minimal.c` into one
folder, then run the following UNIX command:
cc minimal.c mongoose.c -o my_program
If you're on Windows, run this in a Visual Studio shell:
cl minimal.c mongoose.c /TC /MD
Mongoose can call user-defined functions when certain URIs are requested.
These functions are _called uri handlers_. `mg_add_uri_handler()` registers
an URI handler, and there is no restriction on the number of URI handlers.
Also, mongoose can call a user-defined function when it is about to send
HTTP error back to client. HTTP error handler function can be registered
with `mg_set_http_error_handler()`.
In a handler function, user code
can get all information about the request -- parsed headers, etcetera.
Here is a list of well-commented embedding examples:
* [hello.c](https://github.com/cesanta/mongoose/blob/master/examples/hello.c)
This is the most basic "Hello, world!" example
* [post.c](https://github.com/cesanta/mongoose/blob/master/examples/post.c)
This example shows how to handle form submission
* [upload.c](https://github.com/cesanta/mongoose/blob/master/examples/upload.c)
This example shows how to handle file upload
* [websocket.c](https://github.com/cesanta/mongoose/blob/master/examples/websocket.c)
This example shows how to handle websocket requests
* [chat.c](https://github.com/cesanta/mongoose/blob/master/examples/chat.c), [main.js](https://github.com/cesanta/mongoose/blob/master/examples/html/main.js)
An example of web chat application, with cookie-based user authentication,
session support. All UI is done using static HTML/CSS. Interaction
with backed is done using AJAX.
# API Reference
# Mongoose API Reference
struct mg_context *mg_start(const char **configuration_options
int (*event_handler_func)(struct mg_event *),