Embedding Appweb

When extending and embedding Appweb in your application or system, you have three options:

  1. Use the existing Appweb main program and extend Appweb via a loadable module.
  2. Use the existing Appweb main program and extend Appweb via the ESP web framework.
  3. Link the Appweb HTTP library with your application main program.

Extending via a Loadable Module

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.

Extending via ESP

The Embedded Server Pages (ESP) web framework allows the direct embedding of "C" language code in HTML web pages. This is the easiest way to invoke "C" APIs in your program. ESP also supports the loading of Model/View/Controller (MVC-based) applications. ESP will compile and load controller source code. Controllers provide an easy and efficient way to invoke "C" code when requests are made to certain URIs.

Embed the Appweb Library

You can link the Appweb library with 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:

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:

httpNameInit(MaHttp *http, MprModule *module)

Where Name is the name of your module.

int httpSimpleInit(Http *http, MprModule *module) { /* Put custom code here */ return 0; }

See Creating Appweb Modules for more details.

Embed the Appweb Library

You can link the Appweb HTTP library with 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:

  1. Add #include "appweb/appweb.h" to the relevant source files.
  2. Add the Appweb library to your Makefiles or Windows project files. This will mean adding libappweb.dll on Windows or libappweb.so on Unix.
  3. 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; /* Initialize and start the portable runtime services. */ if ((mpr = mprCreate(argc, argv, MPR_USER_EVENTS_THREAD)) == 0) { mprError("Cannot create the web server runtime"); return -1; } if (mprStart() < 0) { mprError("Cannot start the web server runtime"); return -1; } if (httpCreate(HTTP_CLIENT_SIDE | HTTP_SERVER_SIDE) == 0) { mprError("Cannot create the Http server"); return -1; } if (maLoadModules() < 0) { mprError("Cannot load modules"); return -1; } if (maParseConfig("server.conf") < 0) { mprError("Cannot parse the config file %s", "server.conf"); return -1; } if (httpStartEndpoints() < 0) { mprError("Cannot start the web server"); return -1; } mprServiceEvents(-1, 0); mprDestroy(); return 0; }

The Appweb Typical Server sample demonstrates this technique more fully.

More Details

For more details about the embedding API, please consult the Appweb API and the Native APIs.

© Embedthis Software. All rights reserved.