Difference between revisions of "Embedded HTTP Server"
Yvonne.milne (Talk | contribs) (→Runtime Port) |
Yvonne.milne (Talk | contribs) (→Overview) |
||
Line 20: | Line 20: | ||
<code lang="recital"> | <code lang="recital"> | ||
− | // In this example, the | + | // In this Windows example, the p_url will contain "C:\Lianja\library\dws_getinfo" |
− | // dws_getInfo.prg in the library directory | + | // or "C:\Lianja\cloudserver\tenants\public\library\dws_getinfo" (Runtime Port) |
− | parameter p_url | + | // and p_arg will contain "customerid=ALFKI" |
− | + | // Create dws_getInfo.prg in the relevant library directory | |
− | private | + | parameter p_url, p_arg |
− | + | private aArgs, p_customerid, cName, cValue | |
− | return | + | private oArgs[] |
+ | |||
+ | // Escape any backslashes for JSON | ||
+ | p_url = strtran(p_url,"\","\\") | ||
+ | |||
+ | // Load the arguments into a static array | ||
+ | aArgs = explode("&", p_arg) | ||
+ | |||
+ | // Create an associative array to access arguments by name | ||
+ | for i = 1 to alen(aArgs) | ||
+ | cName = substr(aArgs[i],1,at("=",aArgs[i])-1) | ||
+ | cValue = substr(aArgs[i],at("=",aArgs[i])+1) | ||
+ | oArgs[cname] = cValue | ||
+ | endfor | ||
+ | |||
+ | p_customerid = getMember("oArgs", "customerid", "") | ||
+ | return '{"url": "&p_url", "customerid": "&p_customerid"}' | ||
</code> | </code> | ||
Revision as of 11:06, 1 April 2020
Contents
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 aArgs, p_customerid, cName, cValue private oArgs[] // Escape any backslashes for JSON p_url = strtran(p_url,"\","\\") // Load the arguments into a static array aArgs = explode("&", p_arg) // Create an associative array to access arguments by name for i = 1 to alen(aArgs) cName = substr(aArgs[i],1,at("=",aArgs[i])-1) cValue = substr(aArgs[i],at("=",aArgs[i])+1) oArgs[cname] = cValue endfor 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.
Remember to use SAVE DATASESSION/RESTORE DATASESSION as these web service requests are handled asynchronously while your App is running.
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.