Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mongoose
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ganil-acq
GANILinux
linux-service
library
mongoose
Commits
d4eae0e2
Commit
d4eae0e2
authored
11 years ago
by
Sergey Lyubka
Browse files
Options
Downloads
Patches
Plain Diff
Added minimalistic example
parent
4d7ea04d
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
docs/API.md
+32
-6
32 additions, 6 deletions
docs/API.md
with
32 additions
and
6 deletions
docs/API.md
+
32
−
6
View file @
d4eae0e2
...
...
@@ -6,13 +6,39 @@ Embedding Mongoose is done in two steps:
[
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_
start()`
to start the server.
Pass configuration options and event handlers to
`mg_start()`
. Call
`mg_
stop()`
when server needs to be stopped
.
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
.
Mongoose calls event handlers when certain events happen.
For example, when new request arrives, Mongoose calls
`begin_request`
handler to let user handle the request. In the handler, user code
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:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment