Quick Nav
See Also
Embedding Appweb
When embedding Appweb in your application or system, you have a two options:
- Create an Appweb loadable module for your application.
- Embed the Appweb HTTP library in your application.
Loadable Modules
You can extend the Appweb server program by creating an Appweb loadable module. You do this by creating a shared library that contains your application and some Appweb module interface code. This module is then specified in the Appweb configuration file so that the Appweb program will load it. It requires about 10 lines of code to create an Appweb module.
Embed the Appweb Library
You can embed the Appweb library in your application to enable it to listen for HTTP requests and thus become a HTTP server itself. Embedding the Appweb library is easy and can be done with as little as one line of code.
The Appweb Server Program
The Appweb product includes a fully-featured HTTP server program that uses the Appweb HTTP library. This server (called appweb) is run by default when you install the Appweb binary distribution or run "make install" after building from source.
The Appweb server program is ideal for embedded systems as it offers the following features:
- High performance
- Extendable via loadable modules
- Deterministic memory footprint and CPU loads
- Configurable sandbox limits that control memory usage
- Monitored and managed by the Appweb Angel process
Creating a Module
You can extend the Appweb program by creating a loadable module for your application code.
To initialize your module and register with Appweb, you must create an initialization function that is called when Appweb loads your module. This must be named according to the form:
mprNameModuleInit(MaHttp *http)
Where Name is the name of your module.
This initialization function must call the mprCreateModule API to register with Appweb.
MprModule *maSimpleModuleInit(MaHttp *http) { MprModule *module; module = mprCreateModule(http, "testModule", "1.0.0", NULL, NULL, NULL); if (module == 0) { return 0; } /* Put custom code here */ return module; }
See Creating Appweb Modules for more details.
Embed the Appweb Library
You can also embed the Appweb HTTP library in your main program to enable it to function as a HTTP server itself. This is ideal if your application is large and/or has a complex I/O, eventing or threading paradigm.
Linking with the Appweb Library
To include the Appweb library in your program you need to do the following things:
- Add #include "appweb/appweb.h" to the relevant source files.
- Add the Appweb library to your Makefiles or Windows project files. This will mean adding libappweb.dll on Windows or libappweb.so on Unix.
- Use one of the embedding APIs to create the HTTP server.
One Line Embedding
The following code demonstrates the one-line Appweb embedding API. This will create and configure a web server based on the "server.conf" configuration file.
#include "appweb/appweb.h" int main(int argc, char** argv) { return maRunWebServer("server.conf"); }
To build this sample and link with the Appweb library:
cc -o server server.c -lappweb
Full Control API
The Appweb library also provides a lower level embedding interface where you can precisely control how the web server is created and configured. This API also exposes the inner event and threading mechanisms.
This example creates a web server using the "server.conf" configuration file and will service events until terminated.
#include "appweb/appweb.h" int main(int argc, char **argv) { Mpr *mpr; MaHttp *http; MaServer *server; /* * Initialize and start the portable runtime services. */ if ((mpr = mprCreate(0, NULL, NULL)) == 0) { mprError(mpr, "Can't create the web server runtime"); return 0; } if (mprStart(mpr, 0) < 0) { mprError(mpr, "Can't start the web server runtime"); return 0; } http = maCreateHttp(mpr); if ((server = maCreateServer(http, "server.conf", NULL, NULL, -1)) == 0) { mprError(mpr, "Can't create the web server"); return 0; } if (maParseConfig(server, "server.conf") < 0) { mprError(mpr, "Can't parse the config file %s", "server.conf"); return 0; } if (maStartHttp(http) < 0) { mprError(http, "Can't start the web server"); return MPR_ERR_CANT_CREATE; } mprServiceEvents(http, -1, 0); maStopHttp(http); mprFree(mpr); return 0; }
More Details
For more details about the embedding API, please consult the Appweb API and the Native APIs.