Embedded HTTP Server

From Lianjapedia
Jump to: navigation, search

See Also

App Settings, HTTPServer Framework Class

Overview

Imagine...

You have a desktop app that is running on user desktops.

You want to somehow get information from the running app and return that information as JSON, XML or an HTML page.

Well, in Lianja you can.

There is an simple HTTP server built into your Lianja Apps that run under the App Center. By default this server listens on port 8002 but this is configurable (in the App Settings) and handles asynchronous service requests while your App is running.

So while an App is running, from another App you can query it e.g.:

result = getURL("http://localhost:8002/desktopwebservice/library:/dws_getinfo?customerid=ALFKI")

The web service name follows /desktopwebservice/ in the URL and is a normal program or proc. In this case we have a script named dws_getinfo. It is good practice to name these consistently and place them in the library directory. We recommend prefixing your desktop web services with dws_.

// In this Windows example, the p_url will contain "C:\Lianja\library\dws_getinfo"
// or "C:\Lianja\cloudserver\tenants\public\library\dws_getinfo" (Runtime Port)
// and p_arg will contain "customerid=ALFKI"
// Create dws_getInfo.prg in the relevant library directory
parameter p_url, p_arg
private p_customerid
private oArgs[]

// Escape any backslashes for JSON
p_url = strtran(p_url,"\","\\")

// Create an object of the arguments to access by name
oArgs = metadata_decode(strtran(p_arg,"&",";"))

p_customerid = getMember("oArgs", "customerid", "")
return '{"url": "&p_url", "customerid": "&p_customerid"}'

Your service may return a JSON encoded object (as shown above), an XML string, an HTML string or plain text.

If the result returned starts with "file:" then the file specified e.g. "file:mytempfile.txt" is read and the result is sent back to the client. This provides the ability to query large amounts of information not limited by internal string length.

If the result returned starts with "tempfile:" then the file specified e.g. "tempfile:mytempfile.txt" is read and the result is sent back to the client. The file mytempfile.txt is then deleted.

Notice that in the example above no file extension was given to dws_getinfo so ".prg" (Lianja/VFP script) is assumed. You could also specify:

result = getUrl("http://localhost:8002/desktopwebservice/library:/dws_getinfo.js?customerid=ALFKI")

or

result = getUrl("http://localhost:8002/desktopwebservice/library:/dws_getinfo.php?customerid=ALFKI")

or

result = getUrl("http://localhost:8002/desktopwebservice/library:/dws_getinfo.py?customerid=ALFKI")

or

result = getUrl("http://localhost:8002/desktopwebservice/library:/dws_getinfo.rsp?customerid=ALFKI")

or

result = getUrl("http://localhost:8002/desktopwebservice/library:/dws_getinfo.jssp?customerid=ALFKI")

This is not designed to replace the Lianja Cloud Server which handles multiple concurrent requests but rather to provide a means of introspecting running apps and consolidating information from them.

It is not difficult to see how you could use this to great effect with desktop LAN apps in a Web App that uses a .rsp page that queries information from the various clients running Lianja Apps.

Bm-noteicon.png
Pro tip

Remember to use SAVE DATASESSION/RESTORE DATASESSION as these web service requests are handled asynchronously while your App is running.

App Settings

App Settings


Listen on

The interface the HTTP server should listen for request on. Default is 127.0.0.1.

Port

The port that the embedded HTTP server uses in development (Lianja App Builder). Default is 8002.

Runtime Port

The port that the embedded HTTP server uses at runtime (Lianja App Center). Default is 8003.

API key

The API key that should be specified to invoke desktop web services. Specify an apikey=value on the URI and the authenticity of the apikey will be verified when the desktop web service request is received. The default behavior is to service requests with no apikey authentication if none is specified for an app.

Enable services

Enable embedded web services (True | False).

HTTPServer

For information on the HTTPServer Framework Class see here.